*/
public static PJConverter allocate(Configuration config, ItemType itemType,
int cardinality, Class targetClass)
throws XPathException {
TypeHierarchy th = config.getTypeHierarchy();
if (targetClass == SequenceIterator.class) {
return ToSequenceIterator.INSTANCE;
}
if (targetClass == ValueRepresentation.class || targetClass == Item.class) {
return Identity.INSTANCE;
}
if (targetClass == Value.class | targetClass == SequenceExtent.class) {
return ToSequenceExtent.INSTANCE;
}
if (!itemType.isAtomicType()) {
List<ExternalObjectModel> externalObjectModels = config.getExternalObjectModels();
for (int m=0; m<externalObjectModels.size(); m++) {
ExternalObjectModel model = externalObjectModels.get(m);
PJConverter converter = model.getPJConverter(targetClass);
if (converter != null) {
return converter;
}
}
if (NodeInfo.class.isAssignableFrom(targetClass)) {
return Identity.INSTANCE;
}
}
if (Collection.class.isAssignableFrom(targetClass)) {
return ToCollection.INSTANCE;
}
if (targetClass.isArray()) {
PJConverter itemConverter =
allocate(config, itemType, StaticProperty.EXACTLY_ONE, targetClass.getComponentType());
return new ToArray(itemConverter);
}
if (!Cardinality.allowsMany(cardinality)) {
if (itemType.isAtomicType()) {
if (th.isSubType(itemType, BuiltInAtomicType.STRING)) {
if (targetClass == Object.class || targetClass == String.class || targetClass == CharSequence.class) {
return StringValueToString.INSTANCE;
} else if (targetClass.isAssignableFrom(StringValue.class)) {
return Identity.INSTANCE;
} else if (targetClass == char.class || targetClass == Character.class) {
return StringValueToChar.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (itemType == BuiltInAtomicType.UNTYPED_ATOMIC) {
if (targetClass == Object.class || targetClass == String.class || targetClass == CharSequence.class) {
return StringValueToString.INSTANCE;
} else if (targetClass.isAssignableFrom(UntypedAtomicValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.BOOLEAN)) {
if (targetClass == Object.class || targetClass == Boolean.class || targetClass == boolean.class) {
return BooleanValueToBoolean.INSTANCE;
} else if (targetClass.isAssignableFrom(BooleanValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.INTEGER)) {
if (targetClass == Object.class || targetClass == BigInteger.class) {
return IntegerValueToBigInteger.INSTANCE;
} else if (targetClass == long.class || targetClass == Long.class) {
return IntegerValueToLong.INSTANCE;
} else if (targetClass == int.class || targetClass == Integer.class) {
return IntegerValueToInt.INSTANCE;
} else if (targetClass == short.class || targetClass == Short.class) {
return IntegerValueToShort.INSTANCE;
} else if (targetClass == byte.class || targetClass == Byte.class) {
return IntegerValueToByte.INSTANCE;
} else if (targetClass == char.class || targetClass == Character.class) {
return IntegerValueToChar.INSTANCE;
} else if (targetClass == double.class || targetClass == Double.class) {
return NumericValueToDouble.INSTANCE;
} else if (targetClass == float.class || targetClass == Float.class) {
return NumericValueToFloat.INSTANCE;
} else if (targetClass == BigDecimal.class) {
return NumericValueToBigDecimal.INSTANCE;
} else if (targetClass.isAssignableFrom(IntegerValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.DECIMAL)) {
if (targetClass == Object.class || targetClass == BigDecimal.class) {
return NumericValueToBigDecimal.INSTANCE;
} else if (targetClass == double.class || targetClass == Double.class) {
return NumericValueToDouble.INSTANCE;
} else if (targetClass == float.class || targetClass == Float.class) {
return NumericValueToFloat.INSTANCE;
} else if (targetClass.isAssignableFrom(DecimalValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.FLOAT)) {
if (targetClass == Object.class || targetClass == Float.class || targetClass == float.class) {
return NumericValueToFloat.INSTANCE;
} else if (targetClass == double.class || targetClass == Double.class) {
return NumericValueToDouble.INSTANCE;
} else if (targetClass.isAssignableFrom(FloatValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.DOUBLE)) {
if (targetClass == Object.class || targetClass == Double.class || targetClass == double.class) {
return NumericValueToDouble.INSTANCE;
} else if (targetClass.isAssignableFrom(DoubleValue.class)) {
return Identity.INSTANCE;
} else {
return Atomic.INSTANCE;
}
} else if (th.isSubType(itemType, BuiltInAtomicType.ANY_URI)) {
if (targetClass == Object.class || URI.class.isAssignableFrom(targetClass)) {
return AnyURIValueToURI.INSTANCE;
} else if (URL.class.isAssignableFrom(targetClass)) {
return AnyURIValueToURL.INSTANCE;
} else if (targetClass == String.class || targetClass == CharSequence.class) {
return StringValueToString.INSTANCE;
} else if (targetClass.isAssignableFrom(AnyURIValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.QNAME)) {
if (targetClass == Object.class || targetClass == javax.xml.namespace.QName.class) {
// Note JDK1.5 dependency
return QualifiedNameValueToQName.INSTANCE;
} else if (targetClass.isAssignableFrom(QNameValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.NOTATION)) {
if (targetClass == Object.class || targetClass == javax.xml.namespace.QName.class) {
// Note JDK1.5 dependency
return QualifiedNameValueToQName.INSTANCE;
} else if (targetClass.isAssignableFrom(NotationValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.DURATION)) {
if (targetClass.isAssignableFrom(DurationValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.DATE_TIME)) {
if (targetClass.isAssignableFrom(DateTimeValue.class)) {
return Identity.INSTANCE;
} else if (targetClass == java.util.Date.class) {
return CalendarValueToDate.INSTANCE;
} else if (targetClass == java.util.Calendar.class) {
return CalendarValueToCalendar.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.DATE)) {
if (targetClass.isAssignableFrom(DateValue.class)) {
return Identity.INSTANCE;
} else if (targetClass == java.util.Date.class) {
return CalendarValueToDate.INSTANCE;
} else if (targetClass == java.util.Calendar.class) {
return CalendarValueToCalendar.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.TIME)) {
if (targetClass.isAssignableFrom(TimeValue.class)) {
return Identity.INSTANCE;
} else if (targetClass == java.util.Date.class) {
return CalendarValueToDate.INSTANCE;
} else if (targetClass == java.util.Calendar.class) {
return CalendarValueToCalendar.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.G_YEAR)) {
if (targetClass.isAssignableFrom(GYearValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.G_YEAR_MONTH)) {
if (targetClass.isAssignableFrom(GYearMonthValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.G_MONTH)) {
if (targetClass.isAssignableFrom(GMonthValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.G_MONTH_DAY)) {
if (targetClass.isAssignableFrom(GMonthDayValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.G_DAY)) {
if (targetClass.isAssignableFrom(GDayValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.BASE64_BINARY)) {
if (targetClass.isAssignableFrom(Base64BinaryValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}
} else if (th.isSubType(itemType, BuiltInAtomicType.HEX_BINARY)) {
if (targetClass.isAssignableFrom(HexBinaryValue.class)) {
return Identity.INSTANCE;
} else {
throw cannotConvert(itemType, targetClass, config);
}