jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathEvaluationResult.java
author joehw
Tue, 27 Jan 2015 22:01:46 -0800
changeset 28695 427254b89b9e
child 29999 8493f5fc1052
permissions -rw-r--r--
8054196: XPath: support any type Reviewed-by: alanb, lancea, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28695
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     1
/*
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
     2
 * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
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
 * <p>
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    36
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    37
 * @param <T> the object type returned by the XPath evaluation.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    38
 * @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    39
 * 1.0</a>
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    40
 *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    41
 * @since 1.9
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    42
 */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    43
public interface XPathEvaluationResult<T> {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    44
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    45
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    46
     * XPathResultType represents possible return types of an XPath evaluation.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    47
     * Provided as an enum type, it allows the use of switch statement. At the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    48
     * same time, a mapping is provided between the original QName types in
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    49
     * {@link XPathConstants} and class types used in the generic methods.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    50
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    51
    public static enum XPathResultType {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    52
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    53
         * Any type that represents any of the 5 other types listed below.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    54
         * Maps to {@link XPathEvaluationResult}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    55
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    56
        ANY(new QName("http://www.w3.org/1999/XSL/Transform", "any"), XPathEvaluationResult.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    57
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    58
         * The XPath 1.0 boolean data type. Maps to Java {@link Boolean}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    59
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    60
        BOOLEAN(XPathConstants.BOOLEAN, Boolean.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    61
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    62
         * The XPath 1.0 Number data type. Maps to Java {@link Number}. Of the
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    63
         * subtypes of Number, only Double, Integer and Long are required.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    64
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    65
        NUMBER(XPathConstants.NUMBER, Number.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    66
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    67
         * The XPath 1.0 String data type. Maps to Java {@link String}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    68
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    69
        STRING(XPathConstants.STRING, String.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    70
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    71
         * The XPath 1.0 NodeSet data type. Maps to {@link org.w3c.dom.NodeList}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    72
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    73
        NODESET(XPathConstants.NODESET, XPathNodes.class),
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    74
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    75
         * The XPath 1.0 NodeSet data type. Maps to {@link org.w3c.dom.Node}.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    76
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    77
        NODE(XPathConstants.NODE, Node.class);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    78
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    79
        final QName qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    80
        final Class<?> clsType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    81
        XPathResultType(QName qnameType, Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    82
            this.qnameType = qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    83
            this.clsType = clsType;
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
        /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    87
         * Compares this type to the specified class type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    88
         * @param clsType class type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    89
         * @return true if the argument is not null and is a class type that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    90
         * matches that this type represents, false otherwise.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    91
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    92
        private boolean equalsClassType(Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    93
            Objects.nonNull(clsType);
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    94
            if (clsType.isAssignableFrom(this.clsType)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    95
                return true;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    96
            }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
    97
            return false;
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
         * Returns the QName type as specified in {@link XPathConstants} that
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   102
         * corresponds to the specified class type.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   103
         * @param clsType a class type that the enum type supports
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   104
         * @return the QName type that matches with the specified class type,
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   105
         * null if there is no match
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   106
         */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   107
        static public QName getQNameType(Class<?> clsType) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   108
            for (XPathResultType type : XPathResultType.values()) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   109
                if (type.equalsClassType(clsType)) {
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   110
                    return type.qnameType;
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   111
                }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   112
            }
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   113
            return null;
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
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   118
     * Return the result type as an enum specified by {@code XPathResultType}
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   119
     * @return the result type
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   120
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   121
    public XPathResultType type();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   122
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   123
    /**
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   124
     * Returns the value of the result as the type &lt;T&gt; specified for the class.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   125
     *
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   126
     * @return The value of the result.
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   127
     */
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   128
    public T value();
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   129
427254b89b9e 8054196: XPath: support any type
joehw
parents:
diff changeset
   130
}