16 package cz.vutbr.fit.knot.annotations.fragmentUpdater;
18 import javax.xml.xpath.*;
19 import org.w3c.dom.Attr;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
42 XPathFactory factory = XPathFactory.newInstance();
43 XPath xpath = factory.newXPath();
44 XPathExpression expr = (XPathExpression) xpath.compile(XPathString);
56 public static NodeList
evaluateXPath(Document document, XPathExpression expr)
throws XPathExpressionException {
60 result = expr.evaluate(document, XPathConstants.NODESET);
62 return (NodeList) result;
74 if (node != null && node.getOwnerDocument() != null) {
76 Node parentNode = node.getParentNode();
77 if (parentNode != null) {
78 Node prevSibling = node.getPreviousSibling();
80 if (prevSibling != null
81 && prevSibling.getNodeType() == Node.DOCUMENT_TYPE_NODE
82 && node.getNodeType() == Node.ELEMENT_NODE
83 && node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
92 short nodeType = node.getNodeType();
95 case Node.ATTRIBUTE_NODE:
98 case Node.CDATA_SECTION_NODE:
case Node.TEXT_NODE:
99 while (prevSibling != null) {
100 if (prevSibling.getNodeType() == Node.TEXT_NODE
101 || prevSibling.getNodeType() == Node.CDATA_SECTION_NODE) {
104 prevSibling = prevSibling.getPreviousSibling();
108 case Node.COMMENT_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
109 while (prevSibling != null) {
110 if (prevSibling.getNodeType() == nodeType) {
113 prevSibling = prevSibling.getPreviousSibling();
117 case Node.ELEMENT_NODE:
118 while (prevSibling != null) {
119 if (prevSibling.getNodeName().equals(node.getNodeName())) {
122 prevSibling = prevSibling.getPreviousSibling();
127 while (prevSibling != null) {
129 prevSibling = prevSibling.getPreviousSibling();
146 if (node.getNodeType() == Node.DOCUMENT_NODE) {
151 Node currentNode = node;
155 switch (node.getNodeType()) {
157 case Node.CDATA_SECTION_NODE:
158 str =
"/text()" +
"[" + currentIndex +
"]";
160 case Node.COMMENT_NODE:
161 str =
"/comment()" +
"[" + currentIndex +
"]";
163 case Node.PROCESSING_INSTRUCTION_NODE:
164 str =
"/processing-instruction()" +
"[" + currentIndex +
"]";
166 case Node.ATTRIBUTE_NODE:
167 str =
"/@" + ((Attr)node).getName();
169 case Node.ELEMENT_NODE:
172 str =
"/node()" +
"[" + currentIndex +
"]";
175 if (node.getNodeType() != Node.ELEMENT_NODE && currentNode != node.getOwnerDocument()) {
176 currentNode = node.getParentNode();
180 while (currentNode != null && currentNode != node.getOwnerDocument()) {
181 str =
"/" + currentNode.getNodeName() +
"[" + currentIndex +
"]" + str;
182 currentNode = currentNode.getParentNode();
197 public static Node
getNode(Document document, String path)
throws XPathExpressionException {
198 XPathFactory factory = XPathFactory.newInstance();
199 XPath xpath = factory.newXPath();
200 XPathExpression expr = xpath.compile(path);
201 NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
204 if (nodes.getLength() != 1) {
208 return nodes.item(0);
static String XPathStringOfNode(Node node)
static final String DOCUMENT_XPATH
static NodeList evaluateXPath(Document document, XPathExpression expr)
static Node getNode(Document document, String path)
static XPathExpression XPathStringToExpression(String XPathString)
static int XPathIndexOfNode(Node node)
Helper class with util XPath methods.