• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xmlunit / xmlunit / cd160610-9b67-4752-a2c4-e3a0d82d991e

21 Apr 2025 11:55AM UTC coverage: 91.756% (-0.02%) from 91.78%
cd160610-9b67-4752-a2c4-e3a0d82d991e

push

circleci

web-flow
Merge pull request #289 from xmlunit/circleci-project-setup

CircleCI project setup

3996 of 4698 branches covered (85.06%)

11754 of 12810 relevant lines covered (91.76%)

2.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/xmlunit-core/src/main/java/org/xmlunit/diff/RecursiveXPathBuilder.java
1
/*
2
  This file is licensed to You under the Apache License, Version 2.0
3
  (the "License"); you may not use this file except in compliance with
4
  the License.  You may obtain a copy of the License at
5

6
  http://www.apache.org/licenses/LICENSE-2.0
7

8
  Unless required by applicable law or agreed to in writing, software
9
  distributed under the License is distributed on an "AS IS" BASIS,
10
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
  See the License for the specific language governing permissions and
12
  limitations under the License.
13
*/
14
package org.xmlunit.diff;
15

16
import java.util.Collections;
17
import java.util.Map;
18
import javax.xml.namespace.QName;
19
import org.w3c.dom.Attr;
20
import org.w3c.dom.Document;
21
import org.w3c.dom.Node;
22

23
import org.xmlunit.util.IterableNodeList;
24
import org.xmlunit.util.Linqy;
25
import org.xmlunit.util.Mapper;
26
import org.xmlunit.util.Nodes;
27

28
/**
29
 * Finds the XPathContext of a Node by recursively building up the XPathContext.
30
 */
31
public class RecursiveXPathBuilder implements Mapper<Node, XPathContext> {
1✔
32

33
    private Map<String, String> prefix2uri;
34

35
    /**
36
     * Establish a namespace context that will be used in for the
37
     * XPath.
38
     *
39
     * <p>Without a namespace context (or with an empty context) the
40
     * XPath expressions will only use local names for elements and
41
     * attributes.</p>
42
     *
43
     * @param prefix2uri maps from prefix to namespace URI.
44
     */
45
    public void setNamespaceContext(Map<String, String> prefix2uri) {
46
        this.prefix2uri = prefix2uri == null
1!
47
            ? Collections.<String, String> emptyMap()
×
48
            : Collections.unmodifiableMap(prefix2uri);
1✔
49
    }
1✔
50

51
    @Override
52
    public XPathContext apply(Node n) {
53
        if (n instanceof Attr) {
1✔
54
            return getXPathForAttribute((Attr) n);
1✔
55
        } else {
56
            return getXPathForNonAttribute(n);
1✔
57
        }
58
    }
59

60
    private XPathContext getXPathForNonAttribute(Node n) {
61
        Node parent = n.getParentNode();
1✔
62
        if (parent == null || parent instanceof Document) {
1✔
63
            return new XPathContext(prefix2uri, n);
1✔
64
        }
65
        XPathContext parentContext = getXPathForNonAttribute(parent);
1✔
66
        IterableNodeList nl = new IterableNodeList(parent.getChildNodes());
1✔
67
        parentContext.setChildren(Linqy.map(nl, ElementSelectors.TO_NODE_INFO));
1✔
68
        ChildNodeXPathContextProvider cn = new ChildNodeXPathContextProvider(parentContext,
1✔
69
                                                                             nl);
70
        return cn.apply(n);
1✔
71
    }
72

73
    private XPathContext getXPathForAttribute(Attr a) {
74
        XPathContext elementContext = getXPathForNonAttribute(a.getOwnerElement());
1✔
75
        QName q = Nodes.getQName(a);
1✔
76
        elementContext.addAttribute(q);
1✔
77
        elementContext.navigateToAttribute(q);
1✔
78
        return elementContext;
1✔
79
    }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc