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

xmlunit / xmlunit / 667

pending completion
667

push

travis-ci-com

bodewig
add Cyclone DX SBOM generation to build

5824 of 6326 relevant lines covered (92.06%)

3.68 hits per line

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

72.73
/xmlunit-legacy/src/main/java/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java
1
/*
2
******************************************************************
3
Copyright (c) 2006-2007,2015 Jeff Martin, Tim Bacon
4
All rights reserved.
5

6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
8
are met:
9

10
    * Redistributions of source code must retain the above copyright
11
      notice, this list of conditions and the following disclaimer.
12
    * Redistributions in binary form must reproduce the above
13
      copyright notice, this list of conditions and the following
14
      disclaimer in the documentation and/or other materials provided
15
      with the distribution.
16
    * Neither the name of the XMLUnit nor the names
17
      of its contributors may be used to endorse or promote products
18
      derived from this software without specific prior written
19
      permission.
20

21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33

34
******************************************************************
35
*/
36

37
package org.custommonkey.xmlunit.jaxp13;
38

39
import org.custommonkey.xmlunit.NamespaceContext;
40
import org.custommonkey.xmlunit.XMLUnit;
41
import org.custommonkey.xmlunit.XpathEngine;
42
import org.custommonkey.xmlunit.exceptions.ConfigurationException;
43
import org.custommonkey.xmlunit.exceptions.XpathException;
44

45
import java.util.ArrayList;
46
import java.util.Collections;
47
import java.util.List;
48
import javax.xml.transform.dom.DOMSource;
49
import javax.xml.xpath.XPathFactory;
50

51
import org.w3c.dom.Document;
52
import org.w3c.dom.Node;
53
import org.w3c.dom.NodeList;
54

55
import org.xmlunit.XMLUnitException;
56
import org.xmlunit.xpath.JAXPXPathEngine;
57

58
/**
59
 * XPath engine based on javax.xml.xpath.
60
 */
61
public class Jaxp13XpathEngine implements XpathEngine {
62

63
    private final JAXPXPathEngine engine;
64

65
    public Jaxp13XpathEngine() throws ConfigurationException {
4✔
66
        try {
67
            JAXPXPathEngine e = null;
4✔
68
            if (XMLUnit.getXPathFactory() != null) {
4✔
69
                e = new JAXPXPathEngine((XPathFactory) Class
×
70
                                        .forName(XMLUnit.getXPathFactory())
×
71
                                        .newInstance());
×
72
            } else {
73
                e = new JAXPXPathEngine();
4✔
74
            }
75
            engine = e;
4✔
76
        } catch (org.xmlunit.ConfigurationException ex) {
×
77
            throw new ConfigurationException(ex.getCause());
×
78
        } catch (Exception ex) {
×
79
            throw new ConfigurationException(ex);
×
80
        }
4✔
81
    }
4✔
82

83
    /**
84
     * Execute the specified xpath syntax <code>select</code> expression
85
     * on the specified document and return the list of nodes (could have
86
     * length zero) that match
87
     * @param select
88
     * @param document
89
     * @return list of matching nodes
90
     */
91
    public NodeList getMatchingNodes(String select, Document document)
92
        throws XpathException {
93
        try {
94
            return new NodeListForIterable(engine
4✔
95
                                           .selectNodes(select,
4✔
96
                                                        new DOMSource(document))
97
                                           );
98
        } catch (XMLUnitException ex) {
×
99
            throw new XpathException(ex.getCause());
×
100
        }
101
    }
102
    
103
    /**
104
     * Evaluate the result of executing the specified xpath syntax
105
     * <code>select</code> expression on the specified document
106
     * @param select
107
     * @param document
108
     * @return evaluated result
109
     * @throws XpathException
110
     */
111
    public String evaluate(String select, Document document)
112
        throws XpathException {
113
        try {
114
            return engine.evaluate(select, new DOMSource(document));
4✔
115
        } catch (XMLUnitException ex) {
4✔
116
            throw new XpathException(ex.getCause());
4✔
117
        }
118
    }
119

120
    public void setNamespaceContext(NamespaceContext ctx) {
121
        engine.setNamespaceContext(XMLUnitNamespaceContext2Jaxp13
4✔
122
                                   .turnIntoMap(ctx));
4✔
123
    }
4✔
124

125
    private static class NodeListForIterable implements NodeList {
126
        private final List<Node> l;
127

128
        private NodeListForIterable(Iterable<Node> it) {
4✔
129
            List<Node> a = new ArrayList<Node>();
4✔
130
            for (Node n : it) {
4✔
131
                a.add(n);
4✔
132
            }
4✔
133
            l = Collections.unmodifiableList(a);
4✔
134
        }
4✔
135

136
        public int getLength() {
137
            return l.size();
4✔
138
        }
139

140
        public Node item(int idx) {
141
            return l.get(idx);
4✔
142
        }
143
    }
144
}
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