src/java.xml/share/classes/javax/xml/xpath/XPathEvaluationResult.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 35337 jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathEvaluationResult.java@2ba279bcff39
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     1
/*
30522
59a16afe7517 8080344: Incorrect GPL header causes RE script to miss swap to commercial header for licensee source bundle
joehw
parents: 29999
diff changeset
     2
 * Copyright (c) 2015, 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
package javax.xml.xpath;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    26
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    27
import java.util.Objects;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    28
import javax.xml.namespace.QName;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    29
import org.w3c.dom.Node;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    30
/**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    31
 * The {@code XPathEvaluationResult} interface represents the result of the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    32
 * evaluation of an XPath expression within the context of a particular node.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    33
 * The evaluation of an XPath expression can result in various result types as
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    34
 * defined in XML Path Language (XPath) Version 1.0.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    35
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    36
 * @param <T> the object type returned by the XPath evaluation.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    37
 * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    38
 * 1.0</a>
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    39
 *
35337
2ba279bcff39 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30522
diff changeset
    40
 * @since 9
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    41
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    42
public interface XPathEvaluationResult<T> {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    43
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    44
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    45
     * XPathResultType represents possible return types of an XPath evaluation.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    46
     * Provided as an enum type, it allows the use of switch statement. At the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    47
     * same time, a mapping is provided between the original QName types in
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    48
     * {@link XPathConstants} and class types used in the generic methods.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    49
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    50
    public static enum XPathResultType {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    51
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    52
         * Any type that represents any of the 5 other types listed below.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    53
         * Maps to {@link XPathEvaluationResult}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    54
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    55
        ANY(new QName("http://www.w3.org/1999/XSL/Transform", "any"), XPathEvaluationResult.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    56
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    57
         * The XPath 1.0 boolean data type. Maps to Java {@link Boolean}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    58
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    59
        BOOLEAN(XPathConstants.BOOLEAN, Boolean.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    60
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    61
         * The XPath 1.0 Number data type. Maps to Java {@link Number}. Of the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    62
         * subtypes of Number, only Double, Integer and Long are required.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    63
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    64
        NUMBER(XPathConstants.NUMBER, Number.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    65
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    66
         * The XPath 1.0 String data type. Maps to Java {@link String}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    67
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    68
        STRING(XPathConstants.STRING, String.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    69
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    70
         * The XPath 1.0 NodeSet data type. Maps to {@link org.w3c.dom.NodeList}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    71
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    72
        NODESET(XPathConstants.NODESET, XPathNodes.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    73
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    74
         * The XPath 1.0 NodeSet data type. Maps to {@link org.w3c.dom.Node}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    75
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    76
        NODE(XPathConstants.NODE, Node.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    77
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    78
        final QName qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    79
        final Class<?> clsType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    80
        XPathResultType(QName qnameType, Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    81
            this.qnameType = qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    82
            this.clsType = clsType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    83
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    84
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    85
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    86
         * Compares this type to the specified class type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    87
         * @param clsType class type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    88
         * @return true if the argument is not null and is a class type that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    89
         * matches that this type represents, false otherwise.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    90
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    91
        private boolean equalsClassType(Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    92
            Objects.nonNull(clsType);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    93
            if (clsType.isAssignableFrom(this.clsType)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    94
                return true;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    95
            }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    96
            return false;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    97
        }
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
         * Returns the QName type as specified in {@link XPathConstants} that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   101
         * corresponds to the specified class type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   102
         * @param clsType a class type that the enum type supports
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   103
         * @return the QName type that matches with the specified class type,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   104
         * null if there is no match
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   105
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   106
        static public QName getQNameType(Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   107
            for (XPathResultType type : XPathResultType.values()) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   108
                if (type.equalsClassType(clsType)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   109
                    return type.qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   110
                }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   111
            }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   112
            return null;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   113
        }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   114
    }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   115
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   116
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   117
     * Return the result type as an enum specified by {@code XPathResultType}
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   118
     * @return the result type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   119
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   120
    public XPathResultType type();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   121
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   122
    /**
29999
8493f5fc1052 8077332: tidy warnings from javax/xml
avstepan
parents: 28695
diff changeset
   123
     * Returns the value of the result as the type {@code <T>} specified for the class.
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   124
     *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   125
     * @return The value of the result.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   126
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   127
    public T value();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   128
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   129
}