示例#1
0
        public void TesT22()
        {
            var root = new ItemRecord {
                ID = ItemIDs.RootItemID, Name = "Sitecore"
            };
            var namesCache = new ChildrenDataSet(new ItemDefinitions(root));

            // act
            Guid id;
            var  result = ItemPathResolver.TryResolvePath("/sitecore/", namesCache, out id);

            Assert.IsTrue(result);
            Assert.AreEqual(root.ID, id);
        }
示例#2
0
        public void TesT4()
        {
            var root = new ItemRecord {
                ID = ItemIDs.RootItemID, Name = "Sitecore"
            };
            var content = new ItemRecord {
                ID = Guid.NewGuid(), Name = "content", ParentID = root.ID
            };
            var namesCache = new ChildrenDataSet(new ItemDefinitions(root, content));

            // act
            Guid id;
            var  result = ItemPathResolver.TryResolvePath("/sitecore/system", namesCache, out id);

            Assert.IsFalse(result);
            Assert.AreEqual(root.ID, id);
        }
示例#3
0
        private static bool ResolvePath(ChildrenDataSet childrenDataSet, ref Guid id, string[] arr, string path, int shift)
        {
            if (shift >= arr.Length)
            {
                return(true);
            }

            var word = arr[shift];

            if (string.IsNullOrEmpty(word))
            {
                throw new ArgumentException($"itemPath must not contain double-slashes // (actual: {path})");
            }

            var children = childrenDataSet.TryGetValue(id);

            if (children == null)
            {
                return(false);
            }

            foreach (var child in children)
            {
                if ((child?.Name == null) || !child.Name.Equals(word, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var childId = child.ID;
                if (!ResolvePath(childrenDataSet, ref childId, arr, path, shift + 1))
                {
                    continue;
                }

                id = childId;
                return(true);
            }

            return(false);
        }
示例#4
0
        public void TesT5()
        {
            var root = new ItemRecord {
                ID = ItemIDs.RootItemID, Name = "Sitecore"
            };
            var content1 = new ItemRecord {
                ID = Guid.NewGuid(), Name = "content", ParentID = root.ID
            };
            var content2 = new ItemRecord {
                ID = Guid.NewGuid(), Name = "content", ParentID = root.ID
            };
            var home = new ItemRecord {
                ID = Guid.NewGuid(), Name = "Home", ParentID = content2.ID
            };
            var namesCache = new ChildrenDataSet(new ItemDefinitions(root, content1, content2, home));

            // act
            Guid id;
            var  result = ItemPathResolver.TryResolvePath("/sitecore/content/home", namesCache, out id);

            Assert.IsTrue(result);
            Assert.AreEqual(home.ID, id);
        }
示例#5
0
        public ItemsDataSet(Stream stream)
        {
            using (stream)
            {
                var data = Serializer.Deserialize <ItemsData>(stream);
                Assert.IsNotNull(data, nameof(data));

                var definitions = data.Definitions;
                Assert.IsNotNull(definitions, nameof(definitions));

                var sharedData = data.SharedData;
                Assert.IsNotNull(sharedData, nameof(sharedData));

                var languageData = data.LanguageData;
                Assert.IsNotNull(languageData, nameof(languageData));

                Definitions  = definitions;
                SharedData   = sharedData;
                LanguageData = languageData;
            }

            Children = new ChildrenDataSet(Definitions);
        }
示例#6
0
        /// <summary>
        ///   Try to find item ID by path
        /// </summary>
        /// <param name="itemPath"></param>
        /// <param name="childrenDataSet"></param>
        /// <param name="id">If item is successfully found equals to the item ID, otherwise the closest existing ancestor ID</param>
        /// <returns></returns>
        public static bool TryResolvePath(string itemPath, ChildrenDataSet childrenDataSet, out Guid id)
        {
            if (string.IsNullOrWhiteSpace(itemPath))
            {
                throw new ArgumentNullException(nameof(itemPath));
            }

            if (childrenDataSet == null)
            {
                throw new ArgumentNullException(nameof(childrenDataSet));
            }

            if (!itemPath.StartsWith("/sitecore", StringComparison.OrdinalIgnoreCase))
            {
                id = Guid.Empty;
                return(false);
            }

            itemPath = itemPath.TrimEnd('/');
            if (itemPath.Length == "/sitecore".Length)
            {
                id = ItemIDs.RootItemID;
                return(true); //childrenDataSet.ContainsKey(ItemIDs.RootItemID);
            }

            if (itemPath["/sitecore".Length] != '/')
            {
                throw new ArgumentException($"itemPath must start with /sitecore/ (actual: {itemPath})");
            }

            id = ItemIDs.RootItemID;
            var substring = itemPath.Substring("/sitecore/".Length);
            var arr       = substring.Split('/');

            return(ResolvePath(childrenDataSet, ref id, arr, itemPath, 0));
        }