1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xpath.internal.jaxp;
  22 
  23 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
  24 import com.sun.org.apache.xpath.internal.objects.XObject;
  25 import javax.xml.namespace.QName;
  26 import javax.xml.transform.TransformerException;
  27 import javax.xml.xpath.XPathConstants;
  28 import javax.xml.xpath.XPathEvaluationResult;
  29 import javax.xml.xpath.XPathExpression;
  30 import javax.xml.xpath.XPathExpressionException;
  31 import javax.xml.xpath.XPathFunctionResolver;
  32 import javax.xml.xpath.XPathVariableResolver;
  33 import org.w3c.dom.Document;
  34 import org.w3c.dom.Node;
  35 import org.xml.sax.InputSource;
  36 
  37 /**
  38  * The XPathExpression interface encapsulates a (compiled) XPath expression.
  39  *
  40  * @author  Ramesh Mandava
  41  */
  42 public class XPathExpressionImpl extends XPathImplUtil implements XPathExpression {
  43 
  44     private com.sun.org.apache.xpath.internal.XPath xpath;
  45 
  46     /** Protected constructor to prevent direct instantiation; use compile()
  47      * from the context.
  48      */
  49     protected XPathExpressionImpl() {
  50         this(null, null, null, null,
  51              false, true, new FeatureManager());
  52     };
  53 
  54     protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath,
  55             JAXPPrefixResolver prefixResolver,
  56             XPathFunctionResolver functionResolver,
  57             XPathVariableResolver variableResolver) {
  58         this(xpath, prefixResolver, functionResolver, variableResolver,
  59              false, true, new FeatureManager());
  60     };
  61 
  62     protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath,
  63             JAXPPrefixResolver prefixResolver,XPathFunctionResolver functionResolver,
  64             XPathVariableResolver variableResolver, boolean featureSecureProcessing,
  65             boolean useServiceMechanism, FeatureManager featureManager) {
  66         this.xpath = xpath;
  67         this.prefixResolver = prefixResolver;
  68         this.functionResolver = functionResolver;
  69         this.variableResolver = variableResolver;
  70         this.featureSecureProcessing = featureSecureProcessing;
  71         this.useServiceMechanism = useServiceMechanism;
  72         this.featureManager = featureManager;
  73     };
  74 
  75     public void setXPath (com.sun.org.apache.xpath.internal.XPath xpath) {
  76         this.xpath = xpath;
  77     }
  78 
  79     public Object eval(Object item, QName returnType)
  80             throws javax.xml.transform.TransformerException {
  81         XObject resultObject = eval(item, xpath);
  82         return getResultAsType(resultObject, returnType);
  83     }
  84 
  85     @Override
  86     public Object evaluate(Object item, QName returnType)
  87         throws XPathExpressionException {
  88         isSupported(returnType);
  89         try {
  90             return eval(item, returnType);
  91         } catch (java.lang.NullPointerException npe) {
  92             // If VariableResolver returns null Or if we get
  93             // NullPointerException at this stage for some other reason
  94             // then we have to reurn XPathException
  95             throw new XPathExpressionException (npe);
  96         } catch (javax.xml.transform.TransformerException te) {
  97             Throwable nestedException = te.getException();
  98             if (nestedException instanceof javax.xml.xpath.XPathFunctionException) {
  99                 throw (javax.xml.xpath.XPathFunctionException)nestedException;
 100             } else {
 101                 // For any other exceptions we need to throw
 102                 // XPathExpressionException (as per spec)
 103                 throw new XPathExpressionException(te);
 104             }
 105         }
 106 
 107     }
 108 
 109     @Override
 110     public String evaluate(Object item)
 111         throws XPathExpressionException {
 112         return (String)this.evaluate(item, XPathConstants.STRING);
 113     }
 114 
 115     @Override
 116     public Object evaluate(InputSource source, QName returnType)
 117         throws XPathExpressionException {
 118         isSupported (returnType);
 119         try {
 120             Document document = getDocument(source);
 121             return eval(document, returnType);
 122         } catch (TransformerException e) {
 123             throw new XPathExpressionException(e);
 124         }
 125     }
 126 
 127     @Override
 128     public String evaluate(InputSource source)
 129         throws XPathExpressionException {
 130         return (String)this.evaluate(source, XPathConstants.STRING);
 131     }
 132 
 133     @Override
 134     public <T>T evaluateExpression(Object item, Class<T> type)
 135         throws XPathExpressionException {
 136         isSupportedClassType(type);
 137 
 138         try {
 139             XObject resultObject = eval(item, xpath);
 140             if (type.isAssignableFrom(XPathEvaluationResult.class)) {
 141                 XPathEvaluationResult<?> x = getXPathResult(resultObject);
 142 
 143                 return (T)x;
 144             } else {
 145                 return XPathResultImpl.getValue(resultObject, type);
 146             }
 147 
 148         } catch (javax.xml.transform.TransformerException te) {
 149             throw new XPathExpressionException(te);
 150         }
 151     }
 152 
 153     @Override
 154     public XPathEvaluationResult<?> evaluateExpression(Object item)
 155         throws XPathExpressionException {
 156         return evaluateExpression(item, XPathEvaluationResult.class);
 157     }
 158 
 159     @Override
 160     public <T>T  evaluateExpression(InputSource source, Class<T> type)
 161             throws XPathExpressionException {
 162         Document document = getDocument(source);
 163         return evaluateExpression(document, type);
 164     }
 165 
 166     @Override
 167     public XPathEvaluationResult evaluateExpression(InputSource source)
 168         throws XPathExpressionException {
 169         return evaluateExpression(source, XPathEvaluationResult.class);
 170     }
 171  }