-
Notifications
You must be signed in to change notification settings - Fork 27
/
XpathDirective.java
188 lines (176 loc) · 6.24 KB
/
XpathDirective.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright (c) 2013-2017, xembly.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the xembly.org nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.xembly;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import lombok.EqualsAndHashCode;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* XPATH directive.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
*/
@EqualsAndHashCode(of = "expr")
final class XpathDirective implements Directive {
/**
* XPath factory.
*/
private static final ThreadLocal<XPathFactory> FACTORY =
new ThreadLocal<XPathFactory>() {
@Override
protected XPathFactory initialValue() {
return XPathFactory.newInstance();
}
};
/**
* Pattern to match root-only XPath queries.
*/
private static final Pattern ROOT_ONLY =
Pattern.compile("/([^/\\(\\[\\{:]+)");
/**
* XPath to use.
*/
private final transient Arg expr;
/**
* Public ctor.
* @param path XPath
* @throws XmlContentException If invalid input
*/
XpathDirective(final String path) throws XmlContentException {
this.expr = new Arg(path);
}
@Override
public String toString() {
return String.format("XPATH %s", this.expr);
}
@Override
public Directive.Cursor exec(final Node dom,
final Directive.Cursor cursor, final Directive.Stack stack)
throws ImpossibleModificationException {
final Collection<Node> targets;
final String query = this.expr.raw();
final Matcher matcher = XpathDirective.ROOT_ONLY.matcher(query);
if (matcher.matches()) {
targets = XpathDirective.rootOnly(matcher.group(1), dom);
} else {
targets = XpathDirective.traditional(query, dom, cursor);
}
return new DomCursor(targets);
}
/**
* Fetches only root node.
* @param root Root node name
* @param dom Document
* @return Found nodes
*/
private static Collection<Node> rootOnly(final String root,
final Node dom) {
final Node target;
if (dom.getOwnerDocument() == null) {
target = Document.class.cast(dom).getDocumentElement();
} else {
target = dom.getOwnerDocument().getDocumentElement();
}
final Collection<Node> targets;
if (root != null && target != null
&& ("*".equals(root) || target.getNodeName().equals(root))) {
targets = Collections.singletonList(target);
} else {
targets = Collections.emptyList();
}
return targets;
}
/**
* Fetch them in traditional way.
* @param query XPath query
* @param dom Document
* @param current Nodes we're currently at
* @return Found nodes
* @throws ImpossibleModificationException If fails
*/
private static Collection<Node> traditional(final String query,
final Node dom, final Collection<Node> current)
throws ImpossibleModificationException {
final XPath xpath = XpathDirective.FACTORY.get().newXPath();
final Collection<Node> targets = new HashSet<Node>(0);
for (final Node node : XpathDirective.roots(dom, current)) {
final NodeList list;
try {
list = NodeList.class.cast(
xpath.evaluate(query, node, XPathConstants.NODESET)
);
} catch (final XPathExpressionException ex) {
throw new ImpossibleModificationException(
String.format("invalid XPath expr '%s'", query), ex
);
}
final int len = list.getLength();
for (int idx = 0; idx < len; ++idx) {
targets.add(list.item(idx));
}
}
return targets;
}
/**
* Get roots to start searching from.
* @param dom Document
* @param nodes Current nodes
* @return Root nodes to start searching from
*/
private static Iterable<Node> roots(final Node dom,
final Collection<Node> nodes) {
final Collection<Node> roots;
if (nodes.isEmpty()) {
if (dom.getOwnerDocument() == null) {
roots = Collections.singletonList(dom);
} else {
roots = Collections.<Node>singletonList(
dom.getOwnerDocument().getDocumentElement()
);
}
} else {
roots = nodes;
}
return roots;
}
}