private static IEnumerable <Bookmark> locateChildren(BaseElementNavigator nav, IEnumerable <string> path, bool partial)
        {
            Debug.Assert(nav != null); // Caller should validate

            var child = path.First();
            var rest  = path.Skip(1);

            var bm = nav.Bookmark();

            if (nav.MoveToChild(child))
            {
                var result = new List <Bookmark>();

                do
                {
                    if (!rest.Any())
                    {
                        // Exact match!
                        result.Add(nav.Bookmark());
                    }
                    else if (!nav.HasChildren && partial)
                    {
                        // This is as far as we can get in this structure,
                        // so this is a hit too if partial hits are OK
                        result.Add(nav.Bookmark());
                    }
                    else
                    {
                        // So, no hit, but we have children that might fit the bill.
                        result.AddRange(locateChildren(nav, rest, partial));
                    }

                    // Try this for the other matching siblings too...
                }while (nav.MoveToNext(child));

                // We've scanned all my children and collected the results,
                // move the navigator back to where we were before
                nav.ReturnToBookmark(bm);
                return(result);
            }
            else
            {
                return(Enumerable.Empty <Bookmark>());
            }
        }
        /// <summary>Move to last direct child element with same path as current element.</summary>
        /// <returns><c>true</c> if the cursor has moved at least a single element, <c>false</c> otherwise</returns>
        public static bool MoveToLastSlice(this BaseElementNavigator nav)
        {
            if (nav == null)
            {
                throw Error.ArgumentNull("nav");
            }
            if (nav.Current == null)
            {
                throw Error.Argument("nav", "Cannot move to last slice. Current node is not set.");
            }
            if (nav.Current.Base == null)
            {
                throw Error.Argument("nav", "Cannot move to last slice. Current node has no Base.path component (path '{0}').".FormatWith(nav.Path));
            }

            var bm       = nav.Bookmark();
            var basePath = nav.Current.Base.Path;

            if (string.IsNullOrEmpty(basePath))
            {
                throw Error.Argument("nav", "Cannot move to last slice. Current node has no Base.path component (path '{0}').".FormatWith(nav.Path));
            }

            var result = false;

            while (nav.MoveToNext())
            {
                var baseComp = nav.Current.Base;
                if (baseComp != null && baseComp.Path == basePath)
                {
                    // Match, advance cursor
                    bm     = nav.Bookmark();
                    result = true;
                }
                else
                {
                    // Mismatch, back up to previous element and exit
                    nav.ReturnToBookmark(bm);
                    break;
                }
            }
            return(result);
        }
コード例 #3
0
        public static IEnumerable <Bookmark> Find(this BaseElementNavigator nav, string path)
        {
            var parts = path.Split('.');

            var bm = nav.Bookmark();

            nav.Reset();
            var result = locateChildren(nav, parts, partial: false);

            nav.ReturnToBookmark(bm);

            return(result);
        }
        public static bool DeleteChildren(this BaseElementNavigator nav)
        {
            var parent = nav.Bookmark();

            if (nav.MoveToFirstChild())
            {
                while (!nav.IsAtBookmark(parent))
                {
                    nav.DeleteTree();
                }
            }

            return(true);
        }
コード例 #5
0
        public static bool MoveToPrevious(this BaseElementNavigator nav, string name)
        {
            var bm = nav.Bookmark();

            while (nav.MoveToPrevious())
            {
                if (nav.PathName == name)
                {
                    return(true);
                }
            }

            nav.ReturnToBookmark(bm);
            return(false);
        }
        // [WMR 20160802] NEW

        /// <summary>Move the navigator to the next type slice of the (choice) element with the specified name, if it exists.</summary>
        /// <returns><c>true</c> if succesful, <c>false</c> otherwise.</returns>
        public static bool MoveToNextTypeSlice(this BaseElementNavigator nav, string name)
        {
            if (nav == null)
            {
                throw Error.ArgumentNull("nav");
            }
            var bm = nav.Bookmark();

            while (nav.MoveToNext())
            {
                if (NamedNavigation.IsRenamedChoiceElement(name, nav.PathName))
                {
                    return(true);
                }
            }

            nav.ReturnToBookmark(bm);
            return(false);
        }
コード例 #7
0
        private static void rebaseChildren(BaseElementNavigator nav, string path, List<string> newPaths)
        {
            var bm = nav.Bookmark();

            if (nav.MoveToFirstChild())
            {
                do
                {
                    var newPath = path + "." + nav.Current.GetNameFromPath();

                    newPaths.Add(newPath);

                    if(nav.HasChildren) 
                        rebaseChildren(nav, newPath, newPaths);
                }
                while (nav.MoveToNext());

                nav.ReturnToBookmark(bm);
            }
        }
コード例 #8
0
        private static void rebaseChildren(BaseElementNavigator nav, string path, List <string> newPaths)
        {
            var bm = nav.Bookmark();

            if (nav.MoveToFirstChild())
            {
                do
                {
                    var newPath = path + "." + nav.Current.GetNameFromPath();

                    newPaths.Add(newPath);

                    if (nav.HasChildren)
                    {
                        rebaseChildren(nav, newPath, newPaths);
                    }
                }while (nav.MoveToNext());

                nav.ReturnToBookmark(bm);
            }
        }
        public static IEnumerable <Bookmark> Approach(this BaseElementNavigator nav, string path)
        {
            if (nav == null)
            {
                throw Error.ArgumentNull("nav");
            }
            if (path == null)
            {
                throw Error.ArgumentNull("path");
            }

            var parts = path.Split('.');

            var bm = nav.Bookmark();

            nav.Reset();
            var result = locateChildren(nav, parts, partial: true);

            nav.ReturnToBookmark(bm);

            return(result);
        }
        public static bool AppendChild(this BaseElementNavigator nav, ElementDefinition child)
        {
            var bm = nav.Bookmark();

            if (nav.MoveToFirstChild())
            {
                while (nav.MoveToNext())
                {
                    ;
                }
                var result = nav.InsertAfter(child);

                if (!result)
                {
                    nav.ReturnToBookmark(bm);
                }
                return(result);
            }
            else
            {
                return(nav.InsertFirstChild(child));
            }
        }
コード例 #11
0
        private static IEnumerable<Bookmark> locateChildren(BaseElementNavigator nav, IEnumerable<string> path, bool partial)
        {
            var child = path.First();
            var rest = path.Skip(1);

            var bm = nav.Bookmark();

            if (nav.MoveToChild(child))
            {
                var result = new List<Bookmark>();

                do
                {
                    if (!rest.Any())
                    {
                        // Exact match!
                        result.Add(nav.Bookmark());
                    }
                    else if (!nav.HasChildren && partial)
                    {
                        // This is as far as we can get in this structure,
                        // so this is a hit too if partial hits are OK
                        result.Add(nav.Bookmark());
                    }
                    else
                    {
                        // So, no hit, but we have children that might fit the bill.
                        result.AddRange(locateChildren(nav, rest, partial));
                    }

                    // Try this for the other matching siblings too...
                }
                while (nav.MoveToNext(child));

                // We've scanned all my children and collected the results,
                // move the navigator back to where we were before
                nav.ReturnToBookmark(bm);
                return result;
            }
            else
                return Enumerable.Empty<Bookmark>();
        }