Class Converter

java.lang.Object
net.sf.saxon.type.Converter
Direct Known Subclasses:
AtomicSequenceConverter.UntypedConverter, Converter.Base64BinaryToHexBinary, Converter.BooleanToDecimal, Converter.BooleanToDouble, Converter.BooleanToFloat, Converter.BooleanToInteger, Converter.DateTimeToDate, Converter.DateTimeToGDay, Converter.DateTimeToGMonth, Converter.DateTimeToGMonthDay, Converter.DateTimeToGYear, Converter.DateTimeToGYearMonth, Converter.DateTimeToTime, Converter.DateToDateTime, Converter.DecimalToInteger, Converter.DoubleToDecimal, Converter.DoubleToInteger, Converter.DownCastingConverter, Converter.DurationToDayTimeDuration, Converter.DurationToYearMonthDuration, Converter.FloatToDecimal, Converter.FloatToInteger, Converter.HexBinaryToBase64Binary, Converter.IntegerToDecimal, Converter.NotationToQName, Converter.NumericToBoolean, Converter.NumericToDecimal, Converter.NumericToDouble, Converter.NumericToFloat, Converter.NumericToInteger, Converter.PromoterToDouble, Converter.PromoterToFloat, Converter.QNameToNotation, Converter.ToStringConverter, Converter.ToUntypedAtomicConverter, Converter.UpCastingConverter, StringConverter

public abstract class Converter extends Object
A 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 BOOLEAN_TO_DOUBLE. In other cases the converter is paramaterized by the ConversionRules object, 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

  • Field Details

  • Constructor Details

    • 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 Details

    • 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 converted
      targetType - the type to which the value is to be converted
      rules - the conversion rules for the configuration
      Returns:
      the converted value
      Throws:
      ValidationException - if conversion fails
    • getConverter

      public static Converter getConverter(AtomicType sourceType, AtomicType targetType, ConversionRules rules)
      Get a converter that handles conversion from one primitive type to another.

      This method is intended for internal use only. The approved way to get a converter is using the factory method ConversionRules.getConverter(net.sf.saxon.type.AtomicType, net.sf.saxon.type.AtomicType)}

      Parameters:
      sourceType - the fingerprint of the source primitive type
      targetType - the fingerprint of the target primitive type
      rules - the conversion rules to be applied
      Returns:
      the converter if one is available; or null otherwise
    • 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
      Returns:
      the result of the conversion, as an AtomicValue, if conversion succeeds, or a ValidationFailure object 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
    • isXPath30Conversion

      public boolean isXPath30Conversion()
      Ask if this converter implements a conversion that requires XPath 3.0 (or XQuery 3.0 etc) to be enabled
      Returns:
      true if XPath 3.0 support is required
    • isAlwaysSuccessful

      public boolean isAlwaysSuccessful()
      Ask if this converter will always succeed
      Returns:
      true if this Converter will never return a ValidationFailure
    • setNamespaceResolver

      public void 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
    • getNamespaceResolver

      public NamespaceResolver getNamespaceResolver()
      Get the namespace resolver if one has been supplied
      Returns:
      the namespace resolver, or null if none has been supplied