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