--- old/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java Wed Jan 21 21:16:29 2015 +++ new/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathExpressionImpl.java Wed Jan 21 21:16:29 2015 @@ -1,16 +1,16 @@ /* - * reserved comment block - * DO NOT REMOVE OR ALTER! + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. */ /* - * Copyright 1999-2004 The Apache Software Foundation. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,36 +17,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// $Id: XPathExpressionImpl.java,v 1.3 2005/09/27 09:40:43 sunithareddy Exp $ package com.sun.org.apache.xpath.internal.jaxp; -import com.sun.org.apache.xpath.internal.*; -import javax.xml.transform.TransformerException; - -import com.sun.org.apache.xpath.internal.objects.XObject; -import com.sun.org.apache.xml.internal.dtm.DTM; -import com.sun.org.apache.xml.internal.utils.PrefixResolver; -import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; -import com.sun.org.apache.xalan.internal.res.XSLMessages; -import com.sun.org.apache.xalan.internal.utils.FactoryImpl; import com.sun.org.apache.xalan.internal.utils.FeatureManager; - -import javax.xml.namespace.NamespaceContext; +import com.sun.org.apache.xpath.internal.objects.XObject; import javax.xml.namespace.QName; -import javax.xml.xpath.XPathExpressionException; +import javax.xml.transform.TransformerException; import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathEvaluationResult; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFunctionResolver; import javax.xml.xpath.XPathVariableResolver; -import javax.xml.xpath.XPathConstants; - -import org.w3c.dom.Node; import org.w3c.dom.Document; -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.traversal.NodeIterator; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.DocumentBuilder; - +import org.w3c.dom.Node; import org.xml.sax.InputSource; /** @@ -54,22 +39,10 @@ * * @author Ramesh Mandava */ -public class XPathExpressionImpl implements javax.xml.xpath.XPathExpression{ +public class XPathExpressionImpl extends XPathImplUtil implements XPathExpression { - private XPathFunctionResolver functionResolver; - private XPathVariableResolver variableResolver; - private JAXPPrefixResolver prefixResolver; private com.sun.org.apache.xpath.internal.XPath xpath; - // By default Extension Functions are allowed in XPath Expressions. If - // Secure Processing Feature is set on XPathFactory then the invocation of - // extensions function need to throw XPathFunctionException - private boolean featureSecureProcessing = false; - - private boolean useServicesMechanism = true; - - private final FeatureManager featureManager; - /** Protected constructor to prevent direct instantiation; use compile() * from the context. */ @@ -81,7 +54,7 @@ protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath, JAXPPrefixResolver prefixResolver, XPathFunctionResolver functionResolver, - XPathVariableResolver variableResolver ) { + XPathVariableResolver variableResolver) { this(xpath, prefixResolver, functionResolver, variableResolver, false, true, new FeatureManager()); }; @@ -89,291 +62,108 @@ protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath, JAXPPrefixResolver prefixResolver,XPathFunctionResolver functionResolver, XPathVariableResolver variableResolver, boolean featureSecureProcessing, - boolean useServicesMechanism, FeatureManager featureManager ) { + boolean useServiceMechanism, FeatureManager featureManager) { this.xpath = xpath; this.prefixResolver = prefixResolver; this.functionResolver = functionResolver; this.variableResolver = variableResolver; this.featureSecureProcessing = featureSecureProcessing; - this.useServicesMechanism = useServicesMechanism; + this.useServiceMechanism = useServiceMechanism; this.featureManager = featureManager; }; - public void setXPath (com.sun.org.apache.xpath.internal.XPath xpath ) { + public void setXPath (com.sun.org.apache.xpath.internal.XPath xpath) { this.xpath = xpath; } public Object eval(Object item, QName returnType) throws javax.xml.transform.TransformerException { - XObject resultObject = eval ( item ); - return getResultAsType( resultObject, returnType ); + XObject resultObject = eval(item, xpath); + return getResultAsType(resultObject, returnType); } - private XObject eval ( Object contextItem ) - throws javax.xml.transform.TransformerException { - com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null; - if ( functionResolver != null ) { - JAXPExtensionsProvider jep = new JAXPExtensionsProvider( - functionResolver, featureSecureProcessing, featureManager ); - 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; - } - - - /** - *
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.
Object
that is the result of evaluating the
- * expression and converting the result to
- * returnType
.
- *
- * @throws XPathExpressionException If the expression cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one
- * of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If returnType
is
- * null
.
- */
+ @Override
public Object evaluate(Object item, QName returnType)
throws XPathExpressionException {
- //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 );
- }
+ isSupported(returnType);
try {
- return eval( item, returnType);
- } catch ( java.lang.NullPointerException npe ) {
+ 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 ) {
+ throw new XPathExpressionException (npe);
+ } catch (javax.xml.transform.TransformerException te) {
Throwable nestedException = te.getException();
- if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
+ 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);
+ // XPathExpressionException (as per spec)
+ throw new XPathExpressionException(te);
}
}
-
}
- /**
- * 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.
- *
- * @param item The starting context (node or node list, for example).
- *
- * @return The String
that is the result of evaluating the
- * expression and converting the result to a
- * String
.
- *
- * @throws XPathExpressionException If the expression cannot be evaluated.
- */
+
+ @Override
public String evaluate(Object item)
throws XPathExpressionException {
- return (String)this.evaluate( item, XPathConstants.STRING );
+ return (String)this.evaluate(item, XPathConstants.STRING);
}
-
-
- static DocumentBuilderFactory dbf = null;
- static DocumentBuilder db = null;
- static Document d = null;
-
- /**
- *
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.
InputSource
of the document to evaluate
- * over.
- * @param returnType The desired return type.
- *
- * @return The Object
that is the result of evaluating the
- * expression and converting the result to
- * returnType
.
- *
- * @throws XPathExpressionException If the expression cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one
- * of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If source
or
- * returnType
is null
.
- */
+ @Override
public Object evaluate(InputSource source, QName returnType)
throws XPathExpressionException {
- 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 );
- }
+ isSupported (returnType);
try {
- if ( dbf == null ) {
- dbf = FactoryImpl.getDOMFactory(useServicesMechanism);
- 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 );
+ Document document = getDocument(source);
+ return eval(document, returnType);
+ } catch (TransformerException e) {
+ throw new XPathExpressionException(e);
}
}
- /**
- * 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.
InputSource
of the document to evaluate over.
- *
- * @return The String
that is the result of evaluating the expression and converting the result to a
- * String
.
- *
- * @throws XPathExpressionException If the expression cannot be evaluated.
- * @throws NullPointerException If source
is null
.
- */
+ @Override
public String evaluate(InputSource source)
throws XPathExpressionException {
- return (String)this.evaluate( source, XPathConstants.STRING );
+ return (String)this.evaluate(source, XPathConstants.STRING);
}
- private boolean isSupported( 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 ) ) ) {
+ @Override
+ public Establishes a variable resolver.
- * - * @param resolver Variable Resolver - */ + + //-Override- public void setXPathVariableResolver(XPathVariableResolver resolver) { - if ( resolver == null ) { - String fmsg = XSLMessages.createXPATHMessage( - XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, - new Object[] {"XPathVariableResolver"} ); - throw new NullPointerException( fmsg ); - } + requireNonNull(resolver, "XPathVariableResolver"); this.variableResolver = resolver; } - /** - *Returns the current variable resolver.
- * - * @return Current variable resolver - */ + //-Override- public XPathVariableResolver getXPathVariableResolver() { return variableResolver; } - /** - *Establishes a function resolver.
- * - * @param resolver XPath function resolver - */ + //-Override- public void setXPathFunctionResolver(XPathFunctionResolver resolver) { - if ( resolver == null ) { - String fmsg = XSLMessages.createXPATHMessage( - XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, - new Object[] {"XPathFunctionResolver"} ); - throw new NullPointerException( fmsg ); - } + requireNonNull(resolver, "XPathFunctionResolver"); this.functionResolver = resolver; } - /** - *Returns the current function resolver.
- * - * @return Current function resolver - */ + //-Override- public XPathFunctionResolver getXPathFunctionResolver() { return functionResolver; } - /** - *Establishes a namespace context.
- * - * @param nsContext Namespace context to use - */ + //-Override- public void setNamespaceContext(NamespaceContext nsContext) { - if ( nsContext == null ) { - String fmsg = XSLMessages.createXPATHMessage( - XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, - new Object[] {"NamespaceContext"} ); - throw new NullPointerException( fmsg ); - } + requireNonNull(nsContext, "NamespaceContext"); this.namespaceContext = nsContext; - this.prefixResolver = new JAXPPrefixResolver ( nsContext ); + this.prefixResolver = new JAXPPrefixResolver (nsContext); } - /** - *Returns the current namespace context.
- * - * @return Current Namespace context - */ + //-Override- public NamespaceContext getNamespaceContext() { return namespaceContext; } - private static Document d = null; - - private 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 = FactoryImpl.getDOMFactory(useServiceMechanism); - 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); - } - } - - + /** + * Evaluate an {@code XPath} expression in the specified context. + * @param expression The XPath expression. + * @param contextItem The starting context. + * @return an XObject as the result of evaluating the expression + * @throws TransformerException if evaluating fails + */ private XObject eval(String expression, Object contextItem) - throws javax.xml.transform.TransformerException { - 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 ) { - JAXPExtensionsProvider jep = new JAXPExtensionsProvider( - functionResolver, featureSecureProcessing, featureManager ); - xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep ); - } else { - xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(); - } + throws TransformerException { + requireNonNull(expression, "XPath expression"); + 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); - XObject xobj = null; - - xpathSupport.setVarStack(new 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; + return eval(contextItem, xpath); } - /** - *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.
Object
of returnType
.
- *
- * @throws XPathExpressionException If expression
cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If expression
or returnType
is null
.
- */
+ //-Override-
public Object evaluate(String expression, Object item, QName returnType)
throws XPathExpressionException {
- 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 );
- }
+ //this check is necessary before calling eval to maintain binary compatibility
+ requireNonNull(expression, "XPath expression");
+ isSupported(returnType);
try {
- XObject resultObject = eval( expression, item );
- return getResultAsType( resultObject, returnType );
- } catch ( java.lang.NullPointerException npe ) {
+ 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 ) {
+ throw new XPathExpressionException (npe);
+ } catch (TransformerException te) {
Throwable nestedException = te.getException();
- if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
+ 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 );
+ // XPathExpressionException (as per spec)
+ throw new XPathExpressionException (te);
}
}
}
- private boolean isSupported( QName returnType ) {
- 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;
- }
-
- private Object getResultAsType( XObject resultObject, QName returnType )
- throws javax.xml.transform.TransformerException {
- // 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();
- }
- String fmsg = XSLMessages.createXPATHMessage(
- XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
- new Object[] { returnType.toString()});
- throw new IllegalArgumentException( fmsg );
- }
-
-
-
- /**
- * 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.
String
that is the result of evaluating the expression and
- * converting the result to a String
.
- *
- * @throws XPathExpressionException If expression
cannot be evaluated.
- * @throws NullPointerException If expression
is null
.
- */
+ //-Override-
public String evaluate(String expression, Object item)
throws XPathExpressionException {
- return (String)this.evaluate( expression, item, XPathConstants.STRING );
+ return (String)this.evaluate(expression, item, XPathConstants.STRING);
}
- /**
- * 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.
expression
cannot be compiled.
- * @throws NullPointerException If expression
is null
.
- */
+ //-Override-
public XPathExpression compile(String expression)
throws XPathExpressionException {
- if ( expression == null ) {
- String fmsg = XSLMessages.createXPATHMessage(
- XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
- new Object[] {"XPath expression"} );
- throw new NullPointerException ( fmsg );
- }
+ requireNonNull(expression, "XPath expression");
try {
com.sun.org.apache.xpath.internal.XPath xpath = new XPath (expression, null,
- prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT );
+ prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT);
// Can have errorListener
XPathExpressionImpl ximpl = new XPathExpressionImpl (xpath,
prefixResolver, functionResolver, variableResolver,
- featureSecureProcessing, useServiceMechanism, featureManager );
+ featureSecureProcessing, useServiceMechanism, featureManager);
return ximpl;
- } catch ( javax.xml.transform.TransformerException te ) {
- throw new XPathExpressionException ( te ) ;
+ } catch (TransformerException te) {
+ throw new XPathExpressionException (te) ;
}
}
-
- /**
- * 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.
Object
that encapsulates the result of evaluating the expression.
- *
- * @throws XPathExpressionException If expression cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If expression
, source
or returnType
- * is null
.
- */
+ //-Override-
public Object evaluate(String expression, InputSource source,
QName returnType) throws XPathExpressionException {
- // 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 );
- }
+ isSupported(returnType);
- //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 ) {
+ Document document = getDocument(source);
+ XObject resultObject = eval(expression, document);
+ return getResultAsType(resultObject, returnType);
+ } catch (TransformerException te) {
Throwable nestedException = te.getException();
- if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
+ if (nestedException instanceof javax.xml.xpath.XPathFunctionException) {
throw (javax.xml.xpath.XPathFunctionException)nestedException;
} else {
- throw new XPathExpressionException ( te );
+ throw new XPathExpressionException (te);
}
}
-
}
-
-
-
- /**
- * 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.
InputSource
of the document to evaluate over.
- *
- * @return The String
that is the result of evaluating the expression and
- * converting the result to a String
.
- *
- * @throws XPathExpressionException If expression cannot be evaluated.
- * @throws NullPointerException If expression
or source
is null
.
- */
+ //-Override-
public String evaluate(String expression, InputSource source)
throws XPathExpressionException {
- return (String)this.evaluate( expression, source, XPathConstants.STRING );
+ return (String)this.evaluate(expression, source, XPathConstants.STRING);
}
- /**
- * 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
.
XPath
provides access to the XPath evaluation environment and expressions.
Evaluation of XPath Expressions. | * * - * *|
---|---|
context | *
@@ -55,8 +54,8 @@
* If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}
* set with {@link #setXPathVariableResolver(XPathVariableResolver resolver)}.
* An {@link XPathExpressionException} is raised if the variable resolver is undefined or
- * the resolver returns null for the variable.
- * The value of a variable must be immutable through the course of any single evaluation.
+ * the resolver returns {@code null} for the variable.
+ * The value of a variable must be immutable through the course of any single evaluation.
* |
*
* This result of evaluating an expression is converted to an instance of the desired return type. * Valid return types are defined in {@link XPathConstants}. - * Conversion to the return type follows XPath conversion rules. + * Conversion to the return type follows XPath conversion rules. * | *
An XPath object is not thread-safe and not reentrant.
* In other words, it is the application's responsibility to make
* sure that one {@link XPath} object is not used from
- * more than one thread at any given time, and while the evaluate
+ * more than one thread at any given time, and while the {@code evaluate}
* method is invoked, applications may not recursively call
- * the evaluate
method.
+ * the {@code evaluate} method.
*
* * @author Norman Walsh @@ -100,36 +99,38 @@ */ public interface XPath { - /** - *
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
.
Establish a variable resolver.
+ * Reset this {@code XPath} to its original configuration. * - *A NullPointerException
is thrown if resolver
is null
.
{@code XPath} is reset to the same state as when it was created with + * {@link XPathFactory#newXPath()}. + * {@code reset()} is designed to allow the reuse of existing {@code XPath}s + * thus saving resources associated with the creation of new {@code XPath}s. * + *
The reset {@code XPath} is not guaranteed to have the same + * {@link XPathFunctionResolver}, {@link XPathVariableResolver} + * or {@link NamespaceContext} {@code Object}s, e.g. {@link Object#equals(Object obj)}. + * It is guaranteed to have a functionally equal {@code XPathFunctionResolver}, + * {@code XPathVariableResolver} and {@code NamespaceContext}. + */ + public void reset(); + + /** + * Establish a variable resolver. + * + *
A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
+ *
* @param resolver Variable resolver.
*
- * @throws NullPointerException If resolver
is null
.
+ * @throws NullPointerException If {@code resolver} is {@code null}.
*/
public void setXPathVariableResolver(XPathVariableResolver resolver);
/**
- *
Return the current variable resolver.
+ * Return the current variable resolver. * - *null
is returned in no variable resolver is in effect.
{@code null} is returned in no variable resolver is in effect. * * @return Current variable resolver. */ @@ -136,40 +137,40 @@ public XPathVariableResolver getXPathVariableResolver(); /** - *
Establish a function resolver.
+ * Establish a function resolver. * - *A NullPointerException
is thrown if resolver
is null
.
A {@code NullPointerException} is thrown if {@code resolver} is {@code null}.
*
* @param resolver XPath function resolver.
*
- * @throws NullPointerException If resolver
is null
.
+ * @throws NullPointerException If {@code resolver} is {@code null}.
*/
public void setXPathFunctionResolver(XPathFunctionResolver resolver);
/**
- *
Return the current function resolver.
+ * Return the current function resolver. + *+ * {@code null} is returned in no function resolver is in effect. * - *
null
is returned in no function resolver is in effect.
Establish a namespace context.
+ * Establish a namespace context. * - *A NullPointerException
is thrown if nsContext
is null
.
A {@code NullPointerException} is thrown if {@code nsContext} is {@code null}.
*
* @param nsContext Namespace context to use.
*
- * @throws NullPointerException If nsContext
is null
.
+ * @throws NullPointerException If {@code nsContext} is {@code null}.
*/
public void setNamespaceContext(NamespaceContext nsContext);
/**
- *
Return the current namespace context.
+ * Return the current namespace context. * - *null
is returned in no namespace context is in effect.
{@code null} is returned in no namespace context is in effect. * * @return Current Namespace context. */ @@ -176,115 +177,111 @@ public NamespaceContext getNamespaceContext(); /** - *
Compile an XPath expression for later evaluation.
+ * Compile an XPath expression for later evaluation. * - *If expression
contains any {@link XPathFunction}s,
+ *
If {@code 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
contains any variables, the
+ *
If {@code expression} contains any variables, the * {@link XPathVariableResolver} in effect - * at compile time will be used to resolve them.
+ * at compile time will be used to resolve them. * - *If expression
is null
, a NullPointerException
is thrown.
expression
cannot be compiled.
- * @throws NullPointerException If expression
is null
.
+ * @throws XPathExpressionException If {@code expression} cannot be compiled.
+ * @throws NullPointerException If {@code expression} is {@code null}.
*/
public XPathExpression compile(String expression)
throws XPathExpressionException;
/**
- * Evaluate an XPath
expression in the specified context and return the result as the specified type.
See Evaluation of XPath Expressions for context item evaluation,
- * variable, function and QName
resolution and return type conversion.
+ * See Evaluation of XPath Expressions + * for context item evaluation, variable, function and {@code QName} resolution + * and return type conversion. + *
+ * The parameter {@code item} represents the context the XPath expression + * will be operated on. The type of the context is implementation-dependent. + * If the value is {@code null}, the operation must have no dependency on + * the context, otherwise an XPathExpressionException will be thrown. * - *
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.
Object
of returnType
.
+ * @return The result of evaluating an XPath expression as an {@code Object} of {@code returnType}.
*
- * @throws XPathExpressionException If expression
cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If expression
or returnType
is null
.
+ * @throws XPathExpressionException If {@code expression} cannot be evaluated.
+ * @throws IllegalArgumentException If {@code 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}).
+ * @throws NullPointerException If {@code expression or returnType} is {@code null}.
*/
public Object evaluate(String expression, Object item, QName returnType)
throws XPathExpressionException;
/**
- * 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}.
This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a {@code returnType} of + * {@link XPathConstants#STRING}. * *
See Evaluation of XPath Expressions for context item evaluation, - * variable, function and QName resolution and return type conversion.
+ * 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.
+ * The parameter {@code item} represents the context the XPath expression
+ * will be operated on. The type of the context is implementation-dependent.
+ * If the value is {@code null}, the operation must have no dependency on
+ * the context, otherwise an XPathExpressionException will be thrown.
*
+ * @implNote
+ * The type of the context is usually {@link org.w3c.dom.Node}.
+ *
* @param expression The XPath expression.
- * @param item The starting context (a node, for example).
+ * @param item The context the XPath expression will be evaluated in.
*
- * @return The String
that is the result of evaluating the expression and
- * converting the result to a String
.
+ * @return The result of evaluating an XPath expression as a {@code String}.
*
- * @throws XPathExpressionException If expression
cannot be evaluated.
- * @throws NullPointerException If expression
is null
.
+ * @throws XPathExpressionException If {@code expression} cannot be evaluated.
+ * @throws NullPointerException If {@code expression} is {@code null}.
*/
public String evaluate(String expression, Object item)
throws XPathExpressionException;
/**
- *
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.
+ * {@link #evaluate(String expression, Object item, QName returnType)} on the resulting document object. * *See Evaluation of XPath Expressions for context item evaluation, - * variable, function and QName resolution and return type conversion.
+ * 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.
Object
that encapsulates the result of evaluating the expression.
+ * @return The {@code Object} that encapsulates the result of evaluating the expression.
*
* @throws XPathExpressionException If expression cannot be evaluated.
- * @throws IllegalArgumentException If returnType
is not one of the types defined in {@link XPathConstants}.
- * @throws NullPointerException If expression
, source
or returnType
- * is null
.
+ * @throws IllegalArgumentException If {@code returnType} is not one of the types defined in {@link XPathConstants}.
+ * @throws NullPointerException If {@code expression, source or returnType} is {@code null}.
*/
public Object evaluate(
String expression,
@@ -293,27 +290,209 @@
throws XPathExpressionException;
/**
- * 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 for context item evaluation, - * variable, function and QName resolution and return type conversion.
+ * variable, function and QName resolution and return type conversion. * - *If expression
or source
is null
,
- * then a NullPointerException
is thrown.
InputSource
of the document to evaluate over.
+ * @param source The {@code InputSource} of the document to evaluate over.
*
- * @return The String
that is the result of evaluating the expression and
- * converting the result to a String
.
+ * @return The {@code String} that is the result of evaluating the expression and
+ * converting the result to a {@code String}.
*
* @throws XPathExpressionException If expression cannot be evaluated.
- * @throws NullPointerException If expression
or source
is null
.
+ * @throws NullPointerException If {@code expression or source} is {@code null}.
*/
public String evaluate(String expression, InputSource source)
throws XPathExpressionException;
+
+ /**
+ * Evaluate an XPath expression in the specified context and return
+ * the result with the type specified through the {@code class type}
+ *
+ * + * The parameter {@code item} represents the context the XPath expression + * will be operated on. The type of the context is implementation-dependent. + * If the value is {@code null}, the operation must have no dependency on + * the context, otherwise an XPathExpressionException will be thrown. + * + * @implNote + * The type of the context is usually {@link org.w3c.dom.Node}. + * + * @implSpec + * The default implementation in the XPath API is equivalent to: + *
{@code + * (T)evaluate(expression, item, + * XPathEvaluationResult.XPathResultType.getQNameType(type)); + * }+ * + * Since the {@code evaluate} method does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying + * XPathEvaluationResult as the type will result in IllegalArgumentException. + * Any implementation supporting the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override + * this method. + * + * @param
{@code + * evaluateExpression(expression, item, XPathEvaluationResult.class); + * }+ *
+ * The parameter {@code item} represents the context the XPath expression + * will be operated on. The type of the context is implementation-dependent. + * If the value is {@code null}, the operation must have no dependency on + * the context, otherwise an XPathExpressionException will be thrown. + * + * @implNote + * The type of the context is usually {@link org.w3c.dom.Node}. + * + * @implSpec + * The default implementation in the XPath API is equivalent to: + *
{@code + * evaluateExpression(expression, item, XPathEvaluationResult.class); + * }+ * + * Since the {@code evaluate} method does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} + * type, the default implementation of this method will always throw an + * IllegalArgumentException. Any implementation supporting the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore + * override this method. + * + * @param expression The XPath expression. + * @param item The context the XPath expression will be evaluated in. + * + * @return The result of evaluating the expression. + * + * @throws XPathExpressionException If the expression cannot be evaluated. + * @throws IllegalArgumentException If the implementation of this method + * does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type. + * @throws NullPointerException If {@code expression} is {@code null}. + * + * @since 1.9 + */ + default XPathEvaluationResult> evaluateExpression(String expression, Object item) + throws XPathExpressionException + { + return evaluateExpression(expression, item, XPathEvaluationResult.class); + } + + /** + * Evaluate an XPath expression in the context of the specified {@code source} + * and return the result as specified. + *
+ * This method builds a data model for the {@link InputSource} and calls + * {@link #evaluateExpression(String expression, Object item, Class type)} + * on the resulting document object. The data model is usually + * {@link org.w3c.dom.Document} + * + * @implSpec + * The default implementation in the XPath API is equivalent to: + *
{@code + (T)evaluate(expression, source, + XPathEvaluationResult.XPathResultType.getQNameType(type)); + * }+ * + * Since the {@code evaluate} method does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type, specifying + * XPathEvaluationResult as the type will result in IllegalArgumentException. + * Any implementation supporting the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must override + * this method. + * + * @param
{@code + * evaluateExpression(expression, item, XPathEvaluationResult.class); + * }+ *
+ * + * @implSpec + * The default implementation in the XPath API is equivalent to: + *
{@code + * evaluateExpression(expression, source, XPathEvaluationResult.class); + * }+ * + * Since the {@code evaluate} method does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} + * type, the default implementation of this method will always throw an + * IllegalArgumentException. Any implementation supporting the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type must therefore + * override this method. + * + * @param expression The XPath expression. + * @param source The input source of the document to evaluate over. + * + * @return The result of evaluating the expression. + * + * @throws XPathExpressionException If the expression cannot be evaluated. + * @throws IllegalArgumentException If the implementation of this method + * does not support the + * {@link XPathEvaluationResult.XPathResultType#ANY ANY} type. + * @throws NullPointerException If {@code expression or source} is {@code null}. + * + * @since 1.9 + */ + default XPathEvaluationResult> evaluateExpression(String expression, InputSource source) + throws XPathExpressionException + { + return evaluateExpression(expression, source, XPathEvaluationResult.class); + } } --- old/src/java.xml/share/classes/javax/xml/xpath/XPathExpression.java Wed Jan 21 21:16:32 2015 +++ new/src/java.xml/share/classes/javax/xml/xpath/XPathExpression.java Wed Jan 21 21:16:31 2015 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,11 +25,11 @@ package javax.xml.xpath; -import org.xml.sax.InputSource; import javax.xml.namespace.QName; +import org.xml.sax.InputSource; /** - *
XPathExpression
provides access to compiled XPath expressions.
{@code XPathExpression} provides access to compiled XPath expressions.
* * *
* If the expression contains a variable reference, its value will be found through the {@link XPathVariableResolver}.
* An {@link XPathExpressionException} is raised if the variable resolver is undefined or
- * the resolver returns null for the variable.
+ * the resolver returns {@code null} for the variable.
* The value of a variable must be immutable through the course of any single evaluation.
* |
*
@@ -62,7 +62,7 @@
*
* If the expression contains a function reference, the function will be found through the {@link XPathFunctionResolver}.
* An {@link XPathExpressionException} is raised if the function resolver is undefined or
- * the function resolver returns null for the function.
+ * the function resolver returns {@code null} for the function.
* |
*
*