Package net.sf.saxon.type
Class Converter
- java.lang.Object
-
- net.sf.saxon.type.Converter
-
- Direct Known Subclasses:
Converter.DoubleToDecimal,Converter.DoubleToInteger,Converter.DownCastingConverter,Converter.FloatToDecimal,Converter.FloatToInteger,Converter.IdentityConverter,Converter.NumericToDecimal,Converter.NumericToInteger,Converter.PromoterToDouble,Converter.PromoterToFloat,Converter.TwoPhaseConverter,Converter.UnfailingConverter,StringConverter,UntypedSequenceConverter.UntypedConverter
public abstract class Converter extends java.lang.ObjectA converter implements conversions from one atomic type to another - that is, it implements the casting rules for a (source type, destination type) pair.There is potentially one Converter implementation for each pair of (source, target) classes; though in many cases the same implementation handles a number of such pairs.
In cases where the conversion rules are fixed (specifically, where they do not depend on differences between versions of the XSD or QT specifications), the appropriate Converter can be obtained as a static constant, for example
Converter.BooleanToDouble.INSTANCE. In other cases the converter is parameterized by theConversionRulesobject, and should be obtained by calling the appropriate factory method on the ConversionRules.Where the source type of the conversion is xs:string, the converter will always be a subclass of
StringConverter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConverter.Base64BinaryToHexBinaryConverts base64 to hexBinarystatic classConverter.BooleanToDecimalConverts a boolean to a decimalstatic classConverter.BooleanToDoubleConverts a boolean to a doublestatic classConverter.BooleanToFloatConverts a boolean to an xs:floatstatic classConverter.BooleanToIntegerConverts a boolean to an integerstatic classConverter.DateTimeToDateConverts a dateTime to a datestatic classConverter.DateTimeToGDayConverts a dateTime to a gDaystatic classConverter.DateTimeToGMonthConverts a dateTime to a gMonthstatic classConverter.DateTimeToGMonthDayConverts a dateTime to a gMonthDaystatic classConverter.DateTimeToGYearConverts a dateTime to a gYearstatic classConverter.DateTimeToGYearMonthConverts a dateTime to a gYearMonthstatic classConverter.DateTimeToTimeConverts a dateTime to a timestatic classConverter.DateToDateTimeConverts a date to a dateTimestatic classConverter.DecimalToIntegerConverts a decimal to an integer.static classConverter.DoubleToDecimalConvers a double to a decimalstatic classConverter.DoubleToIntegerConverts a double to an integerstatic classConverter.DownCastingConverterConverter that checks that a value belonging to a supertype is a valid instance of a subtype, and returns an instance of the subtypestatic classConverter.DurationToDayTimeDurationConverts a duration to a dayTimeDurationstatic classConverter.DurationToYearMonthDurationConverts a duration to a yearMonthDurationstatic classConverter.FloatToDecimalConverts a float to a decimalstatic classConverter.FloatToIntegerConverts a float to an integerstatic classConverter.HexBinaryToBase64BinaryConverts hexBinary to base64Binarystatic classConverter.IdentityConverterConverter that does nothing - it returns the input unchangedstatic classConverter.IntegerToDecimalConverts an integer to a decimalstatic classConverter.NotationToQNameConverts Notation to QNamestatic classConverter.NumericToBooleanConverts a numeric value to a booleanstatic classConverter.NumericToDecimalConverts any numeric value to a decimalstatic classConverter.NumericToDoubleConverts any numeric value to a double.static classConverter.NumericToFloatConverts any numeric value to xs:floatstatic classConverter.NumericToIntegerConverts any numeric value to an integer.static classConverter.PromoterToDoubleConverter that implements the promotion rules to a required type of xs:doublestatic classConverter.PromoterToFloatConverter that implements the promotion rules to a required type of xs:floatstatic classConverter.QNameToNotationConverts QName to Notationstatic classConverter.ToStringConverterConverts any value to a stringstatic classConverter.ToUntypedAtomicConverterConverts any value to untyped atomicstatic classConverter.TwoPhaseConverterConverter that operates in two phases, via an intermediate typestatic classConverter.UnfailingConverterSpecialisation for converters that always succeedstatic classConverter.UpCastingConverterConverter that does nothing except change the type annotation of the value.
-
Constructor Summary
Constructors Modifier Constructor Description protectedConverter()protectedConverter(ConversionRules rules)Construct a converter with a given set of conversion rules.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract ConversionResultconvert(AtomicValue input)Convert an atomic value from the source type to the target typestatic AtomicValueconvert(AtomicValue value, AtomicType targetType, ConversionRules rules)Convenience method to convert a given value to a given type.ConversionRulesgetConversionRules()Get the conversion rules to be used by this ConverterNamespaceResolvergetNamespaceResolver()Get the namespace resolver if one has been suppliedbooleanisAlwaysSuccessful()Ask if this converter will always succeedvoidsetConversionRules(ConversionRules rules)Set the conversion rules to be used by this ConverterConvertersetNamespaceResolver(NamespaceResolver resolver)Provide a namespace resolver, needed for conversion to namespace-sensitive types such as QName and NOTATION.
-
-
-
Constructor Detail
-
Converter
protected Converter()
-
Converter
protected Converter(ConversionRules rules)
Construct a converter with a given set of conversion rules. For use in constructing subclasses- Parameters:
rules- the conversion rules for the configuration
-
-
Method Detail
-
convert
public static AtomicValue convert(AtomicValue value, AtomicType targetType, ConversionRules rules) throws ValidationException
Convenience method to convert a given value to a given type. Note: it is more efficient to obtain a converter in advance and to reuse it for multiple conversions- Parameters:
value- the value to be convertedtargetType- the type to which the value is to be convertedrules- the conversion rules for the configuration- Returns:
- the converted value
- Throws:
ValidationException- if conversion fails
-
convert
public abstract ConversionResult convert(AtomicValue input)
Convert an atomic value from the source type to the target type- Parameters:
input- the atomic value to be converted, which the caller guarantees to be of the appropriate type for the converter. The results are undefined if the value is of the wrong type; possible outcomes are (apparent) success, or a ClassCastException.- Returns:
- the result of the conversion, as an
AtomicValue, if conversion succeeds, or aValidationFailureobject describing the reasons for failure if conversion is not possible. Note that the ValidationFailure object is not (and does not contain) an exception, because it does not necessarily result in an error being thrown, and creating exceptions on non-failure paths is expensive.
-
setConversionRules
public final void setConversionRules(ConversionRules rules)
Set the conversion rules to be used by this Converter- Parameters:
rules- the conversion rules
-
getConversionRules
public final ConversionRules getConversionRules()
Get the conversion rules to be used by this Converter- Returns:
- the conversion rules
-
isAlwaysSuccessful
public boolean isAlwaysSuccessful()
Ask if this converter will always succeed- Returns:
- true if this Converter will never return a ValidationFailure
-
setNamespaceResolver
public Converter setNamespaceResolver(NamespaceResolver resolver)
Provide a namespace resolver, needed for conversion to namespace-sensitive types such as QName and NOTATION. The resolver is ignored if the target type is not namespace-sensitive- Parameters:
resolver- the namespace resolver to be used- Returns:
- a new Converter customised with the supplied namespace context. The original Converter is unchanged (see bug 2754)
-
getNamespaceResolver
public NamespaceResolver getNamespaceResolver()
Get the namespace resolver if one has been supplied- Returns:
- the namespace resolver, or null if none has been supplied
-
-