| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.lib.ExternalObjectModel; |
| 13 |
import net.sf.saxon.lib.ParseOptions; |
| 14 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 15 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 16 |
import net.sf.saxon.ma.map.MapItem; |
| 17 |
import net.sf.saxon.ma.map.MapType; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 20 |
import net.sf.saxon.pattern.NodeKindTest; |
| 21 |
import net.sf.saxon.s9api.*; |
| 22 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 23 |
import net.sf.saxon.trans.XPathException; |
| 24 |
import net.sf.saxon.type.*; |
| 25 |
import net.sf.saxon.type.ItemType; |
| 26 |
import net.sf.saxon.value.*; |
| 27 |
|
| 28 |
import javax.xml.transform.Source; |
| 29 |
import javax.xml.transform.dom.DOMSource; |
| 30 |
import java.lang.reflect.ParameterizedType; |
| 31 |
import java.math.BigDecimal; |
| 32 |
import java.math.BigInteger; |
| 33 |
import java.net.URI; |
| 34 |
import java.net.URL; |
| 35 |
import java.util.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class JPConverter { |
| 45 |
|
| 46 |
private static HashMap<Class<?>, JPConverter> converterMap = new HashMap<>(); |
| 47 |
|
| 48 |
static { |
| 49 |
converterMap.put(XdmValue.class, new FromXdmValue(AnyItemType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_MORE)); |
| 50 |
converterMap.put(XdmItem.class, new FromXdmValue(AnyItemType.getInstance(), StaticProperty.ALLOWS_ONE)); |
| 51 |
converterMap.put(XdmAtomicValue.class, new FromXdmValue(BuiltInAtomicType.ANY_ATOMIC, StaticProperty.ALLOWS_ONE)); |
| 52 |
converterMap.put(XdmNode.class, new FromXdmValue(AnyNodeTest.getInstance(), StaticProperty.ALLOWS_ONE)); |
| 53 |
converterMap.put(XdmFunctionItem.class, new FromXdmValue(AnyFunctionType.getInstance(), StaticProperty.ALLOWS_ONE)); |
| 54 |
converterMap.put(XdmMap.class, new FromXdmValue(MapType.ANY_MAP_TYPE, StaticProperty.ALLOWS_ONE)); |
| 55 |
converterMap.put(XdmArray.class, new FromXdmValue(ArrayItemType.getInstance(), StaticProperty.ALLOWS_ONE)); |
| 56 |
converterMap.put(XdmEmptySequence.class, new FromXdmValue(ErrorType.getInstance(), StaticProperty.ALLOWS_ZERO)); |
| 57 |
converterMap.put(SequenceIterator.class, FromSequenceIterator.INSTANCE); |
| 58 |
converterMap.put(Sequence.class, FromSequence.INSTANCE); |
| 59 |
converterMap.put(OneOrMore.class, FromSequence.INSTANCE); |
| 60 |
converterMap.put(One.class, FromSequence.INSTANCE); |
| 61 |
converterMap.put(ZeroOrOne.class, FromSequence.INSTANCE); |
| 62 |
converterMap.put(ZeroOrMore.class, FromSequence.INSTANCE); |
| 63 |
converterMap.put(String.class, FromString.INSTANCE); |
| 64 |
converterMap.put(Boolean.class, FromBoolean.INSTANCE); |
| 65 |
converterMap.put(boolean.class, FromBoolean.INSTANCE); |
| 66 |
converterMap.put(Double.class, FromDouble.INSTANCE); |
| 67 |
converterMap.put(double.class, FromDouble.INSTANCE); |
| 68 |
converterMap.put(Float.class, FromFloat.INSTANCE); |
| 69 |
converterMap.put(float.class, FromFloat.INSTANCE); |
| 70 |
converterMap.put(BigDecimal.class, FromBigDecimal.INSTANCE); |
| 71 |
converterMap.put(BigInteger.class, FromBigInteger.INSTANCE); |
| 72 |
converterMap.put(Long.class, FromLong.INSTANCE); |
| 73 |
converterMap.put(long.class, FromLong.INSTANCE); |
| 74 |
converterMap.put(Integer.class, FromInt.INSTANCE); |
| 75 |
converterMap.put(int.class, FromInt.INSTANCE); |
| 76 |
converterMap.put(Short.class, FromShort.INSTANCE); |
| 77 |
converterMap.put(short.class, FromShort.INSTANCE); |
| 78 |
converterMap.put(Byte.class, FromByte.INSTANCE); |
| 79 |
converterMap.put(byte.class, FromByte.INSTANCE); |
| 80 |
converterMap.put(Character.class, FromCharacter.INSTANCE); |
| 81 |
converterMap.put(char.class, FromCharacter.INSTANCE); |
| 82 |
converterMap.put(URI.class, FromURI.INSTANCE); |
| 83 |
converterMap.put(URL.class, FromURI.INSTANCE); |
| 84 |
converterMap.put(Date.class, FromDate.INSTANCE); |
| 85 |
converterMap.put(long[].class, FromLongArray.INSTANCE); |
| 86 |
converterMap.put(int[].class, FromIntArray.INSTANCE); |
| 87 |
converterMap.put(short[].class, FromShortArray.INSTANCE); |
| 88 |
converterMap.put(byte[].class, FromByteArray.INSTANCE); |
| 89 |
converterMap.put(char[].class, FromCharArray.INSTANCE); |
| 90 |
converterMap.put(double[].class, FromDoubleArray.INSTANCE); |
| 91 |
converterMap.put(float[].class, FromFloatArray.INSTANCE); |
| 92 |
converterMap.put(boolean[].class, FromBooleanArray.INSTANCE); |
| 93 |
converterMap.put(Collection.class, FromCollection.INSTANCE); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
private static Map<Class<?>, ItemType> itemTypeMap = new HashMap<>(); |
| 98 |
|
| 99 |
static { |
| 100 |
itemTypeMap.put(BooleanValue.class, BuiltInAtomicType.BOOLEAN); |
| 101 |
itemTypeMap.put(StringValue.class, BuiltInAtomicType.STRING); |
| 102 |
itemTypeMap.put(DoubleValue.class, BuiltInAtomicType.DOUBLE); |
| 103 |
itemTypeMap.put(FloatValue.class, BuiltInAtomicType.FLOAT); |
| 104 |
itemTypeMap.put(BigDecimalValue.class, BuiltInAtomicType.DECIMAL); |
| 105 |
itemTypeMap.put(IntegerValue.class, BuiltInAtomicType.INTEGER); |
| 106 |
itemTypeMap.put(DurationValue.class, BuiltInAtomicType.DURATION); |
| 107 |
itemTypeMap.put(DayTimeDurationValue.class, BuiltInAtomicType.DAY_TIME_DURATION); |
| 108 |
itemTypeMap.put(YearMonthDurationValue.class, BuiltInAtomicType.YEAR_MONTH_DURATION); |
| 109 |
itemTypeMap.put(DateTimeValue.class, BuiltInAtomicType.DATE_TIME); |
| 110 |
itemTypeMap.put(DateValue.class, BuiltInAtomicType.DATE); |
| 111 |
itemTypeMap.put(TimeValue.class, BuiltInAtomicType.TIME); |
| 112 |
itemTypeMap.put(GYearValue.class, BuiltInAtomicType.G_YEAR); |
| 113 |
itemTypeMap.put(GYearMonthValue.class, BuiltInAtomicType.G_YEAR_MONTH); |
| 114 |
itemTypeMap.put(GMonthValue.class, BuiltInAtomicType.G_MONTH); |
| 115 |
itemTypeMap.put(GMonthDayValue.class, BuiltInAtomicType.G_MONTH_DAY); |
| 116 |
itemTypeMap.put(GDayValue.class, BuiltInAtomicType.G_DAY); |
| 117 |
itemTypeMap.put(AnyURIValue.class, BuiltInAtomicType.ANY_URI); |
| 118 |
itemTypeMap.put(QNameValue.class, BuiltInAtomicType.QNAME); |
| 119 |
itemTypeMap.put(NotationValue.class, BuiltInAtomicType.NOTATION); |
| 120 |
itemTypeMap.put(HexBinaryValue.class, BuiltInAtomicType.HEX_BINARY); |
| 121 |
itemTypeMap.put(Base64BinaryValue.class, BuiltInAtomicType.BASE64_BINARY); |
| 122 |
itemTypeMap.put(NodeInfo.class, AnyNodeTest.getInstance()); |
| 123 |
itemTypeMap.put(TreeInfo.class, NodeKindTest.DOCUMENT); |
| 124 |
itemTypeMap.put(MapItem.class, MapType.getInstance()); |
| 125 |
itemTypeMap.put(ArrayItem.class, ArrayItemType.getInstance()); |
| 126 |
itemTypeMap.put(Function.class, AnyFunctionType.getInstance()); |
| 127 |
itemTypeMap.put(AtomicValue.class, BuiltInAtomicType.ANY_ATOMIC); |
| 128 |
itemTypeMap.put(UntypedAtomicValue.class, BuiltInAtomicType.UNTYPED_ATOMIC); |
| 129 |
} |
| 130 |
|
| 131 |
private static Map<Class<?>, Integer> cardinalityMap = new HashMap<>(); |
| 132 |
|
| 133 |
static { |
| 134 |
cardinalityMap.put(Sequence.class, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 135 |
cardinalityMap.put(ZeroOrMore.class, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 136 |
cardinalityMap.put(OneOrMore.class, StaticProperty.ALLOWS_ONE_OR_MORE); |
| 137 |
cardinalityMap.put(One.class, StaticProperty.EXACTLY_ONE); |
| 138 |
cardinalityMap.put(ZeroOrOne.class, StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 139 |
cardinalityMap.put(XdmValue.class, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 140 |
cardinalityMap.put(XdmItem.class, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 141 |
cardinalityMap.put(XdmEmptySequence.class, StaticProperty.ALLOWS_ZERO); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static JPConverter allocate(Class javaClass, java.lang.reflect.Type genericType, Configuration config) { |
| 155 |
if (javax.xml.namespace.QName.class.isAssignableFrom(javaClass)) { |
| 156 |
return FromQName.INSTANCE; |
| 157 |
} |
| 158 |
|
| 159 |
if (Sequence.class.isAssignableFrom(javaClass)) { |
| 160 |
|
| 161 |
if (genericType instanceof ParameterizedType) { |
| 162 |
java.lang.reflect.Type[] params = ((ParameterizedType)genericType).getActualTypeArguments(); |
| 163 |
if (params.length == 1 && params[0] instanceof Class && Item.class.isAssignableFrom((Class) params[0])) { |
| 164 |
ItemType itemType = itemTypeMap.get((Class)params[0]); |
| 165 |
Integer cardinality = cardinalityMap.get(javaClass); |
| 166 |
if (itemType != null && cardinality != null) { |
| 167 |
return new FromSequence(itemType, cardinality); |
| 168 |
} |
| 169 |
} |
| 170 |
} else { |
| 171 |
ItemType itemType = itemTypeMap.get(javaClass); |
| 172 |
if (itemType != null) { |
| 173 |
return new FromSequence(itemType, StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
JPConverter c = converterMap.get(javaClass); |
| 178 |
if (c != null) { |
| 179 |
return c; |
| 180 |
} |
| 181 |
if (javaClass.equals(Object.class)) { |
| 182 |
return FromObject.INSTANCE; |
| 183 |
} |
| 184 |
if (NodeInfo.class.isAssignableFrom(javaClass)) { |
| 185 |
|
| 186 |
return new FromSequence(AnyNodeTest.getInstance(), StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 187 |
} |
| 188 |
if (Source.class.isAssignableFrom(javaClass) && !DOMSource.class.isAssignableFrom(javaClass)) { |
| 189 |
return FromSource.INSTANCE; |
| 190 |
} |
| 191 |
for (Map.Entry<Class<?>, JPConverter> e : converterMap.entrySet()) { |
| 192 |
if (e.getKey().isAssignableFrom(javaClass)) { |
| 193 |
return e.getValue(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
List<ExternalObjectModel> externalObjectModels = config.getExternalObjectModels(); |
| 198 |
for (ExternalObjectModel model : externalObjectModels) { |
| 199 |
JPConverter converter = model.getJPConverter(javaClass, config); |
| 200 |
if (converter != null) { |
| 201 |
return converter; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (javaClass.isArray()) { |
| 206 |
Class itemClass = javaClass.getComponentType(); |
| 207 |
return new FromObjectArray(allocate(itemClass, null, config)); |
| 208 |
} |
| 209 |
|
| 210 |
if (javaClass.equals(Void.TYPE)) { |
| 211 |
return VoidConverter.INSTANCE; |
| 212 |
} |
| 213 |
|
| 214 |
return new ExternalObjectWrapper(config.getJavaExternalObjectType(javaClass)); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public abstract Sequence<?> convert(Object object, XPathContext context) throws XPathException; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public abstract ItemType getItemType(); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public int getCardinality() { |
| 244 |
|
| 245 |
return StaticProperty.EXACTLY_ONE; |
| 246 |
} |
| 247 |
|
| 248 |
public static class FromObject extends JPConverter { |
| 249 |
public static final FromObject INSTANCE = new FromObject(); |
| 250 |
|
| 251 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 252 |
Class theClass = object.getClass(); |
| 253 |
JPConverter instanceConverter = allocate(theClass, null, context.getConfiguration()); |
| 254 |
if (instanceConverter instanceof FromObject) { |
| 255 |
instanceConverter = new ExternalObjectWrapper( |
| 256 |
context.getConfiguration().getJavaExternalObjectType(theClass)); |
| 257 |
} |
| 258 |
return instanceConverter.convert(object, context); |
| 259 |
} |
| 260 |
|
| 261 |
public ItemType getItemType() { |
| 262 |
return AnyItemType.getInstance(); |
| 263 |
} |
| 264 |
|
| 265 |
public int getCardinality() { |
| 266 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
public static class FromSequenceIterator extends JPConverter { |
| 271 |
public static final FromSequenceIterator INSTANCE = new FromSequenceIterator(); |
| 272 |
|
| 273 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 274 |
return ((SequenceIterator<Item<?>>)object).materialize(); |
| 275 |
} |
| 276 |
|
| 277 |
public ItemType getItemType() { |
| 278 |
return AnyItemType.getInstance(); |
| 279 |
} |
| 280 |
|
| 281 |
public int getCardinality() { |
| 282 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public static class FromXdmValue extends JPConverter { |
| 287 |
|
| 288 |
private ItemType resultType; |
| 289 |
private int cardinality; |
| 290 |
|
| 291 |
public FromXdmValue(ItemType resultType, int cardinality) { |
| 292 |
this.resultType = resultType; |
| 293 |
this.cardinality = cardinality; |
| 294 |
} |
| 295 |
|
| 296 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 297 |
return ((XdmValue)object).getUnderlyingValue(); |
| 298 |
} |
| 299 |
|
| 300 |
public ItemType getItemType() { |
| 301 |
return resultType; |
| 302 |
} |
| 303 |
|
| 304 |
public int getCardinality() { |
| 305 |
return cardinality; |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
public static class FromSequence extends JPConverter { |
| 311 |
|
| 312 |
public static final FromSequence INSTANCE = |
| 313 |
new FromSequence(AnyItemType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 314 |
|
| 315 |
private ItemType resultType; |
| 316 |
private int cardinality; |
| 317 |
|
| 318 |
public FromSequence(ItemType resultType, int cardinality) { |
| 319 |
this.resultType = resultType; |
| 320 |
this.cardinality = cardinality; |
| 321 |
} |
| 322 |
|
| 323 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 324 |
return object instanceof Closure ? |
| 325 |
((Closure) object).iterate().materialize() : |
| 326 |
(Sequence<?>) object; |
| 327 |
} |
| 328 |
|
| 329 |
public ItemType getItemType() { |
| 330 |
return resultType; |
| 331 |
} |
| 332 |
|
| 333 |
public int getCardinality() { |
| 334 |
return cardinality; |
| 335 |
} |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
public static class FromString extends JPConverter { |
| 340 |
public static final FromString INSTANCE = new FromString(); |
| 341 |
|
| 342 |
public StringValue convert(Object object, XPathContext context) throws XPathException { |
| 343 |
return new StringValue((String) object); |
| 344 |
} |
| 345 |
|
| 346 |
public ItemType getItemType() { |
| 347 |
return BuiltInAtomicType.STRING; |
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
public static class FromBoolean extends JPConverter { |
| 353 |
public static final FromBoolean INSTANCE = new FromBoolean(); |
| 354 |
|
| 355 |
public BooleanValue convert(Object object, XPathContext context) throws XPathException { |
| 356 |
return BooleanValue.get((Boolean) object); |
| 357 |
} |
| 358 |
|
| 359 |
public ItemType getItemType() { |
| 360 |
return BuiltInAtomicType.BOOLEAN; |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
public static class FromDouble extends JPConverter { |
| 366 |
public static final FromDouble INSTANCE = new FromDouble(); |
| 367 |
|
| 368 |
public DoubleValue convert(Object object, XPathContext context) throws XPathException { |
| 369 |
return new DoubleValue((Double) object); |
| 370 |
} |
| 371 |
|
| 372 |
public ItemType getItemType() { |
| 373 |
return BuiltInAtomicType.DOUBLE; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
public static class FromFloat extends JPConverter { |
| 378 |
public static final FromFloat INSTANCE = new FromFloat(); |
| 379 |
|
| 380 |
public FloatValue convert(Object object, XPathContext context) throws XPathException { |
| 381 |
return new FloatValue((Float) object); |
| 382 |
} |
| 383 |
|
| 384 |
public ItemType getItemType() { |
| 385 |
return BuiltInAtomicType.FLOAT; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
public static class FromBigDecimal extends JPConverter { |
| 390 |
public static final FromBigDecimal INSTANCE = new FromBigDecimal(); |
| 391 |
|
| 392 |
public BigDecimalValue convert(Object object, XPathContext context) throws XPathException { |
| 393 |
return new BigDecimalValue((BigDecimal) object); |
| 394 |
} |
| 395 |
|
| 396 |
public ItemType getItemType() { |
| 397 |
return BuiltInAtomicType.DECIMAL; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
public static class FromBigInteger extends JPConverter { |
| 402 |
public static final FromBigInteger INSTANCE = new FromBigInteger(); |
| 403 |
|
| 404 |
public IntegerValue convert(Object object, XPathContext context) throws XPathException { |
| 405 |
return IntegerValue.makeIntegerValue((BigInteger) object); |
| 406 |
} |
| 407 |
|
| 408 |
public ItemType getItemType() { |
| 409 |
return BuiltInAtomicType.INTEGER; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
public static class FromLong extends JPConverter { |
| 414 |
public static final FromLong INSTANCE = new FromLong(); |
| 415 |
|
| 416 |
public Int64Value convert(Object object, XPathContext context) throws XPathException { |
| 417 |
return new Int64Value((Long) object); |
| 418 |
} |
| 419 |
|
| 420 |
public ItemType getItemType() { |
| 421 |
return BuiltInAtomicType.INTEGER; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
public static class FromInt extends JPConverter { |
| 426 |
public static final FromInt INSTANCE = new FromInt(); |
| 427 |
|
| 428 |
public Int64Value convert(Object object, XPathContext context) throws XPathException { |
| 429 |
return new Int64Value((Integer) object); |
| 430 |
} |
| 431 |
|
| 432 |
public ItemType getItemType() { |
| 433 |
return BuiltInAtomicType.INTEGER; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
public static class FromShort extends JPConverter { |
| 438 |
public static final FromShort INSTANCE = new FromShort(); |
| 439 |
|
| 440 |
public Int64Value convert(Object object, XPathContext context) throws XPathException { |
| 441 |
return new Int64Value(((Short) object).intValue()); |
| 442 |
} |
| 443 |
|
| 444 |
public ItemType getItemType() { |
| 445 |
return BuiltInAtomicType.INTEGER; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
public static class FromByte extends JPConverter { |
| 450 |
public static final FromByte INSTANCE = new FromByte(); |
| 451 |
|
| 452 |
public Int64Value convert(Object object, XPathContext context) throws XPathException { |
| 453 |
return new Int64Value(((Byte) object).intValue()); |
| 454 |
} |
| 455 |
|
| 456 |
public ItemType getItemType() { |
| 457 |
return BuiltInAtomicType.INTEGER; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
public static class FromCharacter extends JPConverter { |
| 462 |
public static final FromCharacter INSTANCE = new FromCharacter(); |
| 463 |
|
| 464 |
public StringValue convert(Object object, XPathContext context) throws XPathException { |
| 465 |
return new StringValue(object.toString()); |
| 466 |
} |
| 467 |
|
| 468 |
public ItemType getItemType() { |
| 469 |
return BuiltInAtomicType.STRING; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
public static class FromQName extends JPConverter { |
| 474 |
public static final FromQName INSTANCE = new FromQName(); |
| 475 |
|
| 476 |
public QNameValue convert(Object object, XPathContext context) throws XPathException { |
| 477 |
javax.xml.namespace.QName qn = (javax.xml.namespace.QName) object; |
| 478 |
return new QNameValue(qn.getPrefix(), qn.getNamespaceURI(), qn.getLocalPart()); |
| 479 |
} |
| 480 |
|
| 481 |
public ItemType getItemType() { |
| 482 |
return BuiltInAtomicType.QNAME; |
| 483 |
} |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
public static class FromURI extends JPConverter { |
| 488 |
|
| 489 |
public static final FromURI INSTANCE = new FromURI(); |
| 490 |
|
| 491 |
public AnyURIValue convert(Object object, XPathContext context) throws XPathException { |
| 492 |
return new AnyURIValue(object.toString()); |
| 493 |
} |
| 494 |
|
| 495 |
public ItemType getItemType() { |
| 496 |
return BuiltInAtomicType.ANY_URI; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
public static class FromDate extends JPConverter { |
| 501 |
public static final FromDate INSTANCE = new FromDate(); |
| 502 |
|
| 503 |
public DateTimeValue convert(Object object, XPathContext context) throws XPathException { |
| 504 |
return DateTimeValue.fromJavaDate((Date) object); |
| 505 |
} |
| 506 |
|
| 507 |
public ItemType getItemType() { |
| 508 |
return BuiltInAtomicType.DATE_TIME; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
public static class ExternalObjectWrapper extends JPConverter { |
| 513 |
|
| 514 |
private JavaExternalObjectType resultType; |
| 515 |
|
| 516 |
public ExternalObjectWrapper(JavaExternalObjectType resultType) { |
| 517 |
this.resultType = resultType; |
| 518 |
} |
| 519 |
|
| 520 |
public ExternalObject<Object> convert(Object object, XPathContext context) throws XPathException { |
| 521 |
if (object == null) { |
| 522 |
return null; |
| 523 |
} else if (resultType.getJavaClass().isInstance(object)) { |
| 524 |
return new ObjectValue<>(object); |
| 525 |
} else { |
| 526 |
throw new XPathException("Java external object of type " + object.getClass().getName() + |
| 527 |
" is not an instance of the required type " + resultType.getJavaClass().getName(), "XPTY0004"); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
public JavaExternalObjectType getItemType() { |
| 532 |
return resultType; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
public static class VoidConverter extends JPConverter { |
| 538 |
|
| 539 |
public static final VoidConverter INSTANCE = new VoidConverter(); |
| 540 |
|
| 541 |
public VoidConverter() { |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
public EmptySequence<Item<?>> convert(Object object, XPathContext context) throws XPathException { |
| 546 |
return EmptySequence.getInstance(); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public ItemType getItemType() { |
| 553 |
return AnyItemType.getInstance(); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
public static class FromCollection extends JPConverter { |
| 558 |
|
| 559 |
public static final FromCollection INSTANCE = new FromCollection(); |
| 560 |
|
| 561 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 562 |
List<Item<?>> list = new ArrayList<>(((Collection) object).size()); |
| 563 |
int a = 0; |
| 564 |
for (Object obj : (Collection) object) { |
| 565 |
JPConverter itemConverter = allocate(obj.getClass(), null, context.getConfiguration()); |
| 566 |
try { |
| 567 |
Item<?> item = SequenceTool.asItem(itemConverter.convert(obj, context)); |
| 568 |
if (item != null) { |
| 569 |
list.add(item); |
| 570 |
} |
| 571 |
} catch (XPathException e) { |
| 572 |
throw new XPathException( |
| 573 |
"Returned Collection contains an object that cannot be converted to an Item (" |
| 574 |
+ obj.getClass() + "): " + e.getMessage(), |
| 575 |
SaxonErrorCode.SXJE0051); |
| 576 |
} |
| 577 |
} |
| 578 |
return new SequenceExtent<>(list); |
| 579 |
} |
| 580 |
|
| 581 |
public ItemType getItemType() { |
| 582 |
return AnyItemType.getInstance(); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public int getCardinality() { |
| 592 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 593 |
} |
| 594 |
|
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
public static class FromSource extends JPConverter { |
| 599 |
|
| 600 |
public static final FromSource INSTANCE = new FromSource(); |
| 601 |
|
| 602 |
public NodeInfo convert(Object object, XPathContext context) throws XPathException { |
| 603 |
ParseOptions options = new ParseOptions(); |
| 604 |
Controller controller = context.getController(); |
| 605 |
if (controller != null) { |
| 606 |
options.setSchemaValidationMode(controller.getSchemaValidationMode()); |
| 607 |
} |
| 608 |
return context.getConfiguration().buildDocumentTree((Source) object, options).getRootNode(); |
| 609 |
} |
| 610 |
|
| 611 |
public ItemType getItemType() { |
| 612 |
return AnyNodeTest.getInstance(); |
| 613 |
} |
| 614 |
|
| 615 |
} |
| 616 |
|
| 617 |
public static class FromLongArray extends JPConverter { |
| 618 |
|
| 619 |
public static final FromLongArray INSTANCE = new FromLongArray(); |
| 620 |
|
| 621 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 622 |
Item<?>[] array = new Item[((long[]) object).length]; |
| 623 |
for (int i = 0; i < array.length; i++) { |
| 624 |
array[i] = Int64Value.makeDerived(((long[]) object)[i], BuiltInAtomicType.LONG); |
| 625 |
} |
| 626 |
return new SequenceExtent<>(array); |
| 627 |
} |
| 628 |
|
| 629 |
public ItemType getItemType() { |
| 630 |
return BuiltInAtomicType.LONG; |
| 631 |
} |
| 632 |
|
| 633 |
public int getCardinality() { |
| 634 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 635 |
} |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
public static class FromIntArray extends JPConverter { |
| 640 |
|
| 641 |
public static final FromIntArray INSTANCE = new FromIntArray(); |
| 642 |
|
| 643 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 644 |
Item<?>[] array = new Item[((int[]) object).length]; |
| 645 |
for (int i = 0; i < array.length; i++) { |
| 646 |
array[i] = Int64Value.makeDerived(((int[]) object)[i], BuiltInAtomicType.INT); |
| 647 |
} |
| 648 |
return new SequenceExtent<>(array); |
| 649 |
} |
| 650 |
|
| 651 |
public ItemType getItemType() { |
| 652 |
return BuiltInAtomicType.INT; |
| 653 |
} |
| 654 |
|
| 655 |
public int getCardinality() { |
| 656 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 657 |
} |
| 658 |
|
| 659 |
} |
| 660 |
|
| 661 |
public static class FromShortArray extends JPConverter { |
| 662 |
|
| 663 |
public static final FromShortArray INSTANCE = new FromShortArray(); |
| 664 |
|
| 665 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 666 |
Item<?>[] array = new Item[((short[]) object).length]; |
| 667 |
for (int i = 0; i < array.length; i++) { |
| 668 |
array[i] = Int64Value.makeDerived(((short[]) object)[i], BuiltInAtomicType.SHORT); |
| 669 |
} |
| 670 |
return new SequenceExtent<>(array); |
| 671 |
} |
| 672 |
|
| 673 |
public ItemType getItemType() { |
| 674 |
return BuiltInAtomicType.SHORT; |
| 675 |
} |
| 676 |
|
| 677 |
public int getCardinality() { |
| 678 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 679 |
} |
| 680 |
|
| 681 |
} |
| 682 |
|
| 683 |
public static class FromByteArray extends JPConverter { |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
public static final FromByteArray INSTANCE = new FromByteArray(); |
| 688 |
|
| 689 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 690 |
Item<?>[] array = new Item[((byte[]) object).length]; |
| 691 |
for (int i = 0; i < array.length; i++) { |
| 692 |
array[i] = Int64Value.makeDerived(255 & (int) ((byte[]) object)[i], BuiltInAtomicType.UNSIGNED_BYTE); |
| 693 |
} |
| 694 |
return new SequenceExtent<>(array); |
| 695 |
} |
| 696 |
|
| 697 |
public ItemType getItemType() { |
| 698 |
return BuiltInAtomicType.UNSIGNED_BYTE; |
| 699 |
} |
| 700 |
|
| 701 |
public int getCardinality() { |
| 702 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
|
| 707 |
public static class FromCharArray extends JPConverter { |
| 708 |
|
| 709 |
public static final FromCharArray INSTANCE = new FromCharArray(); |
| 710 |
|
| 711 |
public StringValue convert(Object object, XPathContext context) throws XPathException { |
| 712 |
return StringValue.makeStringValue(new String((char[]) object)); |
| 713 |
} |
| 714 |
|
| 715 |
public ItemType getItemType() { |
| 716 |
return BuiltInAtomicType.STRING; |
| 717 |
} |
| 718 |
|
| 719 |
} |
| 720 |
|
| 721 |
public static class FromDoubleArray extends JPConverter { |
| 722 |
|
| 723 |
public static final FromDoubleArray INSTANCE = new FromDoubleArray(); |
| 724 |
|
| 725 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 726 |
Item<?>[] array = new Item[((double[]) object).length]; |
| 727 |
for (int i = 0; i < array.length; i++) { |
| 728 |
array[i] = new DoubleValue(((double[]) object)[i]); |
| 729 |
} |
| 730 |
return new SequenceExtent<>(array); |
| 731 |
} |
| 732 |
|
| 733 |
public ItemType getItemType() { |
| 734 |
return BuiltInAtomicType.DOUBLE; |
| 735 |
} |
| 736 |
|
| 737 |
public int getCardinality() { |
| 738 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 739 |
} |
| 740 |
|
| 741 |
} |
| 742 |
|
| 743 |
public static class FromFloatArray extends JPConverter { |
| 744 |
|
| 745 |
public static final FromFloatArray INSTANCE = new FromFloatArray(); |
| 746 |
|
| 747 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 748 |
Item<?>[] array = new Item[((float[]) object).length]; |
| 749 |
for (int i = 0; i < array.length; i++) { |
| 750 |
array[i] = new DoubleValue(((float[]) object)[i]); |
| 751 |
} |
| 752 |
return new SequenceExtent<>(array); |
| 753 |
} |
| 754 |
|
| 755 |
public ItemType getItemType() { |
| 756 |
return BuiltInAtomicType.FLOAT; |
| 757 |
} |
| 758 |
|
| 759 |
public int getCardinality() { |
| 760 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 761 |
} |
| 762 |
|
| 763 |
} |
| 764 |
|
| 765 |
public static class FromBooleanArray extends JPConverter { |
| 766 |
|
| 767 |
public static final FromBooleanArray INSTANCE = new FromBooleanArray(); |
| 768 |
|
| 769 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 770 |
Item<?>[] array = new Item[((boolean[]) object).length]; |
| 771 |
for (int i = 0; i < array.length; i++) { |
| 772 |
array[i] = BooleanValue.get(((boolean[]) object)[i]); |
| 773 |
} |
| 774 |
return new SequenceExtent<>(array); |
| 775 |
} |
| 776 |
|
| 777 |
public ItemType getItemType() { |
| 778 |
return BuiltInAtomicType.BOOLEAN; |
| 779 |
} |
| 780 |
|
| 781 |
public int getCardinality() { |
| 782 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 783 |
} |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
public static class FromObjectArray extends JPConverter { |
| 788 |
|
| 789 |
private JPConverter itemConverter; |
| 790 |
|
| 791 |
public FromObjectArray(JPConverter itemConverter) { |
| 792 |
this.itemConverter = itemConverter; |
| 793 |
} |
| 794 |
|
| 795 |
public Sequence<?> convert(Object object, XPathContext context) throws XPathException { |
| 796 |
Object[] arrayObject = (Object[]) object; |
| 797 |
List<Item<?>> newArray = new ArrayList<>(arrayObject.length); |
| 798 |
int a = 0; |
| 799 |
for (Object member : arrayObject) { |
| 800 |
if (member != null) { |
| 801 |
try { |
| 802 |
Item<?> newItem = SequenceTool.asItem(itemConverter.convert(member, context)); |
| 803 |
if (newItem != null) { |
| 804 |
newArray.add(newItem); |
| 805 |
} |
| 806 |
} catch (XPathException e) { |
| 807 |
throw new XPathException( |
| 808 |
"Returned array contains an object that cannot be converted to an Item (" + |
| 809 |
member.getClass() + "): " + e.getMessage(), |
| 810 |
SaxonErrorCode.SXJE0051); |
| 811 |
} |
| 812 |
} else { |
| 813 |
throw new XPathException("Returned array contains null values: cannot convert to items", SaxonErrorCode.SXJE0051); |
| 814 |
} |
| 815 |
} |
| 816 |
return new SequenceExtent<>(newArray); |
| 817 |
} |
| 818 |
|
| 819 |
public ItemType getItemType() { |
| 820 |
return itemConverter.getItemType(); |
| 821 |
} |
| 822 |
|
| 823 |
public int getCardinality() { |
| 824 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 825 |
} |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
} |
| 830 |
|