src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImplUtil.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.org.apache.xpath.internal.jaxp;
  27 
  28 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  29 import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
  30 import com.sun.org.apache.xml.internal.dtm.DTM;

  31 import com.sun.org.apache.xpath.internal.objects.XObject;
  32 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  33 import java.io.IOException;
  34 import javax.xml.namespace.QName;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.transform.TransformerException;
  38 import javax.xml.xpath.XPathConstants;
  39 import javax.xml.xpath.XPathEvaluationResult;
  40 import javax.xml.xpath.XPathExpressionException;
  41 import javax.xml.xpath.XPathFunctionResolver;
  42 import javax.xml.xpath.XPathNodes;
  43 import javax.xml.xpath.XPathVariableResolver;
  44 import jdk.xml.internal.JdkXmlFeatures;
  45 import org.w3c.dom.Document;
  46 import org.w3c.dom.Node;
  47 import org.w3c.dom.traversal.NodeIterator;
  48 import org.xml.sax.InputSource;
  49 import org.xml.sax.SAXException;
  50 


  56     XPathFunctionResolver functionResolver;
  57     XPathVariableResolver variableResolver;
  58     JAXPPrefixResolver prefixResolver;
  59     boolean useServiceMechanism = true;
  60     // By default Extension Functions are allowed in XPath Expressions. If
  61     // Secure Processing Feature is set on XPathFactory then the invocation of
  62     // extensions function need to throw XPathFunctionException
  63     boolean featureSecureProcessing = false;
  64     JdkXmlFeatures featureManager;
  65 
  66     /**
  67      * Evaluate an XPath context using the internal XPath engine
  68      * @param contextItem The XPath context
  69      * @param xpath The internal XPath engine
  70      * @return an XObject
  71      * @throws javax.xml.transform.TransformerException If the expression cannot be evaluated.
  72      */
  73     XObject eval(Object contextItem, com.sun.org.apache.xpath.internal.XPath xpath)
  74             throws javax.xml.transform.TransformerException {
  75         com.sun.org.apache.xpath.internal.XPathContext xpathSupport;






  76         if (functionResolver != null) {
  77             JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
  78                     functionResolver, featureSecureProcessing, featureManager);
  79             xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(jep);
  80         } else {
  81             xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();
  82         }
  83 
  84         xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
  85         XObject xobj;
  86 
  87         Node contextNode = (Node)contextItem;
  88         // We always need to have a ContextNode with Xalan XPath implementation
  89         // To allow simple expression evaluation like 1+1 we are setting
  90         // dummy Document as Context Node
  91         if (contextNode == null) {
  92             xobj = xpath.execute(xpathSupport, DTM.NULL, prefixResolver);
  93         } else {
  94             xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);
  95         }




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.org.apache.xpath.internal.jaxp;
  27 
  28 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  29 import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
  30 import com.sun.org.apache.xml.internal.dtm.DTM;
  31 import com.sun.org.apache.xpath.internal.axes.LocPathIterator;
  32 import com.sun.org.apache.xpath.internal.objects.XObject;
  33 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  34 import java.io.IOException;
  35 import javax.xml.namespace.QName;
  36 import javax.xml.parsers.DocumentBuilderFactory;
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.transform.TransformerException;
  39 import javax.xml.xpath.XPathConstants;
  40 import javax.xml.xpath.XPathEvaluationResult;
  41 import javax.xml.xpath.XPathExpressionException;
  42 import javax.xml.xpath.XPathFunctionResolver;
  43 import javax.xml.xpath.XPathNodes;
  44 import javax.xml.xpath.XPathVariableResolver;
  45 import jdk.xml.internal.JdkXmlFeatures;
  46 import org.w3c.dom.Document;
  47 import org.w3c.dom.Node;
  48 import org.w3c.dom.traversal.NodeIterator;
  49 import org.xml.sax.InputSource;
  50 import org.xml.sax.SAXException;
  51 


  57     XPathFunctionResolver functionResolver;
  58     XPathVariableResolver variableResolver;
  59     JAXPPrefixResolver prefixResolver;
  60     boolean useServiceMechanism = true;
  61     // By default Extension Functions are allowed in XPath Expressions. If
  62     // Secure Processing Feature is set on XPathFactory then the invocation of
  63     // extensions function need to throw XPathFunctionException
  64     boolean featureSecureProcessing = false;
  65     JdkXmlFeatures featureManager;
  66 
  67     /**
  68      * Evaluate an XPath context using the internal XPath engine
  69      * @param contextItem The XPath context
  70      * @param xpath The internal XPath engine
  71      * @return an XObject
  72      * @throws javax.xml.transform.TransformerException If the expression cannot be evaluated.
  73      */
  74     XObject eval(Object contextItem, com.sun.org.apache.xpath.internal.XPath xpath)
  75             throws javax.xml.transform.TransformerException {
  76         com.sun.org.apache.xpath.internal.XPathContext xpathSupport;
  77         if (contextItem == null && xpath.getExpression() instanceof LocPathIterator) {
  78             // the operation must have no dependency on the context that is null
  79             throw new TransformerException(XSLMessages.createXPATHMessage(
  80                     XPATHErrorResources.ER_CONTEXT_CAN_NOT_BE_NULL,
  81                     new Object[] {}));
  82         }
  83         if (functionResolver != null) {
  84             JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
  85                     functionResolver, featureSecureProcessing, featureManager);
  86             xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(jep);
  87         } else {
  88             xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();
  89         }
  90 
  91         xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
  92         XObject xobj;
  93 
  94         Node contextNode = (Node)contextItem;
  95         // We always need to have a ContextNode with Xalan XPath implementation
  96         // To allow simple expression evaluation like 1+1 we are setting
  97         // dummy Document as Context Node
  98         if (contextNode == null) {
  99             xobj = xpath.execute(xpathSupport, DTM.NULL, prefixResolver);
 100         } else {
 101             xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);
 102         }