| 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 | |
package org.xembly; |
| 31 | |
|
| 32 | |
import java.util.Collection; |
| 33 | |
import java.util.Collections; |
| 34 | |
import java.util.HashSet; |
| 35 | |
import java.util.regex.Matcher; |
| 36 | |
import java.util.regex.Pattern; |
| 37 | |
import javax.xml.xpath.XPath; |
| 38 | |
import javax.xml.xpath.XPathConstants; |
| 39 | |
import javax.xml.xpath.XPathExpressionException; |
| 40 | |
import javax.xml.xpath.XPathFactory; |
| 41 | |
import lombok.EqualsAndHashCode; |
| 42 | |
import org.w3c.dom.Document; |
| 43 | |
import org.w3c.dom.Node; |
| 44 | |
import org.w3c.dom.NodeList; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 0 | @EqualsAndHashCode(of = "expr") |
| 56 | |
final class XpathDirective implements Directive { |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 1 | private static final ThreadLocal<XPathFactory> FACTORY = |
| 62 | 8 | new ThreadLocal<XPathFactory>() { |
| 63 | |
@Override |
| 64 | |
protected XPathFactory initialValue() { |
| 65 | 7 | return XPathFactory.newInstance(); |
| 66 | |
} |
| 67 | |
}; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 2 | private static final Pattern ROOT_ONLY = |
| 73 | 1 | Pattern.compile("/([^/\\(\\[\\{:]+)"); |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private final transient Arg expr; |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 25 | XpathDirective(final String path) throws XmlContentException { |
| 86 | 25 | this.expr = new Arg(path); |
| 87 | 25 | } |
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public String toString() { |
| 91 | 2 | return String.format("XPATH %s", this.expr); |
| 92 | |
} |
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public Directive.Cursor exec(final Node dom, |
| 96 | |
final Directive.Cursor cursor, final Directive.Stack stack) |
| 97 | |
throws ImpossibleModificationException { |
| 98 | |
final Collection<Node> targets; |
| 99 | 20 | final String query = this.expr.raw(); |
| 100 | 20 | final Matcher matcher = XpathDirective.ROOT_ONLY.matcher(query); |
| 101 | 20 | if (matcher.matches()) { |
| 102 | 11 | targets = XpathDirective.rootOnly(matcher.group(1), dom); |
| 103 | |
} else { |
| 104 | 9 | targets = XpathDirective.traditional(query, dom, cursor); |
| 105 | |
} |
| 106 | 20 | return new DomCursor(targets); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
private static Collection<Node> rootOnly(final String root, |
| 116 | |
final Node dom) { |
| 117 | |
final Node target; |
| 118 | 11 | if (dom.getOwnerDocument() == null) { |
| 119 | 11 | target = Document.class.cast(dom).getDocumentElement(); |
| 120 | |
} else { |
| 121 | 0 | target = dom.getOwnerDocument().getDocumentElement(); |
| 122 | |
} |
| 123 | |
final Collection<Node> targets; |
| 124 | 11 | if (root != null && target != null |
| 125 | 10 | && ("*".equals(root) || target.getNodeName().equals(root))) { |
| 126 | 9 | targets = Collections.singletonList(target); |
| 127 | |
} else { |
| 128 | 2 | targets = Collections.emptyList(); |
| 129 | |
} |
| 130 | 11 | return targets; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
private static Collection<Node> traditional(final String query, |
| 142 | |
final Node dom, final Collection<Node> current) |
| 143 | |
throws ImpossibleModificationException { |
| 144 | 9 | final XPath xpath = XpathDirective.FACTORY.get().newXPath(); |
| 145 | 9 | final Collection<Node> targets = new HashSet<Node>(0); |
| 146 | 9 | for (final Node node : XpathDirective.roots(dom, current)) { |
| 147 | |
final NodeList list; |
| 148 | |
try { |
| 149 | 18 | list = NodeList.class.cast( |
| 150 | 9 | xpath.evaluate(query, node, XPathConstants.NODESET) |
| 151 | |
); |
| 152 | 0 | } catch (final XPathExpressionException ex) { |
| 153 | 0 | throw new ImpossibleModificationException( |
| 154 | 0 | String.format("invalid XPath expr '%s'", query), ex |
| 155 | |
); |
| 156 | 9 | } |
| 157 | 9 | final int len = list.getLength(); |
| 158 | 24 | for (int idx = 0; idx < len; ++idx) { |
| 159 | 15 | targets.add(list.item(idx)); |
| 160 | |
} |
| 161 | 9 | } |
| 162 | 9 | return targets; |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
private static Iterable<Node> roots(final Node dom, |
| 172 | |
final Collection<Node> nodes) { |
| 173 | |
final Collection<Node> roots; |
| 174 | 9 | if (nodes.isEmpty()) { |
| 175 | 0 | if (dom.getOwnerDocument() == null) { |
| 176 | 0 | roots = Collections.singletonList(dom); |
| 177 | |
} else { |
| 178 | 0 | roots = Collections.<Node>singletonList( |
| 179 | 0 | dom.getOwnerDocument().getDocumentElement() |
| 180 | |
); |
| 181 | |
} |
| 182 | |
} else { |
| 183 | 9 | roots = nodes; |
| 184 | |
} |
| 185 | 9 | return roots; |
| 186 | |
} |
| 187 | |
|
| 188 | |
} |