Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathEvaluationResult.java
40948 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.xml.xpath;2526import java.util.Objects;27import javax.xml.namespace.QName;28import org.w3c.dom.Node;29/**30* The {@code XPathEvaluationResult} interface represents the result of the31* evaluation of an XPath expression within the context of a particular node.32* The evaluation of an XPath expression can result in various result types as33* defined in XML Path Language (XPath) Version 1.0.34*35* @param <T> the object type returned by the XPath evaluation.36* @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version37* 1.0</a>38*39* @since 940*/41public interface XPathEvaluationResult<T> {4243/**44* XPathResultType represents possible return types of an XPath evaluation.45* Provided as an enum type, it allows the use of switch statement. At the46* same time, a mapping is provided between the original QName types in47* {@link XPathConstants} and class types used in the generic methods.48*/49public static enum XPathResultType {50/**51* Any type that represents any of the 5 other types listed below.52* Maps to {@link XPathEvaluationResult}.53*/54ANY(new QName("http://www.w3.org/1999/XSL/Transform", "any"), XPathEvaluationResult.class),55/**56* The XPath 1.0 boolean data type. Maps to Java {@link Boolean}.57*/58BOOLEAN(XPathConstants.BOOLEAN, Boolean.class),59/**60* The XPath 1.0 Number data type. Maps to Java {@link Number}. Of the61* subtypes of Number, only Double, Integer and Long are required.62*/63NUMBER(XPathConstants.NUMBER, Number.class),64/**65* The XPath 1.0 String data type. Maps to Java {@link String}.66*/67STRING(XPathConstants.STRING, String.class),68/**69* The XPath 1.0 NodeSet data type. Maps to {@link XPathNodes}.70*/71NODESET(XPathConstants.NODESET, XPathNodes.class),72/**73* The XPath 1.0 Node data type. Maps to {@link org.w3c.dom.Node}.74*/75NODE(XPathConstants.NODE, Node.class);7677final QName qnameType;78final Class<?> clsType;79XPathResultType(QName qnameType, Class<?> clsType) {80this.qnameType = qnameType;81this.clsType = clsType;82}8384/**85* Compares this type to the specified class type.86* @param clsType class type87* @return true if the argument is not null and is a class type or accepted subtype that88* matches that this type represents, false otherwise.89*/90private boolean equalsClassType(Class<?> clsType) {91if (Objects.nonNull(clsType) && this.clsType.isAssignableFrom(clsType)) {92if (this.clsType == Number.class) {93return isAcceptedNumberSubType(clsType);94}95return true;96}97return false;98}99100/**101* Compares the specified class type to accepted subtypes of number.102* @param clsType class type103* @return true if class type is an accepted subtype of Number, false otherwise104*/105private boolean isAcceptedNumberSubType(Class<?> clsType) {106return clsType.isAssignableFrom(Double.class) ||107clsType.isAssignableFrom(Integer.class) ||108clsType.isAssignableFrom(Long.class);109}110111/**112* Returns the QName type as specified in {@link XPathConstants} that113* corresponds to the specified class type.114* @param clsType a class type that the enum type supports115* @return the QName type that matches with the specified class type,116* null if there is no match117*/118static public QName getQNameType(Class<?> clsType) {119for (XPathResultType type : XPathResultType.values()) {120if (type.equalsClassType(clsType)) {121return type.qnameType;122}123}124return null;125}126}127128/**129* Return the result type as an enum specified by {@code XPathResultType}130* @return the result type131*/132public XPathResultType type();133134/**135* Returns the value of the result as the type {@code <T>} specified for the class.136*137* @return The value of the result.138*/139public T value();140141}142143144