1 16 19 package org.apache.xpath; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xml.utils.PrefixResolver; 24 import org.apache.xml.utils.PrefixResolverDefault; 25 import org.apache.xpath.objects.XObject; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.traversal.NodeIterator; 31 32 50 public class XPathAPI 51 { 52 53 64 public static Node selectSingleNode(Node contextNode, String str) 65 throws TransformerException 66 { 67 return selectSingleNode(contextNode, str, contextNode); 68 } 69 70 81 public static Node selectSingleNode( 82 Node contextNode, String str, Node namespaceNode) 83 throws TransformerException 84 { 85 86 NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode); 88 89 return nl.nextNode(); 91 } 92 93 103 public static NodeIterator selectNodeIterator(Node contextNode, String str) 104 throws TransformerException 105 { 106 return selectNodeIterator(contextNode, str, contextNode); 107 } 108 109 120 public static NodeIterator selectNodeIterator( 121 Node contextNode, String str, Node namespaceNode) 122 throws TransformerException 123 { 124 125 XObject list = eval(contextNode, str, namespaceNode); 127 128 return list.nodeset(); 130 } 131 132 142 public static NodeList selectNodeList(Node contextNode, String str) 143 throws TransformerException 144 { 145 return selectNodeList(contextNode, str, contextNode); 146 } 147 148 159 public static NodeList selectNodeList( 160 Node contextNode, String str, Node namespaceNode) 161 throws TransformerException 162 { 163 164 XObject list = eval(contextNode, str, namespaceNode); 166 167 return list.nodelist(); 169 } 170 171 187 public static XObject eval(Node contextNode, String str) 188 throws TransformerException 189 { 190 return eval(contextNode, str, contextNode); 191 } 192 193 213 public static XObject eval(Node contextNode, String str, Node namespaceNode) 214 throws TransformerException 215 { 216 217 XPathContext xpathSupport = new XPathContext(); 223 224 PrefixResolverDefault prefixResolver = new PrefixResolverDefault( 229 (namespaceNode.getNodeType() == Node.DOCUMENT_NODE) 230 ? ((Document ) namespaceNode).getDocumentElement() : namespaceNode); 231 232 XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null); 234 235 int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); 238 239 return xpath.execute(xpathSupport, ctxtNode, prefixResolver); 240 } 241 242 264 public static XObject eval( 265 Node contextNode, String str, PrefixResolver prefixResolver) 266 throws TransformerException 267 { 268 269 XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null); 276 277 XPathContext xpathSupport = new XPathContext(); 279 int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); 280 281 return xpath.execute(xpathSupport, ctxtNode, prefixResolver); 282 } 283 } 284 | Popular Tags |