jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathImplUtil.java
author joehw
Thu, 28 Jul 2016 22:58:41 -0700
changeset 39907 db51759e3695
parent 28695 427254b89b9e
child 40483 cb7f243e782d
permissions -rw-r--r--
8158084: Catalog API: JAXP XML Processor Support Reviewed-by: lancea, clanger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     1
/*
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 28695
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     4
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    10
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    15
 * accompanied this code).
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    16
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    20
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    23
 * questions.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    24
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    25
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    26
package com.sun.org.apache.xpath.internal.jaxp;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    27
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    28
import com.sun.org.apache.xalan.internal.res.XSLMessages;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    29
import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    30
import com.sun.org.apache.xml.internal.dtm.DTM;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    31
import com.sun.org.apache.xpath.internal.objects.XObject;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    32
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    33
import java.io.IOException;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    34
import javax.xml.namespace.QName;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    35
import javax.xml.parsers.DocumentBuilderFactory;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    36
import javax.xml.parsers.ParserConfigurationException;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    37
import javax.xml.transform.TransformerException;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    38
import javax.xml.xpath.XPathConstants;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    39
import javax.xml.xpath.XPathEvaluationResult;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    40
import javax.xml.xpath.XPathExpressionException;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    41
import javax.xml.xpath.XPathFunctionResolver;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    42
import javax.xml.xpath.XPathNodes;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    43
import javax.xml.xpath.XPathVariableResolver;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 28695
diff changeset
    44
import jdk.xml.internal.JdkXmlFeatures;
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    45
import org.w3c.dom.Document;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    46
import org.w3c.dom.Node;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    47
import org.w3c.dom.traversal.NodeIterator;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    48
import org.xml.sax.InputSource;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    49
import org.xml.sax.SAXException;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    50
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    51
/**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    52
 * This class contains several utility methods used by XPathImpl and
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    53
 * XPathExpressionImpl
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    54
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    55
class XPathImplUtil {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    56
    XPathFunctionResolver functionResolver;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    57
    XPathVariableResolver variableResolver;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    58
    JAXPPrefixResolver prefixResolver;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    59
    boolean useServiceMechanism = true;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    60
    // By default Extension Functions are allowed in XPath Expressions. If
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    61
    // Secure Processing Feature is set on XPathFactory then the invocation of
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    62
    // extensions function need to throw XPathFunctionException
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    63
    boolean featureSecureProcessing = false;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 28695
diff changeset
    64
    JdkXmlFeatures featureManager;
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    65
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    66
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    67
     * Evaluate an XPath context using the internal XPath engine
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    68
     * @param contextItem The XPath context
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    69
     * @param xpath The internal XPath engine
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    70
     * @return an XObject
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    71
     * @throws javax.xml.transform.TransformerException If the expression cannot be evaluated.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    72
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    73
    XObject eval(Object contextItem, com.sun.org.apache.xpath.internal.XPath xpath)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    74
            throws javax.xml.transform.TransformerException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    75
        com.sun.org.apache.xpath.internal.XPathContext xpathSupport;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    76
        if (functionResolver != null) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    77
            JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    78
                    functionResolver, featureSecureProcessing, featureManager);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    79
            xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext(jep);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    80
        } else {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    81
            xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    82
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    83
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    84
        xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    85
        XObject xobj;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    86
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    87
        Node contextNode = (Node)contextItem;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    88
        // We always need to have a ContextNode with Xalan XPath implementation
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    89
        // To allow simple expression evaluation like 1+1 we are setting
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    90
        // dummy Document as Context Node
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    91
        if (contextNode == null) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    92
            xobj = xpath.execute(xpathSupport, DTM.NULL, prefixResolver);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    93
        } else {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    94
            xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    95
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    96
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    97
        return xobj;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    98
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    99
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   100
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   101
     * Parse the input source and return a Document.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   102
     * @param source The {@code InputSource} of the document
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   103
     * @return a DOM Document
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   104
     * @throws XPathExpressionException if there is an error parsing the source.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   105
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   106
    Document getDocument(InputSource source)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   107
        throws XPathExpressionException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   108
        requireNonNull(source, "Source");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   109
        try {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   110
            // we'd really like to cache those DocumentBuilders, but we can't because:
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   111
            // 1. thread safety. parsers are not thread-safe, so at least
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   112
            //    we need one instance per a thread.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   113
            // 2. parsers are non-reentrant, so now we are looking at having a
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   114
            // pool of parsers.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   115
            // 3. then the class loading issue. The look-up procedure of
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   116
            //    DocumentBuilderFactory.newInstance() depends on context class loader
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   117
            //    and system properties, which may change during the execution of JVM.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   118
            //
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   119
            // so we really have to create a fresh DocumentBuilder every time we need one
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   120
            // - KK
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   121
            DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(useServiceMechanism);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   122
            dbf.setNamespaceAware(true);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   123
            dbf.setValidating(false);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   124
            return dbf.newDocumentBuilder().parse(source);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   125
        } catch (ParserConfigurationException | SAXException | IOException e) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   126
            throw new XPathExpressionException (e);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   127
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   128
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   129
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   130
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   131
     * Get result depending on the QName type defined in XPathConstants
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   132
     * @param resultObject the result of an evaluation
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   133
     * @param returnType the return type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   134
     * @return result per the return type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   135
     * @throws TransformerException if the result can not be converted to
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   136
     * the specified return type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   137
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   138
    Object getResultAsType(XObject resultObject, QName returnType)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   139
        throws TransformerException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   140
        // XPathConstants.STRING
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   141
        if (returnType.equals(XPathConstants.STRING)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   142
            return resultObject.str();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   143
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   144
        // XPathConstants.NUMBER
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   145
        if (returnType.equals(XPathConstants.NUMBER)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   146
            return resultObject.num();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   147
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   148
        // XPathConstants.BOOLEAN
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   149
        if (returnType.equals(XPathConstants.BOOLEAN)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   150
            return resultObject.bool();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   151
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   152
        // XPathConstants.NODESET ---ORdered, UNOrdered???
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   153
        if (returnType.equals(XPathConstants.NODESET)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   154
            return resultObject.nodelist();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   155
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   156
        // XPathConstants.NODE
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   157
        if (returnType.equals(XPathConstants.NODE)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   158
            NodeIterator ni = resultObject.nodeset();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   159
            //Return the first node, or null
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   160
            return ni.nextNode();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   161
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   162
        // If isSupported check is already done then the execution path
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   163
        // shouldn't come here. Being defensive
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   164
        String fmsg = XSLMessages.createXPATHMessage(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   165
                XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   166
                new Object[] { returnType.toString()});
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   167
        throw new IllegalArgumentException (fmsg);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   168
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   169
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   170
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   171
     * Construct an XPathExpressionResult object based on the result of the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   172
     * evaluation and cast to the specified class type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   173
     * @param <T> The class type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   174
     * @param resultObject the result of an evaluation
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   175
     * @param type The class type expected to be returned by the XPath expression.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   176
     * @return an instance of the specified type or null if the XObject returned
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   177
     * an UNKNOWN object type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   178
     * @throws TransformerException if there is an error converting the result
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   179
     * to the specified type. It's unlikely to happen. This is mostly needed
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   180
     * by the internal XPath engine.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   181
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   182
    <T> T getXPathResult(XObject resultObject, Class<T> type)
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   183
            throws TransformerException {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   184
        int resultType = resultObject.getType();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   185
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   186
        switch (resultType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   187
            case XObject.CLASS_BOOLEAN :
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   188
                return type.cast(new XPathResultImpl<>(resultObject, Boolean.class));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   189
            case XObject.CLASS_NUMBER :
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   190
                return type.cast(new XPathResultImpl<>(resultObject, Double.class));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   191
            case XObject.CLASS_STRING :
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   192
                return type.cast(new XPathResultImpl<>(resultObject, String.class));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   193
            case XObject.CLASS_NODESET :
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   194
                return type.cast(new XPathResultImpl<>(resultObject, XPathNodes.class));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   195
            case XObject.CLASS_RTREEFRAG :  //NODE
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   196
                return type.cast(new XPathResultImpl<>(resultObject, Node.class));
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   197
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   198
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   199
        return null;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   200
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   201
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   202
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   203
     * Check whether or not the specified type is supported
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   204
     * @param <T> The class type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   205
     * @param type The type to be checked
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   206
     * @throws IllegalArgumentException if the type is not supported
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   207
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   208
    <T> void isSupportedClassType(Class<T> type) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   209
        requireNonNull(type, "The class type");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   210
        if (type.isAssignableFrom(Boolean.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   211
                type.isAssignableFrom(Double.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   212
                type.isAssignableFrom(Integer.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   213
                type.isAssignableFrom(Long.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   214
                type.isAssignableFrom(String.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   215
                type.isAssignableFrom(XPathNodes.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   216
                type.isAssignableFrom(Node.class) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   217
                type.isAssignableFrom(XPathEvaluationResult.class)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   218
            return;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   219
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   220
        String fmsg = XSLMessages.createXPATHMessage(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   221
                XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   222
                new Object[] { type.toString() });
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   223
        throw new IllegalArgumentException (fmsg);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   224
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   225
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   226
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   227
     * Check if the requested returnType is supported.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   228
     * @param returnType the return type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   229
     * @throws IllegalArgumentException if the return type is not supported
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   230
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   231
    void isSupported(QName returnType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   232
        requireNonNull(returnType, "returnType");
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   233
        if (returnType.equals(XPathConstants.STRING) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   234
                returnType.equals(XPathConstants.NUMBER) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   235
                returnType.equals(XPathConstants.BOOLEAN) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   236
                returnType.equals(XPathConstants.NODE) ||
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   237
                returnType.equals(XPathConstants.NODESET)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   238
            return;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   239
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   240
        String fmsg = XSLMessages.createXPATHMessage(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   241
                XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   242
                new Object[] { returnType.toString() });
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   243
        throw new IllegalArgumentException (fmsg);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   244
     }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   245
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   246
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   247
     * Checks that the specified parameter is not {@code null}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   248
     *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   249
     * @param <T> the type of the reference
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   250
     * @param param the parameter to check for nullity
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   251
     * @param paramName the parameter name
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   252
     * @throws NullPointerException if {@code param} is {@code null}
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   253
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   254
    <T> void requireNonNull(T param, String paramName) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   255
        if (param == null) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   256
            String fmsg = XSLMessages.createXPATHMessage(
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   257
                    XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   258
                    new Object[] {paramName});
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   259
            throw new NullPointerException (fmsg);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   260
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   261
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   262
}