Debian Codegraph @

net/sf/saxon/type/JavaExternalObjectType.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.type;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.StructuredQName;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.value.ObjectValue;
16
17 /**
18 * This class represents the type of an external Java object returned by
19 * an extension function, or supplied as an external variable/parameter.
20 */
21
22 public class JavaExternalObjectType extends ExternalObjectType {
23
24 protected Configuration config;
25 protected Class<?> javaClass;
26
27 /**
28 * Create an external object type.
29 *
30 * @param javaClass the Java class to which this type corresponds
31 */
32
33 public JavaExternalObjectType(Configuration config, Class<?> javaClass) {
34 this.config = config;
35 this.javaClass = javaClass;
36 }
37
38 public Configuration getConfiguration() {
39 return config;
40 }
41
42 /**
43 * Get the local name of this type.
44 *
45 * @return the fully qualified name of the Java class.
46 */
47
48 /*@Nullable*/
49 public String getName() {
50 return javaClass.getName();
51 }
52
53 /**
54 * Get the target namespace of this type. The is always NamespaceConstant.JAVA_TYPE.
55 *
56 * @return the target namespace of this type definition.
57 */
58
59 /*@Nullable*/
60 public String getTargetNamespace() {
61 return NamespaceConstant.JAVA_TYPE;
62 }
63
64 /**
65 * Get the name of this type as a StructuredQName, unless the type is anonymous, in which case
66 * return null
67 *
68 * @return the name of the atomic type, or null if the type is anonymous.
69 */
70
71 /*@Nullable*/
72 public StructuredQName getTypeName() {
73 return classNameToQName(javaClass.getName());
74 }
75
76 /**
77 * Get the primitive item type corresponding to this item type.
78 *
79 * @return EXTERNAL_OBJECT_TYPE, the ExternalObjectType that encapsulates
80 * the Java type Object.class.
81 */
82
83 /*@NotNull*/
84 public ItemType getPrimitiveItemType() {
85 return config.getJavaExternalObjectType(Object.class);
86 }
87
88 /**
89 * Get the relationship of this external object type to another external object type
90 *
91 * @param other the other external object type
92 * @return the relationship of this external object type to another external object type,
93 * as one of the constants in class {@link TypeHierarchy}, for example {@link TypeHierarchy#SUBSUMES}
94 */
95
96 public int getRelationship(/*@NotNull*/ JavaExternalObjectType other) {
97 Class<?> j2 = other.javaClass;
98 if (javaClass.equals(j2)) {
99 return TypeHierarchy.SAME_TYPE;
100 } else if (javaClass.isAssignableFrom(j2)) {
101 return TypeHierarchy.SUBSUMES;
102 } else if (j2.isAssignableFrom(javaClass)) {
103 return TypeHierarchy.SUBSUMED_BY;
104 } else if (javaClass.isInterface() || j2.isInterface()) {
105 return TypeHierarchy.OVERLAPS; // there may be an overlap, we play safe
106 } else {
107 return TypeHierarchy.DISJOINT;
108 }
109 }
110
111 /**
112 * Get the Java class to which this external object type corresponds
113 *
114 * @return the corresponding Java class
115 */
116
117 public Class<?> getJavaClass() {
118 return javaClass;
119 }
120
121 /**
122 * Test whether a given item conforms to this type
123 * @param item The item to be tested
124 * @param th The type hierarchy cache
125 * @return true if the item is an instance of this type; false otherwise
126 */
127 public boolean matches(/*@NotNull*/ Item item, /*@NotNull*/TypeHierarchy th) {
128 if (item instanceof ObjectValue) {
129 Object obj = ((ObjectValue) item).getObject();
130 return javaClass.isAssignableFrom(obj.getClass());
131 }
132 return false;
133 }
134
135 /*@NotNull*/
136 public String toString() {
137 return classNameToQName(javaClass.getName()).getEQName();
138 }
139
140 /*@NotNull*/
141 public String getDisplayName() {
142 return "java-type:" + javaClass.getName();
143 }
144
145 /**
146 * Returns a hash code value for the object.
147 */
148
149 public int hashCode() {
150 return javaClass.hashCode();
151 }
152
153 /**
154 * Test whether two ExternalObjectType objects represent the same type
155 *
156 * @param obj the other ExternalObjectType
157 * @return true if the two objects represent the same type
158 */
159
160 public boolean equals(/*@NotNull*/ Object obj) {
161 return obj instanceof JavaExternalObjectType && javaClass == ((JavaExternalObjectType) obj).javaClass;
162 }
163
164 /**
165 * Static method to convert a Java class name to an XPath local name. This involves the
166 * following substitutions: "$" is replaced by "-", and "[" is replaced by "_-".
167 */
168
169 public static String classNameToLocalName(String className) {
170 return className.replace('$', '-').replace("[", "_-");
171 }
172
173 /**
174 * Static method to convert an XPath local name to a Java class name. This involves the
175 * following substitutions: "-" is replaced by "$", and leading "_-" pairs are replaced by "[".
176 */
177
178 public static String localNameToClassName(String className) {
179 FastStringBuffer fsb = new FastStringBuffer(className.length());
180 boolean atStart = true;
181 for (int i=0; i<className.length(); i++) {
182 char c = className.charAt(i);
183 if (atStart) {
184 if (c == '_' && i+1 < className.length() && className.charAt(i+1) == '-') {
185 fsb.append('[');
186 i++;
187 } else {
188 atStart = false;
189 fsb.append(c == '-' ? '$' : c);
190 }
191 } else {
192 fsb.append(c == '-' ? '$' : c);
193 }
194 }
195 return fsb.toString();
196 }
197
198 /**
199 * Static method to get the QName corresponding to a Java class name
200 */
201
202 public static StructuredQName classNameToQName(String className) {
203 return new StructuredQName("jt", NamespaceConstant.JAVA_TYPE, classNameToLocalName(className));
204 }
205
206 }