JSTLXPathImplpublic class JSTLXPathImpl extends Object implements XPathThe JSTLXPathImpl class provides implementation for the methods defined in
javax.xml.xpath.XPath interface. This provide simple access to the results
of an XPath expression.
This class provides our own implementation of XPath, so that we can support
a generic Object type in returnType arguement for XPath's evaluate instance
method.
Most of the implementation is exactly similar to what is already provided in
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.java |
Fields Summary |
---|
private XPathVariableResolver | variableResolver | private XPathFunctionResolver | functionResolver | private XPathVariableResolver | origVariableResolver | private XPathFunctionResolver | origFunctionResolver | private NamespaceContext | namespaceContext | private JAXPPrefixResolver | prefixResolver | private boolean | featureSecureProcessing | private static Document | d |
Constructors Summary |
---|
JSTLXPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr)
this.origVariableResolver = this.variableResolver = vr;
this.origFunctionResolver = this.functionResolver = fr;
| JSTLXPathImpl(XPathVariableResolver vr, XPathFunctionResolver fr, boolean featureSecureProcessing)
this.origVariableResolver = this.variableResolver = vr;
this.origFunctionResolver = this.functionResolver = fr;
this.featureSecureProcessing = featureSecureProcessing;
|
Methods Summary |
---|
public javax.xml.xpath.XPathExpression | compile(java.lang.String expression)Compile an XPath expression for later evaluation.
If expression contains any {@link XPathFunction}s,
they must be available via the {@link XPathFunctionResolver}.
An {@link XPathExpressionException} will be thrown if the XPathFunction
cannot be resovled with the XPathFunctionResolver .
If expression is null , a NullPointerException is thrown.
// This is never used in JSTL
if ( expression == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"XPath expression"} );
throw new NullPointerException ( fmsg );
}
return null;
/*
try {
com.sun.org.apache.xpath.internal.XPath xpath = new XPath (expression, null,
prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT );
// Can have errorListener
com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl ximpl =
new com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl (xpath,
prefixResolver, functionResolver, variableResolver,
featureSecureProcessing );
return ximpl;
} catch ( javax.xml.transform.TransformerException te ) {
throw new XPathExpressionException ( te ) ;
}
**/
| private com.sun.org.apache.xpath.internal.objects.XObject | eval(java.lang.String expression, java.lang.Object contextItem)
com.sun.org.apache.xpath.internal.XPath xpath = new com.sun.org.apache.xpath.internal.XPath( expression,
null, prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT );
com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null;
if ( functionResolver != null ) {
com.sun.org.apache.xpath.internal.jaxp.JAXPExtensionsProvider jep =
new com.sun.org.apache.xpath.internal.jaxp.JAXPExtensionsProvider(
functionResolver, featureSecureProcessing );
xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep );
} else {
xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();
}
XObject xobj = null;
xpathSupport.setVarStack(new com.sun.org.apache.xpath.internal.jaxp.JAXPVariableStack(variableResolver));
// If item is null, then we will create a a Dummy contextNode
if ( contextItem instanceof Node ) {
xobj = xpath.execute (xpathSupport, (Node)contextItem,
prefixResolver );
} else {
xobj = xpath.execute ( xpathSupport, DTM.NULL, prefixResolver );
}
return xobj;
| public java.lang.Object | evaluate(java.lang.String expression, java.lang.Object item, javax.xml.namespace.QName returnType)Evaluate an 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} (
{@link XPathConstants#NUMBER NUMBER},
{@link XPathConstants#STRING STRING},
{@link XPathConstants#BOOLEAN BOOLEAN},
{@link XPathConstants#NODE NODE} or
{@link XPathConstants#NODESET NODESET})
then an IllegalArgumentException is thrown.
If a null value is provided for
item , an empty document will be used for the
context.
If expression or returnType is null , then a
NullPointerException is thrown.
if ( expression == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"XPath expression"} );
throw new NullPointerException ( fmsg );
}
if ( returnType == null ) {
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 or JSTLXPathConstants
if ( !isSupported ( returnType ) ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString() } );
throw new IllegalArgumentException ( fmsg );
}
try {
XObject resultObject = eval( expression, item );
return getResultAsType( resultObject, 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.String expression, java.lang.Object item)Evaluate an XPath expression in the specified context and return the result as a String .
This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a returnType of
{@link XPathConstants#STRING}.
See "Evaluation of XPath Expressions" 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.
If expression is null , then a NullPointerException is thrown.
return (String)this.evaluate( expression, item, XPathConstants.STRING );
| public java.lang.Object | evaluate(java.lang.String expression, org.xml.sax.InputSource source, javax.xml.namespace.QName returnType)Evaluate an 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(String expression, 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 expression , source or returnType is null ,
then a NullPointerException is thrown.
// Checking validity of different parameters
if( source== null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"source"} );
throw new NullPointerException ( fmsg );
}
if ( expression == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"XPath expression"} );
throw new NullPointerException ( fmsg );
}
if ( returnType == null ) {
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 {
Document document = getParser().parse( source );
XObject resultObject = eval( expression, document );
return getResultAsType( resultObject, returnType );
} catch ( SAXException e ) {
throw new XPathExpressionException ( e );
} catch( IOException e ) {
throw new XPathExpressionException ( e );
} catch ( javax.xml.transform.TransformerException te ) {
Throwable nestedException = te.getException();
if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
throw (javax.xml.xpath.XPathFunctionException)nestedException;
} else {
throw new XPathExpressionException ( te );
}
}
| public java.lang.String | evaluate(java.lang.String expression, org.xml.sax.InputSource source)Evaluate an XPath expression in the context of the specified InputSource
and return the result as a String .
This method calls {@link #evaluate(String expression, 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 expression or source is null ,
then a NullPointerException is thrown.
return (String)this.evaluate( expression, source, XPathConstants.STRING );
| private static org.w3c.dom.Document | getDummyDocument()
// we don't need synchronization here; even if two threads
// enter this code at the same time, we just waste a little time
if(d==null) {
DOMImplementation dim = getParser().getDOMImplementation();
d = dim.createDocument("http://java.sun.com/jaxp/xpath",
"dummyroot", null);
}
return d;
| public javax.xml.namespace.NamespaceContext | getNamespaceContext()Returns the current namespace context.
return namespaceContext;
| private static javax.xml.parsers.DocumentBuilder | getParser()
try {
// we'd really like to cache those DocumentBuilders, but we can't because:
// 1. thread safety. parsers are not thread-safe, so at least
// we need one instance per a thread.
// 2. parsers are non-reentrant, so now we are looking at having a
// pool of parsers.
// 3. then the class loading issue. The look-up procedure of
// DocumentBuilderFactory.newInstance() depends on context class loader
// and system properties, which may change during the execution of JVM.
//
// so we really have to create a fresh DocumentBuilder every time we need one
// - KK
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setValidating( false );
return dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// this should never happen with a well-behaving JAXP implementation.
throw new Error(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 Boolean.valueOf( 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();
}
// JSTLXPathConstants.OBJECT
if ( returnType.equals( JSTLXPathConstants.OBJECT ) ) {
if (resultObject instanceof com.sun.org.apache.xpath.internal.objects.XNodeSet)
return resultObject.nodelist();
else
return resultObject.object();
}
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString()});
throw new IllegalArgumentException( fmsg );
| public javax.xml.xpath.XPathFunctionResolver | getXPathFunctionResolver()Returns the current function resolver.
return functionResolver;
| public javax.xml.xpath.XPathVariableResolver | getXPathVariableResolver()Returns the current variable resolver.
return variableResolver;
| private boolean | isSupported(javax.xml.namespace.QName returnType)
if ( ( returnType.equals( XPathConstants.STRING ) ) ||
( returnType.equals( XPathConstants.NUMBER ) ) ||
( returnType.equals( XPathConstants.BOOLEAN ) ) ||
( returnType.equals( XPathConstants.NODE ) ) ||
( returnType.equals( XPathConstants.NODESET ) ) ||
( returnType.equals( JSTLXPathConstants.OBJECT ) ) ) {
return true;
}
return false;
| public void | reset()Reset this XPath to its original configuration.
XPath is reset to the same state as when it was created with
{@link XPathFactory#newXPath()}.
reset() is designed to allow the reuse of existing XPath s
thus saving resources associated with the creation of new XPath s.
The reset XPath is not guaranteed to have the same
{@link XPathFunctionResolver}, {@link XPathVariableResolver}
or {@link NamespaceContext} Object s, e.g. {@link Object#equals(Object obj)}.
It is guaranteed to have a functionally equal XPathFunctionResolver ,
XPathVariableResolver
and NamespaceContext .
this.variableResolver = this.origVariableResolver;
this.functionResolver = this.origFunctionResolver;
this.namespaceContext = null;
| public void | setNamespaceContext(javax.xml.namespace.NamespaceContext nsContext)Establishes a namespace context.
if ( nsContext == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"NamespaceContext"} );
throw new NullPointerException( fmsg );
}
this.namespaceContext = nsContext;
this.prefixResolver = new com.sun.org.apache.xpath.internal.jaxp.JAXPPrefixResolver ( nsContext );
| public void | setXPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver resolver)Establishes a function resolver.
if ( resolver == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"XPathFunctionResolver"} );
throw new NullPointerException( fmsg );
}
this.functionResolver = resolver;
| public void | setXPathVariableResolver(javax.xml.xpath.XPathVariableResolver resolver)Establishes a variable resolver.
if ( resolver == null ) {
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
new Object[] {"XPathVariableResolver"} );
throw new NullPointerException( fmsg );
}
this.variableResolver = resolver;
|
|