//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2018 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon.lib; import net.sf.saxon.expr.sort.LRUCache; import net.sf.saxon.om.NotationSet; import net.sf.saxon.om.StandardNames; import net.sf.saxon.type.*; import net.sf.saxon.value.StringToDouble11; /** * This class defines a set of rules for converting between different atomic types. It handles the variations * that arise between different versions of the W3C specifications, for example the changes in Name syntax * between XML 1.0 and XML 1.1, the introduction of "+INF" as a permitted xs:double value in XSD 1.1, and so on. *
It is possible to nominate a customized ConversionRules object at the level of the
* {@link net.sf.saxon.Configuration}, either by instantiating this class and changing the properties, or
* by subclassing.
This method is intended for internal use only. The approved way to get a converter is using the * factory method {@link net.sf.saxon.lib.ConversionRules#getConverter(net.sf.saxon.type.AtomicType, net.sf.saxon.type.AtomicType)}}
* * @param sourceType the type of the value to be converted * @param targetType the type of the result of the conversion * @return the converter if one is available; or null if no conversion is possible */ /*@Nullable*/ private Converter makeConverter(AtomicType sourceType, AtomicType targetType) { if (sourceType == targetType) { return StringConverter.IdentityConverter.INSTANCE; } int tt = targetType.getFingerprint(); int tp = targetType.getPrimitiveType(); int st = sourceType.getPrimitiveType(); if ((st == StandardNames.XS_STRING || st == StandardNames.XS_UNTYPED_ATOMIC) && (tp == StandardNames.XS_STRING || tp == StandardNames.XS_UNTYPED_ATOMIC)) { return makeStringConverter(targetType); } if (!targetType.isPrimitiveType()) { AtomicType primTarget = targetType.getPrimitiveItemType(); if (sourceType == primTarget) { return new Converter.DownCastingConverter(targetType, this); } else if (st == StandardNames.XS_STRING || st == StandardNames.XS_UNTYPED_ATOMIC) { return makeStringConverter(targetType); } else { Converter stageOne = makeConverter(sourceType, primTarget); if (stageOne == null) { return null; } Converter stageTwo = new Converter.DownCastingConverter(targetType, this); return new Converter.TwoPhaseConverter(stageOne, stageTwo); } } if (st == tt) { // we are casting between subtypes of the same primitive type. if (typeHierarchy != null && typeHierarchy.isSubType(sourceType, targetType)) { return new Converter.UpCastingConverter(targetType); } Converter upcast = new Converter.UpCastingConverter(sourceType.getPrimitiveItemType()); Converter downcast = new Converter.DownCastingConverter(targetType, this); return new Converter.TwoPhaseConverter(upcast, downcast); } switch (tt) { case StandardNames.XS_UNTYPED_ATOMIC: return Converter.ToUntypedAtomicConverter.INSTANCE; case StandardNames.XS_STRING: return Converter.ToStringConverter.INSTANCE; case StandardNames.XS_FLOAT: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToFloat(this); case StandardNames.XS_DOUBLE: case StandardNames.XS_DECIMAL: case StandardNames.XS_INTEGER: case StandardNames.XS_NUMERIC: return Converter.NumericToFloat.INSTANCE; case StandardNames.XS_BOOLEAN: return Converter.BooleanToFloat.INSTANCE; default: return null; } case StandardNames.XS_DOUBLE: case StandardNames.XS_NUMERIC: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return stringToDouble; case StandardNames.XS_FLOAT: case StandardNames.XS_DECIMAL: case StandardNames.XS_INTEGER: case StandardNames.XS_NUMERIC: return Converter.NumericToDouble.INSTANCE; case StandardNames.XS_BOOLEAN: return Converter.BooleanToDouble.INSTANCE; default: return null; } case StandardNames.XS_DECIMAL: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToDecimal.INSTANCE; case StandardNames.XS_FLOAT: return Converter.FloatToDecimal.INSTANCE; case StandardNames.XS_DOUBLE: return Converter.DoubleToDecimal.INSTANCE; case StandardNames.XS_INTEGER: return Converter.IntegerToDecimal.INSTANCE; case StandardNames.XS_NUMERIC: return Converter.NumericToDecimal.INSTANCE; case StandardNames.XS_BOOLEAN: return Converter.BooleanToDecimal.INSTANCE; default: return null; } case StandardNames.XS_INTEGER: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToInteger.INSTANCE; case StandardNames.XS_FLOAT: return Converter.FloatToInteger.INSTANCE; case StandardNames.XS_DOUBLE: return Converter.DoubleToInteger.INSTANCE; case StandardNames.XS_DECIMAL: return Converter.DecimalToInteger.INSTANCE; case StandardNames.XS_NUMERIC: return Converter.NumericToInteger.INSTANCE; case StandardNames.XS_BOOLEAN: return Converter.BooleanToInteger.INSTANCE; default: return null; } case StandardNames.XS_DURATION: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToDuration.INSTANCE; case StandardNames.XS_DAY_TIME_DURATION: case StandardNames.XS_YEAR_MONTH_DURATION: return new Converter.UpCastingConverter(BuiltInAtomicType.DURATION); default: return null; } case StandardNames.XS_YEAR_MONTH_DURATION: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToYearMonthDuration.INSTANCE; case StandardNames.XS_DURATION: case StandardNames.XS_DAY_TIME_DURATION: return Converter.DurationToYearMonthDuration.INSTANCE; default: return null; } case StandardNames.XS_DAY_TIME_DURATION: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToDayTimeDuration.INSTANCE; case StandardNames.XS_DURATION: case StandardNames.XS_YEAR_MONTH_DURATION: return Converter.DurationToDayTimeDuration.INSTANCE; default: return null; } case StandardNames.XS_DATE_TIME: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToDateTime(this); case StandardNames.XS_DATE: return Converter.DateToDateTime.INSTANCE; default: return null; } case StandardNames.XS_TIME: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToTime.INSTANCE; case StandardNames.XS_DATE_TIME: return Converter.DateTimeToTime.INSTANCE; default: return null; } case StandardNames.XS_DATE: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToDate(this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToDate.INSTANCE; default: return null; } case StandardNames.XS_G_YEAR_MONTH: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToGYearMonth(this); case StandardNames.XS_DATE: return Converter.TwoPhaseConverter.makeTwoPhaseConverter( BuiltInAtomicType.DATE, BuiltInAtomicType.DATE_TIME, BuiltInAtomicType.G_YEAR_MONTH, this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToGYearMonth.INSTANCE; default: return null; } case StandardNames.XS_G_YEAR: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToGYear(this); case StandardNames.XS_DATE: return Converter.TwoPhaseConverter.makeTwoPhaseConverter(BuiltInAtomicType.DATE, BuiltInAtomicType.DATE_TIME, BuiltInAtomicType.G_YEAR, this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToGYear.INSTANCE; default: return null; } case StandardNames.XS_G_MONTH_DAY: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToGMonthDay.INSTANCE; case StandardNames.XS_DATE: return Converter.TwoPhaseConverter.makeTwoPhaseConverter(BuiltInAtomicType.DATE, BuiltInAtomicType.DATE_TIME, BuiltInAtomicType.G_MONTH_DAY, this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToGMonthDay.INSTANCE; default: return null; } case StandardNames.XS_G_DAY: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToGDay.INSTANCE; case StandardNames.XS_DATE: return Converter.TwoPhaseConverter.makeTwoPhaseConverter(BuiltInAtomicType.DATE, BuiltInAtomicType.DATE_TIME, BuiltInAtomicType.G_DAY, this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToGDay.INSTANCE; default: return null; } case StandardNames.XS_G_MONTH: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToGMonth.INSTANCE; case StandardNames.XS_DATE: return Converter.TwoPhaseConverter.makeTwoPhaseConverter(BuiltInAtomicType.DATE, BuiltInAtomicType.DATE_TIME, BuiltInAtomicType.G_MONTH, this); case StandardNames.XS_DATE_TIME: return Converter.DateTimeToGMonth.INSTANCE; default: return null; } case StandardNames.XS_BOOLEAN: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToBoolean.INSTANCE; case StandardNames.XS_FLOAT: case StandardNames.XS_DOUBLE: case StandardNames.XS_DECIMAL: case StandardNames.XS_INTEGER: case StandardNames.XS_NUMERIC: return Converter.NumericToBoolean.INSTANCE; default: return null; } case StandardNames.XS_BASE64_BINARY: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToBase64Binary.INSTANCE; case StandardNames.XS_HEX_BINARY: return Converter.HexBinaryToBase64Binary.INSTANCE; default: return null; } case StandardNames.XS_HEX_BINARY: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return StringConverter.StringToHexBinary.INSTANCE; case StandardNames.XS_BASE64_BINARY: return Converter.Base64BinaryToHexBinary.INSTANCE; default: return null; } case StandardNames.XS_ANY_URI: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToAnyURI(this); default: return null; } case StandardNames.XS_QNAME: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToQName(this); case StandardNames.XS_NOTATION: return Converter.NotationToQName.INSTANCE; default: return null; } case StandardNames.XS_NOTATION: switch (st) { case StandardNames.XS_UNTYPED_ATOMIC: case StandardNames.XS_STRING: return new StringConverter.StringToNotation(this); case StandardNames.XS_QNAME: return Converter.QNameToNotation.INSTANCE; default: return null; } case StandardNames.XS_ANY_ATOMIC_TYPE: return StringConverter.IdentityConverter.INSTANCE; default: throw new IllegalArgumentException("Unknown primitive type " + tt); } } /** * Static factory method to get a StringConverter for a specific target type * * @param targetType the target type of the conversion * @return a StringConverter that can be used to convert strings to the target type, or to * validate strings against the target type */ /*@NotNull*/ private StringConverter makeStringConverter(/*@NotNull*/ final AtomicType targetType) { int tt = targetType.getPrimitiveType(); if (targetType.isBuiltInType()) { if (tt == StandardNames.XS_STRING) { switch (targetType.getFingerprint()) { case StandardNames.XS_STRING: return StringConverter.StringToString.INSTANCE; case StandardNames.XS_NORMALIZED_STRING: return StringConverter.StringToNormalizedString.INSTANCE; case StandardNames.XS_TOKEN: return StringConverter.StringToToken.INSTANCE; case StandardNames.XS_LANGUAGE: return StringConverter.StringToLanguage.INSTANCE; case StandardNames.XS_NAME: return StringConverter.StringToName.INSTANCE; case StandardNames.XS_NCNAME: return StringConverter.StringToNCName.TO_NCNAME; case StandardNames.XS_ID: return StringConverter.StringToNCName.TO_ID; case StandardNames.XS_IDREF: return StringConverter.StringToNCName.TO_IDREF; case StandardNames.XS_ENTITY: return StringConverter.StringToNCName.TO_ENTITY; case StandardNames.XS_NMTOKEN: return StringConverter.StringToNMTOKEN.INSTANCE; default: throw new AssertionError("Unknown built-in subtype of xs:string"); } } else if (tt == StandardNames.XS_UNTYPED_ATOMIC) { return StringConverter.StringToUntypedAtomic.INSTANCE; } else if (targetType.isPrimitiveType()) { // converter to built-in types unrelated to xs:string Converter converter = getConverter(BuiltInAtomicType.STRING, targetType); assert converter != null; return (StringConverter) converter; } else if (tt == StandardNames.XS_INTEGER) { return new StringConverter.StringToIntegerSubtype((BuiltInAtomicType) targetType); } else { switch (targetType.getFingerprint()) { case StandardNames.XS_DAY_TIME_DURATION: return StringConverter.StringToDayTimeDuration.INSTANCE; case StandardNames.XS_YEAR_MONTH_DURATION: return StringConverter.StringToYearMonthDuration.INSTANCE; case StandardNames.XS_DATE_TIME_STAMP: StringConverter first = new StringConverter.StringToDateTime(this); Converter.DownCastingConverter second = new Converter.DownCastingConverter(targetType, this); return new StringConverter.StringToNonStringDerivedType(first, second); default: throw new AssertionError("Unknown built in type " + targetType); } } } else { if (tt == StandardNames.XS_STRING) { if (targetType.getBuiltInBaseType() == BuiltInAtomicType.STRING) { // converter to user-defined subtypes of xs:string return new StringConverter.StringToStringSubtype(this, targetType); } else { // converter to user-defined subtypes of built-in subtypes of xs:string return new StringConverter.StringToDerivedStringSubtype(this, targetType); } } else { // converter to user-defined types derived from types other than xs:string StringConverter first = targetType.getPrimitiveItemType().getStringConverter(this); Converter.DownCastingConverter second = new Converter.DownCastingConverter(targetType, this); return new StringConverter.StringToNonStringDerivedType(first, second); } } } }