| 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.lib.ExternalObjectModel; |
| 12 |
import net.sf.saxon.ma.map.MapItem; |
| 13 |
import net.sf.saxon.ma.map.MapType; |
| 14 |
import net.sf.saxon.om.*; |
| 15 |
import net.sf.saxon.pattern.NodeTest; |
| 16 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.wrapper.VirtualNode; |
| 19 |
import net.sf.saxon.type.*; |
| 20 |
import net.sf.saxon.value.*; |
| 21 |
import net.sf.saxon.value.StringValue; |
| 22 |
|
| 23 |
import java.lang.reflect.Array; |
| 24 |
import java.lang.reflect.Constructor; |
| 25 |
import java.lang.reflect.InvocationTargetException; |
| 26 |
import java.lang.reflect.ParameterizedType; |
| 27 |
import java.math.BigDecimal; |
| 28 |
import java.math.BigInteger; |
| 29 |
import java.net.MalformedURLException; |
| 30 |
import java.net.URI; |
| 31 |
import java.net.URISyntaxException; |
| 32 |
import java.net.URL; |
| 33 |
import java.time.Instant; |
| 34 |
import java.time.ZonedDateTime; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Collection; |
| 37 |
import java.util.HashMap; |
| 38 |
import java.util.List; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class PJConverter { |
| 45 |
|
| 46 |
private static HashMap<Class, SequenceType> jpmap = new HashMap<>(); |
| 47 |
|
| 48 |
static { |
| 49 |
jpmap.put(boolean.class, SequenceType.SINGLE_BOOLEAN); |
| 50 |
jpmap.put(Boolean.class, SequenceType.OPTIONAL_BOOLEAN); |
| 51 |
jpmap.put(String.class, SequenceType.OPTIONAL_STRING); |
| 52 |
jpmap.put(CharSequence.class, SequenceType.OPTIONAL_STRING); |
| 53 |
|
| 54 |
|
| 55 |
jpmap.put(long.class, SequenceType.SINGLE_INTEGER); |
| 56 |
jpmap.put(Long.class, SequenceType.OPTIONAL_INTEGER); |
| 57 |
jpmap.put(int.class, SequenceType.SINGLE_INTEGER); |
| 58 |
jpmap.put(Integer.class, SequenceType.OPTIONAL_INTEGER); |
| 59 |
jpmap.put(short.class, SequenceType.SINGLE_SHORT); |
| 60 |
jpmap.put(Short.class, SequenceType.OPTIONAL_SHORT); |
| 61 |
jpmap.put(byte.class, SequenceType.SINGLE_BYTE); |
| 62 |
jpmap.put(Byte.class, SequenceType.OPTIONAL_BYTE); |
| 63 |
jpmap.put(float.class, SequenceType.SINGLE_FLOAT); |
| 64 |
jpmap.put(Float.class, SequenceType.OPTIONAL_FLOAT); |
| 65 |
jpmap.put(double.class, SequenceType.SINGLE_DOUBLE); |
| 66 |
jpmap.put(Double.class, SequenceType.OPTIONAL_DOUBLE); |
| 67 |
jpmap.put(URI.class, SequenceType.OPTIONAL_ANY_URI); |
| 68 |
jpmap.put(URL.class, SequenceType.OPTIONAL_ANY_URI); |
| 69 |
jpmap.put(BigInteger.class, SequenceType.OPTIONAL_INTEGER); |
| 70 |
jpmap.put(BigDecimal.class, SequenceType.OPTIONAL_DECIMAL); |
| 71 |
|
| 72 |
jpmap.put(StringValue.class, SequenceType.OPTIONAL_STRING); |
| 73 |
jpmap.put(BooleanValue.class, SequenceType.OPTIONAL_BOOLEAN); |
| 74 |
jpmap.put(DoubleValue.class, SequenceType.OPTIONAL_DOUBLE); |
| 75 |
jpmap.put(FloatValue.class, SequenceType.OPTIONAL_FLOAT); |
| 76 |
jpmap.put(DecimalValue.class, SequenceType.OPTIONAL_DECIMAL); |
| 77 |
jpmap.put(IntegerValue.class, SequenceType.OPTIONAL_INTEGER); |
| 78 |
jpmap.put(AnyURIValue.class, SequenceType.OPTIONAL_ANY_URI); |
| 79 |
jpmap.put(QNameValue.class, SequenceType.OPTIONAL_QNAME); |
| 80 |
jpmap.put(NotationValue.class, SequenceType.OPTIONAL_NOTATION); |
| 81 |
jpmap.put(DateValue.class, SequenceType.OPTIONAL_DATE); |
| 82 |
jpmap.put(DateTimeValue.class, SequenceType.OPTIONAL_DATE_TIME); |
| 83 |
jpmap.put(TimeValue.class, SequenceType.OPTIONAL_TIME); |
| 84 |
jpmap.put(DurationValue.class, SequenceType.OPTIONAL_DURATION); |
| 85 |
jpmap.put(DayTimeDurationValue.class, SequenceType.OPTIONAL_DAY_TIME_DURATION); |
| 86 |
jpmap.put(YearMonthDurationValue.class, SequenceType.OPTIONAL_YEAR_MONTH_DURATION); |
| 87 |
jpmap.put(GYearValue.class, SequenceType.OPTIONAL_G_YEAR); |
| 88 |
jpmap.put(GYearMonthValue.class, SequenceType.OPTIONAL_G_YEAR_MONTH); |
| 89 |
jpmap.put(GMonthValue.class, SequenceType.OPTIONAL_G_MONTH); |
| 90 |
jpmap.put(GMonthDayValue.class, SequenceType.OPTIONAL_G_MONTH_DAY); |
| 91 |
jpmap.put(GDayValue.class, SequenceType.OPTIONAL_G_DAY); |
| 92 |
jpmap.put(Base64BinaryValue.class, SequenceType.OPTIONAL_BASE64_BINARY); |
| 93 |
jpmap.put(HexBinaryValue.class, SequenceType.OPTIONAL_HEX_BINARY); |
| 94 |
jpmap.put(Function.class, SequenceType.OPTIONAL_FUNCTION_ITEM); |
| 95 |
jpmap.put(MapItem.class, MapType.OPTIONAL_MAP_ITEM); |
| 96 |
jpmap.put(NodeInfo.class, SequenceType.OPTIONAL_NODE); |
| 97 |
jpmap.put(TreeInfo.class, SequenceType.OPTIONAL_DOCUMENT_NODE); |
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static SequenceType getEquivalentSequenceType(Class javaClass) { |
| 113 |
if (javaClass.isArray()) { |
| 114 |
Class memberClass = javaClass.getComponentType(); |
| 115 |
if (memberClass == byte.class) { |
| 116 |
|
| 117 |
return SequenceType.makeSequenceType(BuiltInAtomicType.UNSIGNED_BYTE, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 118 |
} else { |
| 119 |
SequenceType memberType = getEquivalentSequenceType(memberClass); |
| 120 |
if (memberType != null) { |
| 121 |
return SequenceType.makeSequenceType(memberType.getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return jpmap.get(javaClass); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static SequenceType getParameterizedSequenceType(java.lang.reflect.Type javaType) { |
| 138 |
if (javaType instanceof ParameterizedType) { |
| 139 |
ParameterizedType aType = (ParameterizedType) javaType; |
| 140 |
java.lang.reflect.Type[] parameterArgTypes = aType.getActualTypeArguments(); |
| 141 |
if (parameterArgTypes.length == 1 && |
| 142 |
parameterArgTypes[0] instanceof Class && |
| 143 |
Item.class.isAssignableFrom((Class)parameterArgTypes[0])) { |
| 144 |
SequenceType memberType = getEquivalentSequenceType((Class)parameterArgTypes[0]); |
| 145 |
ItemType itemType = memberType == null ? null : memberType.getPrimaryType(); |
| 146 |
java.lang.reflect.Type collectionType = aType.getRawType(); |
| 147 |
int cardinality = -1; |
| 148 |
if (collectionType.equals(ZeroOrOne.class)) { |
| 149 |
cardinality = StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 150 |
} else if (collectionType.equals(One.class)) { |
| 151 |
cardinality = StaticProperty.ALLOWS_ONE; |
| 152 |
} else if (collectionType.equals(OneOrMore.class)) { |
| 153 |
cardinality = StaticProperty.ALLOWS_ONE_OR_MORE; |
| 154 |
} else if (collectionType.equals(ZeroOrMore.class)) { |
| 155 |
cardinality = StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 156 |
} |
| 157 |
if (itemType != null && cardinality != -1) { |
| 158 |
return SequenceType.makeSequenceType(itemType, cardinality); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public abstract Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) |
| 179 |
throws XPathException; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static PJConverter allocate(Configuration config, ItemType itemType, |
| 194 |
int cardinality, Class<?> targetClass) |
| 195 |
throws XPathException { |
| 196 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 197 |
if (targetClass == SequenceIterator.class) { |
| 198 |
return ToSequenceIterator.INSTANCE; |
| 199 |
} |
| 200 |
if (targetClass == Sequence.class || targetClass == Item.class) { |
| 201 |
return Identity.INSTANCE; |
| 202 |
} |
| 203 |
if (targetClass == One.class) { |
| 204 |
return ToOne.INSTANCE; |
| 205 |
} |
| 206 |
if (targetClass == ZeroOrOne.class) { |
| 207 |
return ToZeroOrOne.INSTANCE; |
| 208 |
} |
| 209 |
if (targetClass == OneOrMore.class) { |
| 210 |
return ToOneOrMore.INSTANCE; |
| 211 |
} |
| 212 |
if (targetClass == ZeroOrMore.class) { |
| 213 |
return ToZeroOrMore.INSTANCE; |
| 214 |
} |
| 215 |
if (targetClass == GroundedValue.class | targetClass == SequenceExtent.class) { |
| 216 |
return ToSequenceExtent.INSTANCE; |
| 217 |
} |
| 218 |
|
| 219 |
if (!itemType.isPlainType()) { |
| 220 |
List<ExternalObjectModel> externalObjectModels = config.getExternalObjectModels(); |
| 221 |
for (ExternalObjectModel model : externalObjectModels) { |
| 222 |
PJConverter converter = model.getPJConverter(targetClass); |
| 223 |
if (converter != null) { |
| 224 |
return converter; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (NodeInfo.class.isAssignableFrom(targetClass)) { |
| 229 |
return Identity.INSTANCE; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if (Collection.class.isAssignableFrom(targetClass)) { |
| 234 |
return ToCollection.INSTANCE; |
| 235 |
} |
| 236 |
if (targetClass.isArray()) { |
| 237 |
PJConverter itemConverter = |
| 238 |
allocate(config, itemType, StaticProperty.EXACTLY_ONE, targetClass.getComponentType()); |
| 239 |
return new ToArray(itemConverter); |
| 240 |
} |
| 241 |
if (!Cardinality.allowsMany(cardinality)) { |
| 242 |
if (itemType.isPlainType()) { |
| 243 |
if (itemType == ErrorType.getInstance()) { |
| 244 |
|
| 245 |
return StringValueToString.INSTANCE; |
| 246 |
} else if (th.isSubType(itemType, BuiltInAtomicType.STRING)) { |
| 247 |
if (targetClass == Object.class || targetClass == String.class || targetClass == CharSequence.class) { |
| 248 |
return StringValueToString.INSTANCE; |
| 249 |
} else if (targetClass.isAssignableFrom(StringValue.class)) { |
| 250 |
return Identity.INSTANCE; |
| 251 |
} else if (targetClass == char.class || targetClass == Character.class) { |
| 252 |
return StringValueToChar.INSTANCE; |
| 253 |
} else { |
| 254 |
throw cannotConvert(itemType, targetClass, config); |
| 255 |
} |
| 256 |
} else if (itemType == BuiltInAtomicType.UNTYPED_ATOMIC) { |
| 257 |
if (targetClass == Object.class || targetClass == String.class || targetClass == CharSequence.class) { |
| 258 |
return StringValueToString.INSTANCE; |
| 259 |
} else if (targetClass.isAssignableFrom(UntypedAtomicValue.class)) { |
| 260 |
return Identity.INSTANCE; |
| 261 |
} else { |
| 262 |
try { |
| 263 |
final Constructor constructor = targetClass.getConstructor(String.class); |
| 264 |
return new PJConverter() { |
| 265 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 266 |
try { |
| 267 |
return constructor.newInstance(value.head().getStringValue()); |
| 268 |
} catch (InstantiationException | IllegalAccessException e) { |
| 269 |
throw new XPathException(e); |
| 270 |
} catch (InvocationTargetException e) { |
| 271 |
throw new XPathException("Cannot convert untypedAtomic to " + targetClass.getName() + |
| 272 |
": " + e.getMessage(), "FORG0001"); |
| 273 |
} |
| 274 |
} |
| 275 |
}; |
| 276 |
} catch (NoSuchMethodException e) { |
| 277 |
throw cannotConvert(itemType, targetClass, config); |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
} else if (th.isSubType(itemType, BuiltInAtomicType.BOOLEAN)) { |
| 282 |
if (targetClass == Object.class || targetClass == Boolean.class || targetClass == boolean.class) { |
| 283 |
return BooleanValueToBoolean.INSTANCE; |
| 284 |
} else if (targetClass.isAssignableFrom(BooleanValue.class)) { |
| 285 |
return Identity.INSTANCE; |
| 286 |
} else { |
| 287 |
throw cannotConvert(itemType, targetClass, config); |
| 288 |
} |
| 289 |
} else if (th.isSubType(itemType, BuiltInAtomicType.INTEGER)) { |
| 290 |
if (targetClass == Object.class || targetClass == BigInteger.class) { |
| 291 |
return IntegerValueToBigInteger.INSTANCE; |
| 292 |
} else if (targetClass == long.class || targetClass == Long.class) { |
| 293 |
return IntegerValueToLong.INSTANCE; |
| 294 |
} else if (targetClass == int.class || targetClass == Integer.class) { |
| 295 |
return IntegerValueToInt.INSTANCE; |
| 296 |
} else if (targetClass == short.class || targetClass == Short.class) { |
| 297 |
return IntegerValueToShort.INSTANCE; |
| 298 |
} else if (targetClass == byte.class || targetClass == Byte.class) { |
| 299 |
return IntegerValueToByte.INSTANCE; |
| 300 |
} else if (targetClass == char.class || targetClass == Character.class) { |
| 301 |
return IntegerValueToChar.INSTANCE; |
| 302 |
} else if (targetClass == double.class || targetClass == Double.class) { |
| 303 |
return NumericValueToDouble.INSTANCE; |
| 304 |
} else if (targetClass == float.class || targetClass == Float.class) { |
| 305 |
return NumericValueToFloat.INSTANCE; |
| 306 |
} else if (targetClass == BigDecimal.class) { |
| 307 |
return NumericValueToBigDecimal.INSTANCE; |
| 308 |
} else if (targetClass.isAssignableFrom(IntegerValue.class)) { |
| 309 |
return Identity.INSTANCE; |
| 310 |
} else { |
| 311 |
throw cannotConvert(itemType, targetClass, config); |
| 312 |
} |
| 313 |
} else if (th.isSubType(itemType, BuiltInAtomicType.DECIMAL)) { |
| 314 |
if (targetClass == Object.class || targetClass == BigDecimal.class) { |
| 315 |
return NumericValueToBigDecimal.INSTANCE; |
| 316 |
} else if (targetClass == double.class || targetClass == Double.class) { |
| 317 |
return NumericValueToDouble.INSTANCE; |
| 318 |
} else if (targetClass == float.class || targetClass == Float.class) { |
| 319 |
return NumericValueToFloat.INSTANCE; |
| 320 |
} else if (targetClass.isAssignableFrom(BigDecimalValue.class)) { |
| 321 |
return Identity.INSTANCE; |
| 322 |
} else { |
| 323 |
throw cannotConvert(itemType, targetClass, config); |
| 324 |
} |
| 325 |
} else if (th.isSubType(itemType, BuiltInAtomicType.FLOAT)) { |
| 326 |
if (targetClass == Object.class || targetClass == Float.class || targetClass == float.class) { |
| 327 |
return NumericValueToFloat.INSTANCE; |
| 328 |
} else if (targetClass == double.class || targetClass == Double.class) { |
| 329 |
return NumericValueToDouble.INSTANCE; |
| 330 |
} else if (targetClass.isAssignableFrom(FloatValue.class)) { |
| 331 |
return Identity.INSTANCE; |
| 332 |
} else { |
| 333 |
throw cannotConvert(itemType, targetClass, config); |
| 334 |
} |
| 335 |
} else if (th.isSubType(itemType, BuiltInAtomicType.DOUBLE)) { |
| 336 |
if (targetClass == Object.class || targetClass == Double.class || targetClass == double.class) { |
| 337 |
return NumericValueToDouble.INSTANCE; |
| 338 |
} else if (targetClass.isAssignableFrom(DoubleValue.class)) { |
| 339 |
return Identity.INSTANCE; |
| 340 |
} else { |
| 341 |
return Atomic.INSTANCE; |
| 342 |
} |
| 343 |
} else if (th.isSubType(itemType, BuiltInAtomicType.ANY_URI)) { |
| 344 |
if (targetClass == Object.class || URI.class.isAssignableFrom(targetClass)) { |
| 345 |
return AnyURIValueToURI.INSTANCE; |
| 346 |
} else if (URL.class.isAssignableFrom(targetClass)) { |
| 347 |
return AnyURIValueToURL.INSTANCE; |
| 348 |
} else if (targetClass == String.class || targetClass == CharSequence.class) { |
| 349 |
return StringValueToString.INSTANCE; |
| 350 |
} else if (targetClass.isAssignableFrom(AnyURIValue.class)) { |
| 351 |
return Identity.INSTANCE; |
| 352 |
} else { |
| 353 |
throw cannotConvert(itemType, targetClass, config); |
| 354 |
} |
| 355 |
} else if (th.isSubType(itemType, BuiltInAtomicType.QNAME)) { |
| 356 |
if (targetClass == Object.class || targetClass == javax.xml.namespace.QName.class) { |
| 357 |
return QualifiedNameValueToQName.INSTANCE; |
| 358 |
} else if (targetClass.isAssignableFrom(QNameValue.class)) { |
| 359 |
return Identity.INSTANCE; |
| 360 |
} else { |
| 361 |
throw cannotConvert(itemType, targetClass, config); |
| 362 |
} |
| 363 |
} else if (th.isSubType(itemType, BuiltInAtomicType.NOTATION)) { |
| 364 |
if (targetClass == Object.class || targetClass == javax.xml.namespace.QName.class) { |
| 365 |
return QualifiedNameValueToQName.INSTANCE; |
| 366 |
} else if (targetClass.isAssignableFrom(NotationValue.class)) { |
| 367 |
return Identity.INSTANCE; |
| 368 |
} else { |
| 369 |
throw cannotConvert(itemType, targetClass, config); |
| 370 |
} |
| 371 |
} else if (th.isSubType(itemType, BuiltInAtomicType.DURATION)) { |
| 372 |
if (targetClass.isAssignableFrom(DurationValue.class)) { |
| 373 |
return Identity.INSTANCE; |
| 374 |
} else { |
| 375 |
throw cannotConvert(itemType, targetClass, config); |
| 376 |
} |
| 377 |
} else if (th.isSubType(itemType, BuiltInAtomicType.DATE_TIME)) { |
| 378 |
if (targetClass.isAssignableFrom(DateTimeValue.class)) { |
| 379 |
return Identity.INSTANCE; |
| 380 |
} else if (targetClass == java.util.Date.class) { |
| 381 |
return CalendarValueToDate.INSTANCE; |
| 382 |
} else if (targetClass == java.util.Calendar.class) { |
| 383 |
return CalendarValueToCalendar.INSTANCE; |
| 384 |
} else if (targetClass == Instant.class) { |
| 385 |
return CalendarValueToInstant.INSTANCE; |
| 386 |
} else if (targetClass == ZonedDateTime.class) { |
| 387 |
return CalendarValueToZonedDateTime.INSTANCE; |
| 388 |
} else { |
| 389 |
throw cannotConvert(itemType, targetClass, config); |
| 390 |
} |
| 391 |
} else if (th.isSubType(itemType, BuiltInAtomicType.DATE)) { |
| 392 |
if (targetClass.isAssignableFrom(DateValue.class)) { |
| 393 |
return Identity.INSTANCE; |
| 394 |
} else if (targetClass == java.util.Date.class) { |
| 395 |
return CalendarValueToDate.INSTANCE; |
| 396 |
} else if (targetClass == java.util.Calendar.class) { |
| 397 |
return CalendarValueToCalendar.INSTANCE; |
| 398 |
} else if (targetClass == Instant.class) { |
| 399 |
return CalendarValueToInstant.INSTANCE; |
| 400 |
} else if (targetClass == ZonedDateTime.class) { |
| 401 |
return CalendarValueToZonedDateTime.INSTANCE; |
| 402 |
} else { |
| 403 |
throw cannotConvert(itemType, targetClass, config); |
| 404 |
} |
| 405 |
} else if (th.isSubType(itemType, BuiltInAtomicType.TIME)) { |
| 406 |
if (targetClass.isAssignableFrom(TimeValue.class)) { |
| 407 |
return Identity.INSTANCE; |
| 408 |
} else if (targetClass == java.util.Date.class) { |
| 409 |
return CalendarValueToDate.INSTANCE; |
| 410 |
} else if (targetClass == java.util.Calendar.class) { |
| 411 |
return CalendarValueToCalendar.INSTANCE; |
| 412 |
} else if (targetClass == Instant.class) { |
| 413 |
return CalendarValueToInstant.INSTANCE; |
| 414 |
} else if (targetClass == ZonedDateTime.class) { |
| 415 |
return CalendarValueToZonedDateTime.INSTANCE; |
| 416 |
} else { |
| 417 |
throw cannotConvert(itemType, targetClass, config); |
| 418 |
} |
| 419 |
} else if (th.isSubType(itemType, BuiltInAtomicType.G_YEAR)) { |
| 420 |
if (targetClass.isAssignableFrom(GYearValue.class)) { |
| 421 |
return Identity.INSTANCE; |
| 422 |
} else { |
| 423 |
throw cannotConvert(itemType, targetClass, config); |
| 424 |
} |
| 425 |
} else if (th.isSubType(itemType, BuiltInAtomicType.G_YEAR_MONTH)) { |
| 426 |
if (targetClass.isAssignableFrom(GYearMonthValue.class)) { |
| 427 |
return Identity.INSTANCE; |
| 428 |
} else { |
| 429 |
throw cannotConvert(itemType, targetClass, config); |
| 430 |
} |
| 431 |
} else if (th.isSubType(itemType, BuiltInAtomicType.G_MONTH)) { |
| 432 |
if (targetClass.isAssignableFrom(GMonthValue.class)) { |
| 433 |
return Identity.INSTANCE; |
| 434 |
} else { |
| 435 |
throw cannotConvert(itemType, targetClass, config); |
| 436 |
} |
| 437 |
} else if (th.isSubType(itemType, BuiltInAtomicType.G_MONTH_DAY)) { |
| 438 |
if (targetClass.isAssignableFrom(GMonthDayValue.class)) { |
| 439 |
return Identity.INSTANCE; |
| 440 |
} else { |
| 441 |
throw cannotConvert(itemType, targetClass, config); |
| 442 |
} |
| 443 |
} else if (th.isSubType(itemType, BuiltInAtomicType.G_DAY)) { |
| 444 |
if (targetClass.isAssignableFrom(GDayValue.class)) { |
| 445 |
return Identity.INSTANCE; |
| 446 |
} else { |
| 447 |
throw cannotConvert(itemType, targetClass, config); |
| 448 |
} |
| 449 |
} else if (th.isSubType(itemType, BuiltInAtomicType.BASE64_BINARY)) { |
| 450 |
if (targetClass.isAssignableFrom(Base64BinaryValue.class)) { |
| 451 |
return Identity.INSTANCE; |
| 452 |
} else { |
| 453 |
throw cannotConvert(itemType, targetClass, config); |
| 454 |
} |
| 455 |
} else if (th.isSubType(itemType, BuiltInAtomicType.HEX_BINARY)) { |
| 456 |
if (targetClass.isAssignableFrom(HexBinaryValue.class)) { |
| 457 |
return Identity.INSTANCE; |
| 458 |
} else { |
| 459 |
throw cannotConvert(itemType, targetClass, config); |
| 460 |
} |
| 461 |
} else { |
| 462 |
return Atomic.INSTANCE; |
| 463 |
} |
| 464 |
|
| 465 |
} else if (itemType instanceof JavaExternalObjectType) { |
| 466 |
return UnwrapExternalObject.INSTANCE; |
| 467 |
|
| 468 |
} else if (itemType instanceof ErrorType) { |
| 469 |
return ToNull.INSTANCE; |
| 470 |
|
| 471 |
} else if (itemType instanceof NodeTest) { |
| 472 |
if (NodeInfo.class.isAssignableFrom(targetClass)) { |
| 473 |
return Identity.INSTANCE; |
| 474 |
} else { |
| 475 |
return General.INSTANCE; |
| 476 |
} |
| 477 |
|
| 478 |
} else { |
| 479 |
|
| 480 |
return General.INSTANCE; |
| 481 |
} |
| 482 |
} else { |
| 483 |
|
| 484 |
return General.INSTANCE; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
private static XPathException cannotConvert(ItemType source, Class target, Configuration config) { |
| 489 |
return new XPathException("Cannot convert from " + source + |
| 490 |
" to " + target.getName()); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public static PJConverter allocateNodeListCreator(Configuration config, Object node) { |
| 504 |
List<ExternalObjectModel> externalObjectModels = config.getExternalObjectModels(); |
| 505 |
for (ExternalObjectModel model : externalObjectModels) { |
| 506 |
PJConverter converter = model.getNodeListCreator(node); |
| 507 |
if (converter != null) { |
| 508 |
return converter; |
| 509 |
} |
| 510 |
} |
| 511 |
return ToCollection.INSTANCE; |
| 512 |
} |
| 513 |
|
| 514 |
public static class ToSequenceIterator extends PJConverter { |
| 515 |
|
| 516 |
public static final ToSequenceIterator INSTANCE = new ToSequenceIterator(); |
| 517 |
|
| 518 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 519 |
return value.iterate(); |
| 520 |
} |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
public static class ToNull extends PJConverter { |
| 525 |
|
| 526 |
public static final ToNull INSTANCE = new ToNull(); |
| 527 |
|
| 528 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 529 |
return null; |
| 530 |
} |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
public static class ToSequenceExtent extends PJConverter { |
| 535 |
|
| 536 |
public static final ToSequenceExtent INSTANCE = new ToSequenceExtent(); |
| 537 |
|
| 538 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 539 |
return value.iterate().materialize(); |
| 540 |
} |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public static class ToCollection extends PJConverter { |
| 550 |
|
| 551 |
public static final ToCollection INSTANCE = new ToCollection(); |
| 552 |
|
| 553 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 554 |
Collection<Object> list; |
| 555 |
if (targetClass.isAssignableFrom(ArrayList.class)) { |
| 556 |
list = new ArrayList<>(100); |
| 557 |
} else { |
| 558 |
try { |
| 559 |
list = (Collection<Object>)targetClass.newInstance(); |
| 560 |
} catch (InstantiationException e) { |
| 561 |
XPathException de = new XPathException("Cannot instantiate collection class " + targetClass); |
| 562 |
de.setXPathContext(context); |
| 563 |
throw de; |
| 564 |
} catch (IllegalAccessException e) { |
| 565 |
XPathException de = new XPathException("Cannot access collection class " + targetClass); |
| 566 |
de.setXPathContext(context); |
| 567 |
throw de; |
| 568 |
} |
| 569 |
} |
| 570 |
Configuration config = context.getConfiguration(); |
| 571 |
SequenceIterator<?> iter = value.iterate(); |
| 572 |
Item<?> it; |
| 573 |
while ((it = iter.next()) != null) { |
| 574 |
if (it instanceof AtomicValue) { |
| 575 |
PJConverter pj = allocate( |
| 576 |
config, ((AtomicValue) it).getItemType(), StaticProperty.EXACTLY_ONE, Object.class); |
| 577 |
list.add(pj.convert(it, Object.class, context)); |
| 578 |
} else if (it instanceof VirtualNode) { |
| 579 |
list.add(((VirtualNode) it).getRealNode()); |
| 580 |
} else { |
| 581 |
list.add(it); |
| 582 |
} |
| 583 |
} |
| 584 |
return list; |
| 585 |
} |
| 586 |
|
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public static class ToArray extends PJConverter { |
| 594 |
|
| 595 |
private PJConverter itemConverter; |
| 596 |
|
| 597 |
public ToArray(PJConverter itemConverter) { |
| 598 |
this.itemConverter = itemConverter; |
| 599 |
} |
| 600 |
|
| 601 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 602 |
if (value instanceof ExternalObject && targetClass.isAssignableFrom(((ExternalObject) value).getObject().getClass())) { |
| 603 |
return ((ExternalObject) value).getObject(); |
| 604 |
} |
| 605 |
Class componentClass = targetClass.getComponentType(); |
| 606 |
List<Object> list = new ArrayList<>(20); |
| 607 |
SequenceIterator<?> iter = value.iterate(); |
| 608 |
Item<?> item; |
| 609 |
while ((item = iter.next()) != null) { |
| 610 |
Object obj = itemConverter.convert(item, componentClass, context); |
| 611 |
if (obj != null) { |
| 612 |
list.add(obj); |
| 613 |
} |
| 614 |
} |
| 615 |
Object array = Array.newInstance(componentClass, list.size()); |
| 616 |
for (int i = 0; i < list.size(); i++) { |
| 617 |
Array.set(array, i, list.get(i)); |
| 618 |
} |
| 619 |
return array; |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
public static class ToOne extends PJConverter { |
| 631 |
|
| 632 |
public static final ToOne INSTANCE = new ToOne(); |
| 633 |
|
| 634 |
public One<Item<?>> convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 635 |
|
| 636 |
return new One<>(value.head()); |
| 637 |
} |
| 638 |
|
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
public static class ToZeroOrOne extends PJConverter { |
| 647 |
|
| 648 |
public static final ToZeroOrOne INSTANCE = new ToZeroOrOne(); |
| 649 |
|
| 650 |
public ZeroOrOne<Item<?>> convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 651 |
|
| 652 |
return new ZeroOrOne<>(value.head()); |
| 653 |
} |
| 654 |
|
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public static class ToOneOrMore extends PJConverter { |
| 663 |
|
| 664 |
public static final ToOneOrMore INSTANCE = new ToOneOrMore(); |
| 665 |
|
| 666 |
public OneOrMore<?> convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 667 |
return OneOrMore.makeOneOrMore(value); |
| 668 |
} |
| 669 |
|
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
public static class ToZeroOrMore extends PJConverter { |
| 679 |
|
| 680 |
public static final ToZeroOrMore INSTANCE = new ToZeroOrMore(); |
| 681 |
|
| 682 |
public ZeroOrMore<Item<?>> convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 683 |
return new ZeroOrMore<>(value.iterate()); |
| 684 |
} |
| 685 |
|
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
public static class Identity extends PJConverter { |
| 690 |
|
| 691 |
public static final Identity INSTANCE = new Identity(); |
| 692 |
|
| 693 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 694 |
if (value instanceof Closure) { |
| 695 |
value = ((Closure) value).reduce(); |
| 696 |
} |
| 697 |
if (value instanceof ZeroOrOne) { |
| 698 |
value = ((ZeroOrOne<?>) value).head(); |
| 699 |
} |
| 700 |
if (value instanceof VirtualNode) { |
| 701 |
Object obj = ((VirtualNode) value).getRealNode(); |
| 702 |
if (targetClass.isAssignableFrom(obj.getClass())) { |
| 703 |
return obj; |
| 704 |
} |
| 705 |
} |
| 706 |
if (targetClass.isAssignableFrom(value.getClass())) { |
| 707 |
return value; |
| 708 |
} else { |
| 709 |
GroundedValue gv = value.materialize(); |
| 710 |
if (targetClass.isAssignableFrom(gv.getClass())) { |
| 711 |
return gv; |
| 712 |
} |
| 713 |
gv = gv.reduce(); |
| 714 |
if (targetClass.isAssignableFrom(gv.getClass())) { |
| 715 |
return gv; |
| 716 |
} |
| 717 |
if (gv.getLength() == 0) { |
| 718 |
return null; |
| 719 |
} else { |
| 720 |
throw new XPathException("Cannot convert value " + value.getClass() + " of type " + |
| 721 |
SequenceTool.getItemType(value, context.getConfiguration().getTypeHierarchy()) + |
| 722 |
" to class " + targetClass.getName()); |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
public static class UnwrapExternalObject extends PJConverter { |
| 730 |
|
| 731 |
public static final UnwrapExternalObject INSTANCE = new UnwrapExternalObject(); |
| 732 |
|
| 733 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 734 |
Item head = value.head(); |
| 735 |
if (head == null) { |
| 736 |
return null; |
| 737 |
} |
| 738 |
if (!(head instanceof ExternalObject)) { |
| 739 |
if (Sequence.class.isAssignableFrom(targetClass)) { |
| 740 |
head = new ObjectValue<>(value); |
| 741 |
} else { |
| 742 |
throw new XPathException("Expected external object of class " + targetClass.getName() + |
| 743 |
", got " + head.getClass()); |
| 744 |
} |
| 745 |
} |
| 746 |
Object obj = ((ExternalObject) head).getObject(); |
| 747 |
if (!targetClass.isAssignableFrom(obj.getClass())) { |
| 748 |
throw new XPathException("External object has wrong class (is " |
| 749 |
+ obj.getClass().getName() + ", expected " + targetClass.getName() + ")"); |
| 750 |
} |
| 751 |
return obj; |
| 752 |
} |
| 753 |
|
| 754 |
} |
| 755 |
|
| 756 |
public static class StringValueToString extends PJConverter { |
| 757 |
|
| 758 |
public static final StringValueToString INSTANCE = new StringValueToString(); |
| 759 |
|
| 760 |
public String convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 761 |
Item first = value.head(); |
| 762 |
return first == null ? null : first.getStringValue(); |
| 763 |
} |
| 764 |
|
| 765 |
} |
| 766 |
|
| 767 |
public static class StringValueToChar extends PJConverter { |
| 768 |
|
| 769 |
public static final StringValueToChar INSTANCE = new StringValueToChar(); |
| 770 |
|
| 771 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 772 |
Item first = value.head(); |
| 773 |
if (first == null) { |
| 774 |
return null; |
| 775 |
} |
| 776 |
String str = first.getStringValue(); |
| 777 |
if (str.length() == 1) { |
| 778 |
return str.charAt(0); |
| 779 |
} else { |
| 780 |
XPathException de = new XPathException("Cannot convert xs:string to Java char unless length is 1"); |
| 781 |
de.setXPathContext(context); |
| 782 |
de.setErrorCode(SaxonErrorCode.SXJE0005); |
| 783 |
throw de; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
public static class BooleanValueToBoolean extends PJConverter { |
| 791 |
|
| 792 |
public static final BooleanValueToBoolean INSTANCE = new BooleanValueToBoolean(); |
| 793 |
|
| 794 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 795 |
BooleanValue bv = (BooleanValue) value.head(); |
| 796 |
assert bv != null; |
| 797 |
return bv.getBooleanValue(); |
| 798 |
} |
| 799 |
|
| 800 |
} |
| 801 |
|
| 802 |
public static class IntegerValueToBigInteger extends PJConverter { |
| 803 |
|
| 804 |
public static final IntegerValueToBigInteger INSTANCE = new IntegerValueToBigInteger(); |
| 805 |
|
| 806 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 807 |
IntegerValue val = (IntegerValue) value.head(); |
| 808 |
return val == null ? null : val.asBigInteger(); |
| 809 |
} |
| 810 |
|
| 811 |
} |
| 812 |
|
| 813 |
public static class IntegerValueToLong extends PJConverter { |
| 814 |
|
| 815 |
public static final IntegerValueToLong INSTANCE = new IntegerValueToLong(); |
| 816 |
|
| 817 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 818 |
IntegerValue iv = (IntegerValue) value.head(); |
| 819 |
assert iv != null; |
| 820 |
return iv.longValue(); |
| 821 |
} |
| 822 |
|
| 823 |
} |
| 824 |
|
| 825 |
public static class IntegerValueToInt extends PJConverter { |
| 826 |
|
| 827 |
public static final IntegerValueToInt INSTANCE = new IntegerValueToInt(); |
| 828 |
|
| 829 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 830 |
IntegerValue iv = (IntegerValue) value.head(); |
| 831 |
assert iv != null; |
| 832 |
return (int) iv.longValue(); |
| 833 |
} |
| 834 |
|
| 835 |
} |
| 836 |
|
| 837 |
public static class IntegerValueToShort extends PJConverter { |
| 838 |
|
| 839 |
public static final IntegerValueToShort INSTANCE = new IntegerValueToShort(); |
| 840 |
|
| 841 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 842 |
IntegerValue iv = (IntegerValue) value.head(); |
| 843 |
assert iv != null; |
| 844 |
return (short) iv.longValue(); |
| 845 |
} |
| 846 |
|
| 847 |
} |
| 848 |
|
| 849 |
public static class IntegerValueToByte extends PJConverter { |
| 850 |
|
| 851 |
public static final IntegerValueToByte INSTANCE = new IntegerValueToByte(); |
| 852 |
|
| 853 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 854 |
IntegerValue iv = (IntegerValue) value.head(); |
| 855 |
assert iv != null; |
| 856 |
return (byte) iv.longValue(); |
| 857 |
} |
| 858 |
|
| 859 |
} |
| 860 |
|
| 861 |
public static class IntegerValueToChar extends PJConverter { |
| 862 |
|
| 863 |
public static final IntegerValueToChar INSTANCE = new IntegerValueToChar(); |
| 864 |
|
| 865 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 866 |
IntegerValue iv = (IntegerValue) value.head(); |
| 867 |
assert iv != null; |
| 868 |
return (char) iv.longValue(); |
| 869 |
} |
| 870 |
|
| 871 |
} |
| 872 |
|
| 873 |
public static class NumericValueToBigDecimal extends PJConverter { |
| 874 |
|
| 875 |
public static final NumericValueToBigDecimal INSTANCE = new NumericValueToBigDecimal(); |
| 876 |
|
| 877 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 878 |
NumericValue nv = (NumericValue) value.head(); |
| 879 |
return nv == null ? null : nv.getDecimalValue(); |
| 880 |
} |
| 881 |
|
| 882 |
} |
| 883 |
|
| 884 |
public static class NumericValueToDouble extends PJConverter { |
| 885 |
|
| 886 |
public static final NumericValueToDouble INSTANCE = new NumericValueToDouble(); |
| 887 |
|
| 888 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 889 |
NumericValue nv = (NumericValue) value.head(); |
| 890 |
assert nv != null; |
| 891 |
return nv.getDoubleValue(); |
| 892 |
} |
| 893 |
|
| 894 |
} |
| 895 |
|
| 896 |
public static class NumericValueToFloat extends PJConverter { |
| 897 |
|
| 898 |
public static final NumericValueToFloat INSTANCE = new NumericValueToFloat(); |
| 899 |
|
| 900 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 901 |
NumericValue nv = (NumericValue) value.head(); |
| 902 |
assert nv != null; |
| 903 |
return nv.getFloatValue(); |
| 904 |
} |
| 905 |
|
| 906 |
} |
| 907 |
|
| 908 |
public static class AnyURIValueToURI extends PJConverter { |
| 909 |
|
| 910 |
public static final AnyURIValueToURI INSTANCE = new AnyURIValueToURI(); |
| 911 |
|
| 912 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 913 |
AnyURIValue av = (AnyURIValue) value.head(); |
| 914 |
try { |
| 915 |
return av == null ? null : new URI(((AnyURIValue) value).getStringValue()); |
| 916 |
} catch (URISyntaxException err) { |
| 917 |
throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URI"); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
} |
| 922 |
|
| 923 |
public static class AnyURIValueToURL extends PJConverter { |
| 924 |
|
| 925 |
public static final AnyURIValueToURL INSTANCE = new AnyURIValueToURL(); |
| 926 |
|
| 927 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 928 |
AnyURIValue av = (AnyURIValue) value.head(); |
| 929 |
try { |
| 930 |
return av == null ? null : new URL(((AnyURIValue) value).getStringValue()); |
| 931 |
} catch (MalformedURLException err) { |
| 932 |
throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URL"); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
} |
| 937 |
|
| 938 |
public static class QualifiedNameValueToQName extends PJConverter { |
| 939 |
|
| 940 |
public static final QualifiedNameValueToQName INSTANCE = new QualifiedNameValueToQName(); |
| 941 |
|
| 942 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 943 |
QualifiedNameValue qv = (QualifiedNameValue) value.head(); |
| 944 |
return qv == null ? null : qv.toJaxpQName(); |
| 945 |
} |
| 946 |
|
| 947 |
} |
| 948 |
|
| 949 |
public static class CalendarValueToInstant extends PJConverter { |
| 950 |
|
| 951 |
public static final CalendarValueToInstant INSTANCE = new CalendarValueToInstant(); |
| 952 |
|
| 953 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 954 |
CalendarValue cv = (CalendarValue) value.head(); |
| 955 |
return cv == null ? null : cv.toDateTime().toJavaInstant(); |
| 956 |
} |
| 957 |
|
| 958 |
} |
| 959 |
|
| 960 |
public static class CalendarValueToZonedDateTime extends PJConverter { |
| 961 |
|
| 962 |
public static final CalendarValueToZonedDateTime INSTANCE = new CalendarValueToZonedDateTime(); |
| 963 |
|
| 964 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 965 |
CalendarValue cv = (CalendarValue) value.head(); |
| 966 |
return cv == null ? null : cv.toDateTime().toZonedDateTime(); |
| 967 |
} |
| 968 |
|
| 969 |
} |
| 970 |
|
| 971 |
public static class CalendarValueToDate extends PJConverter { |
| 972 |
|
| 973 |
public static final CalendarValueToDate INSTANCE = new CalendarValueToDate(); |
| 974 |
|
| 975 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 976 |
CalendarValue cv = (CalendarValue) value.head(); |
| 977 |
return cv == null ? null : cv.getCalendar().getTime(); |
| 978 |
} |
| 979 |
|
| 980 |
} |
| 981 |
|
| 982 |
public static class CalendarValueToCalendar extends PJConverter { |
| 983 |
|
| 984 |
public static final CalendarValueToCalendar INSTANCE = new CalendarValueToCalendar(); |
| 985 |
|
| 986 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 987 |
CalendarValue cv = (CalendarValue) value.head(); |
| 988 |
return cv == null ? null : cv.getCalendar(); |
| 989 |
} |
| 990 |
|
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
public static class Atomic extends PJConverter { |
| 1034 |
|
| 1035 |
public static final Atomic INSTANCE = new Atomic(); |
| 1036 |
|
| 1037 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 1038 |
|
| 1039 |
AtomicValue item = (AtomicValue) value.head(); |
| 1040 |
if (item == null) { |
| 1041 |
return null; |
| 1042 |
} |
| 1043 |
Configuration config = context.getConfiguration(); |
| 1044 |
PJConverter converter = allocate( |
| 1045 |
config, item.getItemType(), StaticProperty.EXACTLY_ONE, targetClass); |
| 1046 |
return converter.convert(item, targetClass, context); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
public static class General extends PJConverter { |
| 1056 |
|
| 1057 |
public static final General INSTANCE = new General(); |
| 1058 |
|
| 1059 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) |
| 1060 |
throws XPathException { |
| 1061 |
Configuration config = context.getConfiguration(); |
| 1062 |
GroundedValue<?> gv = value.materialize(); |
| 1063 |
PJConverter converter = allocate( |
| 1064 |
config, SequenceTool.getItemType(gv, config.getTypeHierarchy()), SequenceTool.getCardinality(gv), targetClass); |
| 1065 |
if (converter instanceof General) { |
| 1066 |
converter = Identity.INSTANCE; |
| 1067 |
} |
| 1068 |
return converter.convert(gv, targetClass, context); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|