Exemplo n.º 1
0
        public static XmlDocument Add([NotNull] this XmlDocument that, [NotNull] XmlNode parent, [NotNull] string xpath, string contents = null)
        {
            Assert.ArgumentNotNull(that, nameof(that));
            Assert.ArgumentNotNull(parent, nameof(parent));
            Assert.ArgumentNotNull(xpath, nameof(xpath));

            var nodeSegments = XPathHelper.GetNodeSegments(xpath);

            foreach (var nodeSegment in nodeSegments)
            {
                var node = that.CreateElement(nodeSegment.Name);
                foreach (var attribute in nodeSegment.Attributes)
                {
                    var attributeName = attribute.Key;
                    if (attributeName == null)
                    {
                        continue;
                    }

                    node.SetAttribute(attributeName, attribute.Value ?? string.Empty);
                }

                parent.AppendChild(node);
                parent = node;
            }

            if (contents != null)
            {
                parent.InnerXml = contents;
            }

            return(that);
        }
 private void GetNodeSegmentsTestSuccess(string xpath, string name)
 {
     {
         var res = XPathHelper.GetNodeSegments(xpath).ToArray();
         Assert.AreEqual(1, res.Length);
         var seg = res[0];
         Assert.IsNotNull(seg);
         Assert.AreEqual(name, seg.Name);
     }
 }
        private static void GetNodeSegmentsTestSuccessAttribute(string xpath, string name, string value)
        {
            var res = XPathHelper.GetNodeSegments(xpath).ToArray();

            Assert.AreEqual(1, res.Length);
            var seg = res[0];

            Assert.IsNotNull(seg);
            Assert.AreEqual("n.o-d_e", seg.Name);
            Assert.AreEqual(name, seg.Attributes.Single().Key);
            Assert.AreEqual(value, seg.Attributes.Single().Value);
        }
Exemplo n.º 4
0
        public static void Add([NotNull] this XmlDocument that, [NotNull] XmlNode parent, [NotNull] string xpath, [NotNull] NameValueCollection attributes, string contents = null)
        {
            Assert.ArgumentNotNull(that, nameof(that));
            Assert.ArgumentNotNull(parent, nameof(parent));
            Assert.ArgumentNotNull(xpath, nameof(xpath));
            Assert.ArgumentNotNull(attributes, nameof(attributes));

            foreach (var xpathSegment in XPathHelper.GetNodeSegments(xpath))
            {
                var element = that.CreateElement(xpathSegment.Name);

                foreach (var keyValuePair in xpathSegment.Attributes)
                {
                    var key = keyValuePair.Key;
                    if (key != null)
                    {
                        element.SetAttribute(key, keyValuePair.Value ?? string.Empty);
                    }
                }

                parent.AppendChild(element);
                parent = element;
            }

            if (contents != null)
            {
                parent.InnerXml = contents;
            }

            foreach (string key in attributes.Keys)
            {
                var att = that.CreateAttribute(key);
                att.Value = attributes[key];
                parent.Attributes.Append(att);
            }
        }
        public void GetNodeSegmentsTest()
        {
            GetNodeSegmentsTestSuccess("/n.o-d_e/", "n.o-d_e");
            new Action(() => XPathHelper.GetNodeSegments("//").ToArray().DoNothing())
            .ShouldThrow <NotSupportedException>()
            .WithMessage("Axes are not supported (//)");

            GetNodeSegmentsTestFail("/n.o-d_e]", "open bracket is missing");
            GetNodeSegmentsTestFail("/n.o-d_e'", "open bracket is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[", "attribute name is missing");
            GetNodeSegmentsTestFail("/n.o-d_e['", "attribute name is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a''", "equals sign is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a='", "close quote is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a='[", "close quote is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a='[", "close quote is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a=']", "close quote is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a=''", "close bracket or ' and ' is missing");
            GetNodeSegmentsTestFail("/n.o-d_e[a=''", "close bracket or ' and ' is missing");

            // 1
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/']", "a", "/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[']", "a", "[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']']", "a", "]");

            // 2-1
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='//']", "a", "//");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/[']", "a", "/[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/]']", "a", "/]");

            // 2-2
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[/']", "a", "[/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[[']", "a", "[[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[]']", "a", "[]");

            // 3-1-1
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='///']", "a", "///");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='//[']", "a", "//[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='//]']", "a", "//]");

            // 3-1-2
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/[/']", "a", "/[/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/[[']", "a", "/[[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/[]']", "a", "/[]");

            // 3-1-3
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/]/']", "a", "/]/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/][']", "a", "/][");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='/]]']", "a", "/]]");

            // 3-2-1
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[//']", "a", "[//");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[/[']", "a", "[/[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[/]']", "a", "[/]");

            // 3-2-2
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[[/']", "a", "[[/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[[[']", "a", "[[[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[[]']", "a", "[[]");

            // 3-2-3
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[]/']", "a", "[]/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[][']", "a", "[][");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='[]]']", "a", "[]]");

            // 3-3-1
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']//']", "a", "]//");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']/[']", "a", "]/[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']/]']", "a", "]/]");

            // 3-3-2
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='][/']", "a", "][/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='][[']", "a", "][[");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a='][]']", "a", "][]");

            // 3-3-3
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']]/']", "a", "]]/");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']][']", "a", "]][");
            GetNodeSegmentsTestSuccessAttribute("/n.o-d_e[a=']]]']", "a", "]]]");

            new Action(() => XPathHelper.GetNodeSegments("/node ]").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (open bracket is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/node][").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (open bracket is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/node[ /node[@a='b']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (attribute name is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/node ]/node[@a='b']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (open bracket is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/node][/node[@a='b']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (open bracket is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/ node [ a ] /node[@a=']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (equals sign is missing)");

            new Action(() => XPathHelper.GetNodeSegments("/ node [ a 'a' ] /node[@a=']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (equals sign is missing)");

            // here a attribute value is "]/node[@aa="
            new Action(() => XPathHelper.GetNodeSegments("/node/node[@a=']/node[@aa='b']").ToArray().DoNothing())
            .ShouldThrow <InvalidXPathException>()
            .WithMessage("Invalid xpath (close bracket or ' and ' is missing)");

            {
                var res = XPathHelper.GetNodeSegments("/hello[@html='<a href=\"ftp://*****:*****@sitecore.net/#abc\">[link]</a>' and  @empty='']").ToArray();
                Assert.AreEqual(1, res.Length);
                var segment = res[0];
                Assert.IsNotNull(segment);
                Assert.AreEqual("hello", segment.Name);
                var html = segment.Attributes.SingleOrDefault(x => x.Key == "html");
                Assert.IsNotNull(html);
                Assert.AreEqual("<a href=\"ftp://*****:*****@sitecore.net/#abc\">[link]</a>", html.Value);
                var empty = segment.Attributes.SingleOrDefault(x => x.Key == "empty");
                Assert.IsNotNull(empty);
                Assert.AreEqual(string.Empty, empty.Value);
            }

            new Action(() => XPathHelper.GetNodeSegments("/node//node").ToArray().DoNothing())
            .ShouldThrow <NotSupportedException>()
            .WithMessage("Axes are not supported (//)");
        }
 private static void GetNodeSegmentsTestFail(string xpath, string message)
 {
     new Action(() => XPathHelper.GetNodeSegments(xpath).ToArray().DoNothing()).ShouldThrow <InvalidXPathException>().WithMessage("Invalid xpath (" + message + ")");
 }