1 16 18 package com.sun.org.apache.xpath.internal.jaxp; 19 20 import javax.xml.namespace.QName ; 21 import javax.xml.namespace.NamespaceContext ; 22 import javax.xml.xpath.XPathExpressionException ; 23 import javax.xml.xpath.XPathConstants ; 24 import javax.xml.xpath.XPathFunctionResolver ; 25 import javax.xml.xpath.XPathVariableResolver ; 26 import javax.xml.xpath.XPathExpression ; 27 28 import com.sun.org.apache.xml.internal.dtm.DTM; 29 import com.sun.org.apache.xpath.internal.*; 30 import com.sun.org.apache.xpath.internal.objects.XObject; 31 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 32 import com.sun.org.apache.xalan.internal.res.XSLMessages; 33 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.DOMImplementation ; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.traversal.NodeIterator; 38 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 import javax.xml.parsers.*; 43 44 import java.io.IOException ; 45 46 55 public class XPathImpl implements javax.xml.xpath.XPath { 56 57 private XPathVariableResolver variableResolver; 59 private XPathFunctionResolver functionResolver; 60 private XPathVariableResolver origVariableResolver; 61 private XPathFunctionResolver origFunctionResolver; 62 private NamespaceContext namespaceContext=null; 63 private JAXPPrefixResolver prefixResolver; 64 private boolean featureSecureProcessing = false; 68 69 XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr ) { 70 this.origVariableResolver = this.variableResolver = vr; 71 this.origFunctionResolver = this.functionResolver = fr; 72 } 73 74 XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr, 75 boolean featureSecureProcessing ) { 76 this.origVariableResolver = this.variableResolver = vr; 77 this.origFunctionResolver = this.functionResolver = fr; 78 this.featureSecureProcessing = featureSecureProcessing; 79 } 80 81 86 public void setXPathVariableResolver(XPathVariableResolver resolver) { 87 if ( resolver == null ) { 88 String fmsg = XSLMessages.createXPATHMessage( 89 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 90 new Object [] {"XPathVariableResolver"} ); 91 throw new NullPointerException ( fmsg ); 92 } 93 this.variableResolver = resolver; 94 } 95 96 101 public XPathVariableResolver getXPathVariableResolver() { 102 return variableResolver; 103 } 104 105 110 public void setXPathFunctionResolver(XPathFunctionResolver resolver) { 111 if ( resolver == null ) { 112 String fmsg = XSLMessages.createXPATHMessage( 113 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 114 new Object [] {"XPathFunctionResolver"} ); 115 throw new NullPointerException ( fmsg ); 116 } 117 this.functionResolver = resolver; 118 } 119 120 125 public XPathFunctionResolver getXPathFunctionResolver() { 126 return functionResolver; 127 } 128 129 134 public void setNamespaceContext(NamespaceContext nsContext) { 135 if ( nsContext == null ) { 136 String fmsg = XSLMessages.createXPATHMessage( 137 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 138 new Object [] {"NamespaceContext"} ); 139 throw new NullPointerException ( fmsg ); 140 } 141 this.namespaceContext = nsContext; 142 this.prefixResolver = new JAXPPrefixResolver ( nsContext ); 143 } 144 145 150 public NamespaceContext getNamespaceContext() { 151 return namespaceContext; 152 } 153 154 private static Document d = null; 155 156 private static DocumentBuilder getParser() { 157 try { 158 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 170 dbf.setNamespaceAware( true ); 171 dbf.setValidating( false ); 172 return dbf.newDocumentBuilder(); 173 } catch (ParserConfigurationException e) { 174 throw new Error (e); 176 } 177 } 178 179 private static Document getDummyDocument( ) { 180 if(d==null) { 183 DOMImplementation dim = getParser().getDOMImplementation(); 184 d = dim.createDocument("http://java.sun.com/jaxp/xpath", 185 "dummyroot", null); 186 } 187 return d; 188 } 189 190 191 private XObject eval(String expression, Object contextItem) 192 throws javax.xml.transform.TransformerException { 193 com.sun.org.apache.xpath.internal.XPath xpath = new com.sun.org.apache.xpath.internal.XPath( expression, 194 null, prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT ); 195 com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null; 196 if ( functionResolver != null ) { 197 JAXPExtensionsProvider jep = new JAXPExtensionsProvider( 198 functionResolver, featureSecureProcessing ); 199 xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep ); 200 } else { 201 xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(); 202 } 203 204 XObject xobj = null; 205 206 xpathSupport.setVarStack(new JAXPVariableStack(variableResolver)); 207 208 if ( contextItem instanceof Node ) { 210 xobj = xpath.execute (xpathSupport, (Node )contextItem, 211 prefixResolver ); 212 } else { 213 xobj = xpath.execute ( xpathSupport, DTM.NULL, prefixResolver ); 214 } 215 216 return xobj; 217 } 218 219 250 public Object evaluate(String expression, Object item, QName returnType) 251 throws XPathExpressionException { 252 if ( expression == null ) { 253 String fmsg = XSLMessages.createXPATHMessage( 254 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 255 new Object [] {"XPath expression"} ); 256 throw new NullPointerException ( fmsg ); 257 } 258 if ( returnType == null ) { 259 String fmsg = XSLMessages.createXPATHMessage( 260 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 261 new Object [] {"returnType"} ); 262 throw new NullPointerException ( fmsg ); 263 } 264 if ( !isSupported ( returnType ) ) { 267 String fmsg = XSLMessages.createXPATHMessage( 268 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 269 new Object [] { returnType.toString() } ); 270 throw new IllegalArgumentException ( fmsg ); 271 } 272 273 try { 274 275 XObject resultObject = eval( expression, item ); 276 return getResultAsType( resultObject, returnType ); 277 } catch ( java.lang.NullPointerException npe ) { 278 throw new XPathExpressionException ( npe ); 282 } catch ( javax.xml.transform.TransformerException te ) { 283 Throwable nestedException = te.getException(); 284 if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) { 285 throw (javax.xml.xpath.XPathFunctionException )nestedException; 286 } else { 287 throw new XPathExpressionException ( te ); 290 } 291 } 292 293 } 294 295 private boolean isSupported( QName returnType ) { 296 if ( ( returnType.equals( XPathConstants.STRING ) ) || 297 ( returnType.equals( XPathConstants.NUMBER ) ) || 298 ( returnType.equals( XPathConstants.BOOLEAN ) ) || 299 ( returnType.equals( XPathConstants.NODE ) ) || 300 ( returnType.equals( XPathConstants.NODESET ) ) ) { 301 302 return true; 303 } 304 return false; 305 } 306 307 private Object getResultAsType( XObject resultObject, QName returnType ) 308 throws javax.xml.transform.TransformerException { 309 if ( returnType.equals( XPathConstants.STRING ) ) { 311 return resultObject.str(); 312 } 313 if ( returnType.equals( XPathConstants.NUMBER ) ) { 315 return new Double ( resultObject.num()); 316 } 317 if ( returnType.equals( XPathConstants.BOOLEAN ) ) { 319 return new Boolean ( resultObject.bool()); 320 } 321 if ( returnType.equals( XPathConstants.NODESET ) ) { 323 return resultObject.nodelist(); 324 } 325 if ( returnType.equals( XPathConstants.NODE ) ) { 327 NodeIterator ni = resultObject.nodeset(); 328 return ni.nextNode(); 330 } 331 String fmsg = XSLMessages.createXPATHMessage( 332 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 333 new Object [] { returnType.toString()}); 334 throw new IllegalArgumentException ( fmsg ); 335 } 336 337 338 339 363 public String evaluate(String expression, Object item) 364 throws XPathExpressionException { 365 return (String )this.evaluate( expression, item, XPathConstants.STRING ); 366 } 367 368 385 public XPathExpression compile(String expression) 386 throws XPathExpressionException { 387 if ( expression == null ) { 388 String fmsg = XSLMessages.createXPATHMessage( 389 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 390 new Object [] {"XPath expression"} ); 391 throw new NullPointerException ( fmsg ); 392 } 393 try { 394 com.sun.org.apache.xpath.internal.XPath xpath = new XPath (expression, null, 395 prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT ); 396 XPathExpressionImpl ximpl = new XPathExpressionImpl (xpath, 398 prefixResolver, functionResolver, variableResolver, 399 featureSecureProcessing ); 400 return ximpl; 401 } catch ( javax.xml.transform.TransformerException te ) { 402 throw new XPathExpressionException ( te ) ; 403 } 404 } 405 406 407 435 public Object evaluate(String expression, InputSource source, 436 QName returnType) throws XPathExpressionException { 437 if( source== null ) { 439 String fmsg = XSLMessages.createXPATHMessage( 440 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 441 new Object [] {"source"} ); 442 throw new NullPointerException ( fmsg ); 443 } 444 if ( expression == null ) { 445 String fmsg = XSLMessages.createXPATHMessage( 446 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 447 new Object [] {"XPath expression"} ); 448 throw new NullPointerException ( fmsg ); 449 } 450 if ( returnType == null ) { 451 String fmsg = XSLMessages.createXPATHMessage( 452 XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, 453 new Object [] {"returnType"} ); 454 throw new NullPointerException ( fmsg ); 455 } 456 457 if ( !isSupported ( returnType ) ) { 460 String fmsg = XSLMessages.createXPATHMessage( 461 XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE, 462 new Object [] { returnType.toString() } ); 463 throw new IllegalArgumentException ( fmsg ); 464 } 465 466 try { 467 468 Document document = getParser().parse( source ); 469 470 XObject resultObject = eval( expression, document ); 471 return getResultAsType( resultObject, returnType ); 472 } catch ( SAXException e ) { 473 throw new XPathExpressionException ( e ); 474 } catch( IOException e ) { 475 throw new XPathExpressionException ( e ); 476 } catch ( javax.xml.transform.TransformerException te ) { 477 Throwable nestedException = te.getException(); 478 if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) { 479 throw (javax.xml.xpath.XPathFunctionException )nestedException; 480 } else { 481 throw new XPathExpressionException ( te ); 482 } 483 } 484 485 } 486 487 488 489 490 513 public String evaluate(String expression, InputSource source) 514 throws XPathExpressionException { 515 return (String )this.evaluate( expression, source, XPathConstants.STRING ); 516 } 517 518 533 public void reset() { 534 this.variableResolver = this.origVariableResolver; 535 this.functionResolver = this.origFunctionResolver; 536 this.namespaceContext = null; 537 } 538 539 } 540 | Popular Tags |