Esempio n. 1
0
        public void CanParseHalPath()
        {
            //Arrange

            //Act
            var halpath = new HalPath("/foo/item[2]/bar");
            var itemSegment = halpath.Segments.ElementAt(1);

            //Assert
            Assert.AreEqual(3, halpath.Segments.Count());
            Assert.AreEqual("item[2]", itemSegment.Key);
        }
Esempio n. 2
0
        /// <summary>relationPath specifies a unique path to a single link</summary>
        public static HalLink FindLink(this HalDocument document, string relationPath)
        {
            var halPath = new HalPath(relationPath);

            HalNode currentNode = document.Root;
            foreach (var segment in halPath.Segments) {
                if (currentNode is HalResource) {
                    currentNode = ((HalResource)currentNode).Contents[segment.Key];
                }
            }
            var halNode = currentNode;
            if (halNode is HalLink) return (HalLink)halNode;
            if (halNode is HalResource) {
                var halLink = ((HalResource)halNode).ResourceLink;
                //halLink.Name = halPath.Segments.Last().Name;
                return halLink;
            }

            return null;
        }
Esempio n. 3
0
        /// <summary>relationPath specifies a unique path to a single HAL resource, from which all links are found (searches deep).
        /// If relationPath is null, all links are returned</summary>
        public static IEnumerable<HalLink> FindAllLinks(this HalDocument document, string relationPath = null)
        {
            var resource = document.Root;
            if (relationPath != null) {
                var halPath = new HalPath(relationPath);

                foreach (var segment in halPath.Segments) {
                    var node = resource.Contents[segment.Key] as HalResource;
                    if (node == null) {
                        throw new Exception("Path must specify a resource");
                    }

                    resource = node;
                }
            }

            var links = new List<HalLink>();

            InternalFindAllLinks(resource, links);

            return links;
        }