1 16 18 package com.sun.org.apache.xpath.internal.jaxp; 19 20 import com.sun.org.apache.xpath.internal.*; 21 import javax.xml.transform.TransformerException ; 22 23 import com.sun.org.apache.xpath.internal.objects.XObject; 24 import com.sun.org.apache.xml.internal.utils.PrefixResolver; 25 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 26 import com.sun.org.apache.xalan.internal.res.XSLMessages; 27 28 import javax.xml.namespace.NamespaceContext ; 29 import javax.xml.namespace.QName ; 30 import javax.xml.xpath.XPathExpressionException ; 31 import javax.xml.xpath.XPathConstants ; 32 import javax.xml.xpath.XPathFunctionResolver ; 33 import javax.xml.xpath.XPathVariableResolver ; 34 import javax.xml.xpath.XPathConstants ; 35 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.DOMImplementation ; 39 import org.w3c.dom.traversal.NodeIterator; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 import javax.xml.parsers.DocumentBuilder ; 42 43 import org.xml.sax.InputSource ; 44 45 51 public class XPathExpressionImpl implements javax.xml.xpath.XPathExpression { 52 53 private XPathFunctionResolver functionResolver; 54 private XPathVariableResolver variableResolver; 55 private JAXPPrefixResolver prefixResolver; 56 private com.sun.org.apache.xpath.internal.XPath xpath; 57 58 private boolean featureSecureProcessing = false; 62 63 66 protected XPathExpressionImpl() { }; 67 68 protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath, 69 JAXPPrefixResolver prefixResolver, 70 XPathFunctionResolver functionResolver, 71 XPathVariableResolver variableResolver ) { 72 this.xpath = xpath; 73 this.prefixResolver = prefixResolver; 74 this.functionResolver = functionResolver; 75 this.variableResolver = variableResolver; 76 this.featureSecureProcessing = false; 77 }; 78 79 protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath, 80 JAXPPrefixResolver prefixResolver, 81 XPathFunctionResolver functionResolver, 82 XPathVariableResolver variableResolver, 83 boolean featureSecureProcessing ) { 84 this.xpath = xpath; 85 this.prefixResolver = prefixResolver; 86 this.functionResolver = functionResolver; 87 this.variableResolver = variableResolver; 88 this.featureSecureProcessing = featureSecureProcessing; 89 }; 90 91 public void setXPath (com.sun.org.apache.xpath.internal.XPath xpath ) { 92 this.xpath = xpath; 93 } 94 95 public Object eval(Object item, QName returnType) 96 throws javax.xml.transform.TransformerException { 97 XObject resultObject = eval ( item ); 98 return getResultAsType( resultObject, returnType ); 99 } 100 101 private XObject eval ( Object contextItem ) 102 throws javax.xml.transform.TransformerException { 103 com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null; 104 if ( functionResolver != null ) { 105 JAXPExtensionsProvider jep = new JAXPExtensionsProvider( 106 functionResolver, featureSecureProcessing ); 107 xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep ); 108 } else { 109 xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(); 110 } 111 112 xpathSupport.setVarStack(new JAXPVariableStack(variableResolver)); 113 XObject xobj = null; 114 115 Node contextNode = (Node )contextItem; 116 if ( contextNode == null ) { 120 contextNode = getDummyDocument(); 121 } 122 123 xobj = xpath.execute(xpathSupport, contextNode, prefixResolver ); 124 return xobj; 125 } 126 127 128 159 public Object evaluate(Object item, QName returnType) 160 throws XPathExpressionException { 161 if ( returnType == null ) { 163 String fmsg = XSLMessages.createXPATHMessage( 165 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 166 new Object [] {"returnType"} ); 167 throw new NullPointerException ( fmsg ); 168 } 169 if ( !isSupported ( returnType ) ) { 172 String fmsg = XSLMessages.createXPATHMessage( 173 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 174 new Object [] { returnType.toString() } ); 175 throw new IllegalArgumentException ( fmsg ); 176 } 177 try { 178 return eval( item, returnType); 179 } catch ( java.lang.NullPointerException npe ) { 180 throw new XPathExpressionException ( npe ); 184 } catch ( javax.xml.transform.TransformerException te ) { 185 Throwable nestedException = te.getException(); 186 if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) { 187 throw (javax.xml.xpath.XPathFunctionException )nestedException; 188 } else { 189 throw new XPathExpressionException ( te); 192 } 193 } 194 195 } 196 197 221 public String evaluate(Object item) 222 throws XPathExpressionException { 223 return (String )this.evaluate( item, XPathConstants.STRING ); 224 } 225 226 227 228 static DocumentBuilderFactory dbf = null; 229 static DocumentBuilder db = null; 230 static Document d = null; 231 232 266 public Object evaluate(InputSource source, QName returnType) 267 throws XPathExpressionException { 268 if ( ( source == null ) || ( returnType == null ) ) { 269 String fmsg = XSLMessages.createXPATHMessage( 270 XPATHErrorResources.ER_SOURCE_RETURN_TYPE_CANNOT_BE_NULL, 271 null ); 272 throw new NullPointerException ( fmsg ); 273 } 274 if ( !isSupported ( returnType ) ) { 277 String fmsg = XSLMessages.createXPATHMessage( 278 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 279 new Object [] { returnType.toString() } ); 280 throw new IllegalArgumentException ( fmsg ); 281 } 282 try { 283 if ( dbf == null ) { 284 dbf = DocumentBuilderFactory.newInstance(); 285 dbf.setNamespaceAware( true ); 286 dbf.setValidating( false ); 287 } 288 db = dbf.newDocumentBuilder(); 289 Document document = db.parse( source ); 290 return eval( document, returnType ); 291 } catch ( Exception e ) { 292 throw new XPathExpressionException ( e ); 293 } 294 } 295 296 317 public String evaluate(InputSource source) 318 throws XPathExpressionException { 319 return (String )this.evaluate( source, XPathConstants.STRING ); 320 } 321 322 private boolean isSupported( QName returnType ) { 323 if ( ( returnType.equals( XPathConstants.STRING ) ) || 325 ( returnType.equals( XPathConstants.NUMBER ) ) || 326 ( returnType.equals( XPathConstants.BOOLEAN ) ) || 327 ( returnType.equals( XPathConstants.NODE ) ) || 328 ( returnType.equals( XPathConstants.NODESET ) ) ) { 329 330 return true; 331 } 332 return false; 333 } 334 335 private Object getResultAsType( XObject resultObject, QName returnType ) 336 throws javax.xml.transform.TransformerException { 337 if ( returnType.equals( XPathConstants.STRING ) ) { 339 return resultObject.str(); 340 } 341 if ( returnType.equals( XPathConstants.NUMBER ) ) { 343 return new Double ( resultObject.num()); 344 } 345 if ( returnType.equals( XPathConstants.BOOLEAN ) ) { 347 return new Boolean ( resultObject.bool()); 348 } 349 if ( returnType.equals( XPathConstants.NODESET ) ) { 351 return resultObject.nodelist(); 352 } 353 if ( returnType.equals( XPathConstants.NODE ) ) { 355 NodeIterator ni = resultObject.nodeset(); 356 return ni.nextNode(); 358 } 359 String fmsg = XSLMessages.createXPATHMessage( 362 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 363 new Object [] { returnType.toString()}); 364 throw new IllegalArgumentException ( fmsg ); 365 } 366 367 368 private static Document getDummyDocument( ) { 369 try { 370 if ( dbf == null ) { 371 dbf = DocumentBuilderFactory.newInstance(); 372 dbf.setNamespaceAware( true ); 373 dbf.setValidating( false ); 374 } 375 db = dbf.newDocumentBuilder(); 376 377 DOMImplementation dim = db.getDOMImplementation(); 378 d = dim.createDocument("http://java.sun.com/jaxp/xpath", 379 "dummyroot", null); 380 return d; 381 } catch ( Exception e ) { 382 e.printStackTrace(); 383 } 384 return null; 385 } 386 387 388 389 390 } 391 | Popular Tags |