1 2 18 package com.sun.org.apache.xml.internal.security.utils; 19 20 21 22 import javax.xml.transform.TransformerException ; 23 24 import com.sun.org.apache.xml.internal.security.transforms.implementations.FuncHereContext; 25 import com.sun.org.apache.xml.internal.utils.PrefixResolver; 26 import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault; 27 import com.sun.org.apache.xpath.internal.XPath; 28 import com.sun.org.apache.xpath.internal.objects.XObject; 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.ProcessingInstruction ; 34 import org.w3c.dom.Text ; 35 import org.w3c.dom.traversal.NodeIterator; 36 37 38 39 40 55 public class XPathFuncHereAPI { 56 57 68 public static Node selectSingleNode(Node contextNode, Node xpathnode) 69 throws TransformerException { 70 return selectSingleNode(contextNode, xpathnode, contextNode); 71 } 72 73 84 public static Node selectSingleNode( 85 Node contextNode, Node xpathnode, Node namespaceNode) 86 throws TransformerException { 87 88 NodeIterator nl = selectNodeIterator(contextNode, xpathnode, 90 namespaceNode); 91 92 return nl.nextNode(); 94 } 95 96 106 public static NodeIterator selectNodeIterator( 107 Node contextNode, Node xpathnode) throws TransformerException { 108 return selectNodeIterator(contextNode, xpathnode, contextNode); 109 } 110 111 122 public static NodeIterator selectNodeIterator( 123 Node contextNode, Node xpathnode, Node namespaceNode) 124 throws TransformerException { 125 126 XObject list = eval(contextNode, xpathnode, namespaceNode); 128 129 return list.nodeset(); 131 } 132 133 143 public static NodeList selectNodeList(Node contextNode, Node xpathnode) 144 throws TransformerException { 145 return selectNodeList(contextNode, xpathnode, contextNode); 146 } 147 148 159 public static NodeList selectNodeList( 160 Node contextNode, Node xpathnode, Node namespaceNode) 161 throws TransformerException { 162 163 XObject list = eval(contextNode, xpathnode, namespaceNode); 165 166 return list.nodelist(); 168 } 169 170 185 public static XObject eval(Node contextNode, Node xpathnode) 186 throws TransformerException { 187 return eval(contextNode, xpathnode, contextNode); 188 } 189 190 210 public static XObject eval( 211 Node contextNode, Node xpathnode, Node namespaceNode) 212 throws TransformerException { 213 214 FuncHereContext xpathSupport = new FuncHereContext(xpathnode); 220 221 PrefixResolverDefault prefixResolver = 226 new PrefixResolverDefault((namespaceNode.getNodeType() 227 == Node.DOCUMENT_NODE) 228 ? ((Document ) namespaceNode) 229 .getDocumentElement() 230 : namespaceNode); 231 String str = getStrFromNode(xpathnode); 232 233 XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null); 235 236 int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); 239 240 return xpath.execute(xpathSupport, ctxtNode, prefixResolver); 241 } 242 243 264 public static XObject eval( 265 Node contextNode, Node xpathnode, PrefixResolver prefixResolver) 266 throws TransformerException { 267 268 String str = getStrFromNode(xpathnode); 269 270 XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null); 277 278 FuncHereContext xpathSupport = new FuncHereContext(xpathnode); 280 int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode); 281 282 return xpath.execute(xpathSupport, ctxtNode, prefixResolver); 283 } 284 285 291 private static String getStrFromNode(Node xpathnode) { 292 293 if (xpathnode.getNodeType() == Node.TEXT_NODE) { 294 return ((Text ) xpathnode).getData(); 295 } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { 296 return ((Attr ) xpathnode).getNodeValue(); 297 } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { 298 return ((ProcessingInstruction ) xpathnode).getNodeValue(); 299 } 300 301 return ""; 302 } 303 } 304 | Popular Tags |