Class XmlUtilities

java.lang.Object
org.apache.sis.internal.system.SystemListener
org.apache.sis.internal.xml.XmlUtilities
All Implemented Interfaces:
EventListener

public final class XmlUtilities extends SystemListener
Utilities methods related to XML.
Since:
0.3
Version:
1.3
  • Field Details

  • Constructor Details

    • XmlUtilities

      private XmlUtilities()
      For internal usage only.
  • Method Details

    • classpathChanged

      protected void classpathChanged()
      Invoked when the classpath changed. This method resets the factory to null in order to force the search for a new instance.
      Specified by:
      classpathChanged in class SystemListener
    • getDatatypeFactory

      public static DatatypeFactory getDatatypeFactory() throws DatatypeConfigurationException
      Returns the factory for creating javax.xml.datatype objects.
      Returns:
      the factory (never null).
      Throws:
      DatatypeConfigurationException - if the factory cannot be created.
    • trimTime

      public static boolean trimTime(XMLGregorianCalendar gc, boolean force)
      Trims the time components of the given calendar if their values are zero, or leaves them unchanged otherwise (except for milliseconds). More specifically:
      • If the force argument is false, then:
        • If every time components (hour, minute, seconds and milliseconds) are zero, set them to FIELD_UNDEFINED in order to prevent them from being formatted at XML marshalling time. Then returns true.
        • Otherwise returns false. But before doing so, still set the milliseconds to FIELD_UNDEFINED if its value was 0.
      • Otherwise (if the force argument is false), then the temporal part is set to FIELD_UNDEFINED unconditionally and this method returns true.
      WARNING: The timezone information may be lost! This method is used mostly when the Gregorian Calendar were created from a Date, in which case we don't know if the time is really 0 or just unspecified. This method should be invoked only when we want to assume that a time of zero means "unspecified".

      This method will be deprecated after we implemented ISO 19108 in SIS.

      Parameters:
      gc - the date to modify in-place.
      force - true for forcing the temporal components to be removed without any check.
      Returns:
      true if the time part has been completely removed, false otherwise.
    • toXML

      public static XMLGregorianCalendar toXML(Context context, Temporal date) throws DatatypeConfigurationException
      Converts the given temporal object to a XML Gregorian calendar. The returned calendar may have undefined fields (including undefined time zone) if the corresponding information was not provided in the given temporal object.

      If the returned date has a time, then it usually has millisecond accuracy. Caller may want to clear the millisecond field if it is equal to zero.

      Parameters:
      context - the current (un)marshalling context, or null if none.
      date - the date to convert to a XML calendar, or null.
      Returns:
      the XML calendar, or null if date was null.
      Throws:
      DatatypeConfigurationException - if the factory cannot be created.
    • toXML

      public static XMLGregorianCalendar toXML(Context context, Date date) throws DatatypeConfigurationException
      Converts the given date to a XML Gregorian calendar using the locale and timezone from the current marshalling context. The returned date has millisecond accuracy. Caller may want to clear the millisecond field if it is equal to zero.
      Parameters:
      context - the current (un)marshalling context, or null if none.
      date - the date to convert to a XML calendar, or null.
      Returns:
      the XML calendar, or null if date was null.
      Throws:
      DatatypeConfigurationException - if the factory cannot be created.
    • createGregorianCalendar

      private static GregorianCalendar createGregorianCalendar(Context context)
      Creates a new Gregorian calendar for the current timezone and locale. If no locale or timezone were explicitly set, then the default ones are used as documented in the XML.TIMEZONE constant.
      Returns:
      a Gregorian calendar initialized with the current timezone and locale.
    • zeroIfUndef

      private static int zeroIfUndef(int value)
      Replaces undefined value by zero. Used for optional time fields.
    • toTemporal

      public static Temporal toTemporal(Context context, XMLGregorianCalendar xml)
      Converts the given XML Gregorian calendar to a temporal object. The temporal object may be LocalDate, LocalTime, LocalDateTime, OffsetDateTime, Year or YearMonth depending on which fields are defined in the given calendar.
      Parameters:
      context - the current (un)marshalling context, or null if none.
      xml - the XML calendar to convert to a temporal object, or null.
      Returns:
      the temporal object, or null if xml is null or has too many undefined fields.
    • toDate

      public static Date toDate(Context context, XMLGregorianCalendar xml)
      Converts the given XML Gregorian calendar to a date.
      Parameters:
      context - the current (un)marshalling context, or null if none.
      xml - the XML calendar to convert to a date, or null.
      Returns:
      the date, or null if xml was null.