XPathExpressionImplpublic class XPathExpressionImpl extends Object implements XPathExpressionThe XPathExpression interface encapsulates a (compiled) XPath expression. |
Fields Summary |
---|
private XPathFunctionResolver | functionResolver | private XPathVariableResolver | variableResolver | private JAXPPrefixResolver | prefixResolver | private XPath | xpath | private boolean | featureSecureProcessing | static DocumentBuilderFactory | dbf | static DocumentBuilder | db | static Document | d |
Constructors Summary |
---|
protected XPathExpressionImpl()Protected constructor to prevent direct instantiation; use compile()
from the context.
| protected XPathExpressionImpl(XPath xpath, JAXPPrefixResolver prefixResolver, XPathFunctionResolver functionResolver, XPathVariableResolver variableResolver)
this.xpath = xpath;
this.prefixResolver = prefixResolver;
this.functionResolver = functionResolver;
this.variableResolver = variableResolver;
this.featureSecureProcessing = false;
| protected XPathExpressionImpl(XPath xpath, JAXPPrefixResolver prefixResolver, XPathFunctionResolver functionResolver, XPathVariableResolver variableResolver, boolean featureSecureProcessing)
this.xpath = xpath;
this.prefixResolver = prefixResolver;
this.functionResolver = functionResolver;
this.variableResolver = variableResolver;
this.featureSecureProcessing = featureSecureProcessing;
|
Methods Summary |
---|
public java.lang.Object | eval(java.lang.Object item, javax.xml.namespace.QName returnType)
XObject resultObject = eval ( item );
return getResultAsType( resultObject, returnType );
| private com.sun.org.apache.xpath.internal.objects.XObject | eval(java.lang.Object contextItem)
com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null;
if ( functionResolver != null ) {
JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
functionResolver, featureSecureProcessing );
xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep );
} else {
xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();
}
xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
XObject xobj = null;
Node contextNode = (Node)contextItem;
// We always need to have a ContextNode with Xalan XPath implementation
// To allow simple expression evaluation like 1+1 we are setting
// dummy Document as Context Node
if ( contextNode == null )
xobj = xpath.execute(xpathSupport, DTM.NULL, prefixResolver);
else
xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);
return xobj;
| public java.lang.String | evaluate(org.xml.sax.InputSource source)Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as a
String .
This method calls {@link #evaluate(InputSource source, QName returnType)} with a returnType of
{@link XPathConstants#STRING}.
See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
for context item evaluation,
variable, function and QName resolution and return type conversion.
If source is null , then a NullPointerException is thrown.
return (String)this.evaluate( source, XPathConstants.STRING );
| public java.lang.Object | evaluate(java.lang.Object item, javax.xml.namespace.QName returnType)Evaluate the compiled XPath expression in the specified context and
return the result as the specified type.
See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
for context item evaluation,
variable, function and QName resolution and return type conversion.
If returnType is not one of the types defined
in {@link XPathConstants},
then an IllegalArgumentException is thrown.
If a null value is provided for
item , an empty document will be used for the
context.
If returnType is null , then a
NullPointerException is thrown.
//Validating parameters to enforce constraints defined by JAXP spec
if ( returnType == null ) {
//Throwing NullPointerException as defined in spec
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"returnType"} );
throw new NullPointerException( fmsg );
}
// Checking if requested returnType is supported. returnType need to be
// defined in XPathConstants
if ( !isSupported ( returnType ) ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString() } );
throw new IllegalArgumentException ( fmsg );
}
try {
return eval( item, returnType);
} catch ( java.lang.NullPointerException npe ) {
// If VariableResolver returns null Or if we get
// NullPointerException at this stage for some other reason
// then we have to reurn XPathException
throw new XPathExpressionException ( npe );
} catch ( javax.xml.transform.TransformerException te ) {
Throwable nestedException = te.getException();
if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
throw (javax.xml.xpath.XPathFunctionException)nestedException;
} else {
// For any other exceptions we need to throw
// XPathExpressionException ( as per spec )
throw new XPathExpressionException( te);
}
}
| public java.lang.String | evaluate(java.lang.Object item)Evaluate the compiled XPath expression in the specified context and
return the result as a String .
This method calls {@link #evaluate(Object item, QName returnType)}
with a returnType of
{@link XPathConstants#STRING}.
See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
for context item evaluation,
variable, function and QName resolution and return type conversion.
If a null value is provided for
item , an empty document will be used for the
context.
return (String)this.evaluate( item, XPathConstants.STRING );
| public java.lang.Object | evaluate(org.xml.sax.InputSource source, javax.xml.namespace.QName returnType)Evaluate the compiled XPath expression in the context of the
specified InputSource and return the result as the
specified type.
This method builds a data model for the {@link InputSource} and calls
{@link #evaluate(Object item, QName returnType)} on the resulting
document object.
See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
for context item evaluation,
variable, function and QName resolution and return type conversion.
If returnType is not one of the types defined in
{@link XPathConstants},
then an IllegalArgumentException is thrown.
If source or returnType is null ,
then a NullPointerException is thrown.
if ( ( source == null ) || ( returnType == null ) ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_SOURCE_RETURN_TYPE_CANNOT_BE_NULL,
null );
throw new NullPointerException ( fmsg );
}
// Checking if requested returnType is supported. returnType need to be
// defined in XPathConstants
if ( !isSupported ( returnType ) ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString() } );
throw new IllegalArgumentException ( fmsg );
}
try {
if ( dbf == null ) {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setValidating( false );
}
db = dbf.newDocumentBuilder();
Document document = db.parse( source );
return eval( document, returnType );
} catch ( Exception e ) {
throw new XPathExpressionException ( e );
}
| private java.lang.Object | getResultAsType(com.sun.org.apache.xpath.internal.objects.XObject resultObject, javax.xml.namespace.QName returnType)
// XPathConstants.STRING
if ( returnType.equals( XPathConstants.STRING ) ) {
return resultObject.str();
}
// XPathConstants.NUMBER
if ( returnType.equals( XPathConstants.NUMBER ) ) {
return new Double ( resultObject.num());
}
// XPathConstants.BOOLEAN
if ( returnType.equals( XPathConstants.BOOLEAN ) ) {
return new Boolean( resultObject.bool());
}
// XPathConstants.NODESET ---ORdered, UNOrdered???
if ( returnType.equals( XPathConstants.NODESET ) ) {
return resultObject.nodelist();
}
// XPathConstants.NODE
if ( returnType.equals( XPathConstants.NODE ) ) {
NodeIterator ni = resultObject.nodeset();
//Return the first node, or null
return ni.nextNode();
}
// If isSupported check is already done then the execution path
// shouldn't come here. Being defensive
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString()});
throw new IllegalArgumentException ( fmsg );
| private boolean | isSupported(javax.xml.namespace.QName returnType)
// XPathConstants.STRING
if ( ( returnType.equals( XPathConstants.STRING ) ) ||
( returnType.equals( XPathConstants.NUMBER ) ) ||
( returnType.equals( XPathConstants.BOOLEAN ) ) ||
( returnType.equals( XPathConstants.NODE ) ) ||
( returnType.equals( XPathConstants.NODESET ) ) ) {
return true;
}
return false;
| public void | setXPath(com.sun.org.apache.xpath.internal.XPath xpath)
this.xpath = xpath;
|
|