Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/XPathEvaluationResult.java
40948 views
1
/*
2
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package javax.xml.xpath;
26
27
import java.util.Objects;
28
import javax.xml.namespace.QName;
29
import org.w3c.dom.Node;
30
/**
31
* The {@code XPathEvaluationResult} interface represents the result of the
32
* evaluation of an XPath expression within the context of a particular node.
33
* The evaluation of an XPath expression can result in various result types as
34
* defined in XML Path Language (XPath) Version 1.0.
35
*
36
* @param <T> the object type returned by the XPath evaluation.
37
* @see <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version
38
* 1.0</a>
39
*
40
* @since 9
41
*/
42
public interface XPathEvaluationResult<T> {
43
44
/**
45
* XPathResultType represents possible return types of an XPath evaluation.
46
* Provided as an enum type, it allows the use of switch statement. At the
47
* same time, a mapping is provided between the original QName types in
48
* {@link XPathConstants} and class types used in the generic methods.
49
*/
50
public static enum XPathResultType {
51
/**
52
* Any type that represents any of the 5 other types listed below.
53
* Maps to {@link XPathEvaluationResult}.
54
*/
55
ANY(new QName("http://www.w3.org/1999/XSL/Transform", "any"), XPathEvaluationResult.class),
56
/**
57
* The XPath 1.0 boolean data type. Maps to Java {@link Boolean}.
58
*/
59
BOOLEAN(XPathConstants.BOOLEAN, Boolean.class),
60
/**
61
* The XPath 1.0 Number data type. Maps to Java {@link Number}. Of the
62
* subtypes of Number, only Double, Integer and Long are required.
63
*/
64
NUMBER(XPathConstants.NUMBER, Number.class),
65
/**
66
* The XPath 1.0 String data type. Maps to Java {@link String}.
67
*/
68
STRING(XPathConstants.STRING, String.class),
69
/**
70
* The XPath 1.0 NodeSet data type. Maps to {@link XPathNodes}.
71
*/
72
NODESET(XPathConstants.NODESET, XPathNodes.class),
73
/**
74
* The XPath 1.0 Node data type. Maps to {@link org.w3c.dom.Node}.
75
*/
76
NODE(XPathConstants.NODE, Node.class);
77
78
final QName qnameType;
79
final Class<?> clsType;
80
XPathResultType(QName qnameType, Class<?> clsType) {
81
this.qnameType = qnameType;
82
this.clsType = clsType;
83
}
84
85
/**
86
* Compares this type to the specified class type.
87
* @param clsType class type
88
* @return true if the argument is not null and is a class type or accepted subtype that
89
* matches that this type represents, false otherwise.
90
*/
91
private boolean equalsClassType(Class<?> clsType) {
92
if (Objects.nonNull(clsType) && this.clsType.isAssignableFrom(clsType)) {
93
if (this.clsType == Number.class) {
94
return isAcceptedNumberSubType(clsType);
95
}
96
return true;
97
}
98
return false;
99
}
100
101
/**
102
* Compares the specified class type to accepted subtypes of number.
103
* @param clsType class type
104
* @return true if class type is an accepted subtype of Number, false otherwise
105
*/
106
private boolean isAcceptedNumberSubType(Class<?> clsType) {
107
return clsType.isAssignableFrom(Double.class) ||
108
clsType.isAssignableFrom(Integer.class) ||
109
clsType.isAssignableFrom(Long.class);
110
}
111
112
/**
113
* Returns the QName type as specified in {@link XPathConstants} that
114
* corresponds to the specified class type.
115
* @param clsType a class type that the enum type supports
116
* @return the QName type that matches with the specified class type,
117
* null if there is no match
118
*/
119
static public QName getQNameType(Class<?> clsType) {
120
for (XPathResultType type : XPathResultType.values()) {
121
if (type.equalsClassType(clsType)) {
122
return type.qnameType;
123
}
124
}
125
return null;
126
}
127
}
128
129
/**
130
* Return the result type as an enum specified by {@code XPathResultType}
131
* @return the result type
132
*/
133
public XPathResultType type();
134
135
/**
136
* Returns the value of the result as the type {@code <T>} specified for the class.
137
*
138
* @return The value of the result.
139
*/
140
public T value();
141
142
}
143
144