Package org.apache.sis.internal.xml
Class XmlUtilities
java.lang.Object
org.apache.sis.internal.system.SystemListener
org.apache.sis.internal.xml.XmlUtilities
- All Implemented Interfaces:
EventListener
Utilities methods related to XML.
- Since:
- 0.3
- Version:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static DatatypeFactory
The factory for creatingjavax.xml.datatype
objects.private static final ChronoField[]
Temporal fields that may be copied intoXMLGregorianCalendar
.private static final ObjIntConsumer<XMLGregorianCalendar>[]
Setter methods to invoke for setting the value of a temporal field on aXMLGregorianCalendar
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Invoked when the classpath changed.private static GregorianCalendar
createGregorianCalendar
(Context context) Creates a new Gregorian calendar for the current timezone and locale.static DatatypeFactory
Returns the factory for creatingjavax.xml.datatype
objects.static Date
toDate
(Context context, XMLGregorianCalendar xml) Converts the given XML Gregorian calendar to a date.static Temporal
toTemporal
(Context context, XMLGregorianCalendar xml) Converts the given XML Gregorian calendar to a temporal object.static XMLGregorianCalendar
Converts the given temporal object to a XML Gregorian calendar.static XMLGregorianCalendar
Converts the given date to a XML Gregorian calendar using the locale and timezone from the current marshalling context.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).private static int
zeroIfUndef
(int value) Replaces undefined value by zero.Methods inherited from class org.apache.sis.internal.system.SystemListener
add, databaseChanged, fireClasspathChanged, remove
-
Field Details
-
factory
The factory for creatingjavax.xml.datatype
objects. -
FIELDS
Temporal fields that may be copied intoXMLGregorianCalendar
. -
SETTERS
Setter methods to invoke for setting the value of a temporal field on aXMLGregorianCalendar
. Indices in this array must correspond to indices in theFIELDS
array.
-
-
Constructor Details
-
XmlUtilities
private XmlUtilities()For internal usage only.
-
-
Method Details
-
classpathChanged
protected void classpathChanged()Invoked when the classpath changed. This method resets thefactory
tonull
in order to force the search for a new instance.- Specified by:
classpathChanged
in classSystemListener
-
getDatatypeFactory
Returns the factory for creatingjavax.xml.datatype
objects.- Returns:
- the factory (never
null
). - Throws:
DatatypeConfigurationException
- if the factory cannot be created.
-
trimTime
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 isfalse
, 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 returnstrue
. - Otherwise returns
false
. But before doing so, still set the milliseconds toFIELD_UNDEFINED
if its value was 0.
- If every time components (hour, minute, seconds and milliseconds) are zero, set
them to
- Otherwise (if the
force
argument isfalse
), then the temporal part is set toFIELD_UNDEFINED
unconditionally and this method returnstrue
.
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.
- If the
-
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, ornull
if none.date
- the date to convert to a XML calendar, ornull
.- Returns:
- the XML calendar, or
null
ifdate
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, ornull
if none.date
- the date to convert to a XML calendar, ornull
.- Returns:
- the XML calendar, or
null
ifdate
was null. - Throws:
DatatypeConfigurationException
- if the factory cannot be created.
-
createGregorianCalendar
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 theXML.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
Converts the given XML Gregorian calendar to a temporal object. The temporal object may beLocalDate
,LocalTime
,LocalDateTime
,OffsetDateTime
,Year
orYearMonth
depending on which fields are defined in the given calendar.- Parameters:
context
- the current (un)marshalling context, ornull
if none.xml
- the XML calendar to convert to a temporal object, ornull
.- Returns:
- the temporal object, or
null
ifxml
is null or has too many undefined fields.
-
toDate
Converts the given XML Gregorian calendar to a date.- Parameters:
context
- the current (un)marshalling context, ornull
if none.xml
- the XML calendar to convert to a date, ornull
.- Returns:
- the date, or
null
ifxml
was null.
-