286N/A/*
286N/A * Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage javax.xml.datatype;
286N/A
286N/Aimport java.math.BigInteger;
286N/Aimport java.math.BigDecimal;
286N/Aimport java.util.GregorianCalendar;
286N/Aimport java.util.regex.Matcher;
286N/Aimport java.util.regex.Pattern;
286N/A
286N/A/**
286N/A * <p>Factory that creates new <code>javax.xml.datatype</code> <code>Object</code>s that map XML to/from Java <code>Object</code>s.</p>
286N/A *
286N/A * <p><a name="DatatypeFactory.newInstance"/>{@link #newInstance()} is used to create a new <code>DatatypeFactory</code>.
286N/A * The following implementation resolution mechanisms are used in the following order:</p>
286N/A * <ol>
286N/A * <li>
286N/A * If the system property specified by {@link #DATATYPEFACTORY_PROPERTY}, "<code>javax.xml.datatype.DatatypeFactory</code>",
286N/A * exists, a class with the name of the property's value is instantiated.
286N/A * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
286N/A * </li>
286N/A * <li>
286N/A * If the file ${JAVA_HOME}/lib/jaxp.properties exists, it is loaded in a {@link java.util.Properties} <code>Object</code>.
286N/A * The <code>Properties</code> <code>Object </code> is then queried for the property as documented in the prior step
286N/A * and processed as documented in the prior step.
286N/A * </li>
286N/A * <li>
286N/A * The services resolution mechanism is used, e.g. <code>META-INF/services/java.xml.datatype.DatatypeFactory</code>.
286N/A * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
286N/A * </li>
286N/A * <li>
286N/A * The final mechanism is to attempt to instantiate the <code>Class</code> specified by
286N/A * {@link #DATATYPEFACTORY_IMPLEMENTATION_CLASS}.
286N/A * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
286N/A * </li>
286N/A * </ol>
286N/A *
286N/A * @author <a href="mailto:Joseph.Fialli@Sun.COM">Joseph Fialli</a>
286N/A * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
286N/A * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
286N/A *
286N/A * @version $Revision: 1.13 $, $Date: 2010/03/11 23:10:53 $
286N/A * @since 1.5
286N/A */
286N/Apublic abstract class DatatypeFactory {
286N/A
286N/A /**
286N/A * <p>Default property name as defined in JSR 206: Java(TM) API for XML Processing (JAXP) 1.3.</p>
286N/A *
286N/A * <p>Default value is <code>javax.xml.datatype.DatatypeFactory</code>.</p>
286N/A */
286N/A public static final String DATATYPEFACTORY_PROPERTY = "javax.xml.datatype.DatatypeFactory";
286N/A
286N/A /**
286N/A * <p>Default implementation class name as defined in
286N/A * <em>JSR 206: Java(TM) API for XML Processing (JAXP) 1.3</em>.</p>
286N/A *
286N/A * <p>Implementers should specify the name of an appropriate class
286N/A * to be instantiated if no other implementation resolution mechanism
286N/A * succeeds.</p>
286N/A *
286N/A * <p>Users should not refer to this field; it is intended only to
286N/A * document a factory implementation detail.
286N/A * </p>
286N/A */
286N/A public static final String DATATYPEFACTORY_IMPLEMENTATION_CLASS = new String("com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl");
286N/A
286N/A /**
286N/A * http://www.w3.org/TR/xpath-datamodel/#xdtschema defines two regexps
286N/A * to constrain the value space of dayTimeDuration ([^YM]*[DT].*)
286N/A * and yearMonthDuration ([^DT]*). Note that these expressions rely on
286N/A * the fact that the value must be an xs:Duration, they simply exclude
286N/A * some Durations.
286N/A */
286N/A private static final Pattern XDTSCHEMA_YMD =
286N/A Pattern.compile("[^DT]*");
286N/A
286N/A private static final Pattern XDTSCHEMA_DTD =
286N/A Pattern.compile("[^YM]*[DT].*");
286N/A
286N/A /**
286N/A * <p>Protected constructor to prevent instaniation outside of package.</p>
286N/A *
286N/A * <p>Use {@link #newInstance()} to create a <code>DatatypeFactory</code>.</p>
286N/A */
286N/A protected DatatypeFactory() {
286N/A }
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>DatatypeFactory</code>.</p>
286N/A *
286N/A * <p>The implementation resolution mechanisms are <a href="#DatatypeFactory.newInstance">defined</a> in this
286N/A * <code>Class</code>'s documentation.</p>
286N/A *
286N/A * @return New instance of a <code>DatatypeFactory</code>
286N/A *
286N/A * @throws DatatypeConfigurationException If the implementation is not
286N/A * available or cannot be instantiated.
286N/A *
286N/A * @see #newInstance(String factoryClassName, ClassLoader classLoader)
286N/A */
286N/A public static DatatypeFactory newInstance()
286N/A throws DatatypeConfigurationException {
286N/A
286N/A try {
286N/A return (DatatypeFactory) FactoryFinder.find(
286N/A /* The default property name according to the JAXP spec */
286N/A DATATYPEFACTORY_PROPERTY,
286N/A /* The fallback implementation class name */
286N/A DATATYPEFACTORY_IMPLEMENTATION_CLASS);
286N/A } catch (FactoryFinder.ConfigurationError e) {
286N/A throw new DatatypeConfigurationException(e.getMessage(), e.getException());
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>DatatypeFactory</code> from class name.
286N/A * This function is useful when there are multiple providers in the classpath.
286N/A * It gives more control to the application as it can specify which provider
286N/A * should be loaded.</p>
286N/A *
286N/A * <p>Once an application has obtained a reference to a <code>DatatypeFactory</code>
286N/A * it can use the factory to configure and obtain datatype instances.</P>
286N/A *
286N/A *
286N/A * <h2>Tip for Trouble-shooting</h2>
286N/A * <p>Setting the <code>jaxp.debug</code> system property will cause
286N/A * this method to print a lot of debug messages
286N/A * to <code>System.err</code> about what it is doing and where it is looking at.</p>
286N/A *
286N/A * <p> If you have problems try:</p>
286N/A * <pre>
286N/A * java -Djaxp.debug=1 YourProgram ....
286N/A * </pre>
286N/A *
286N/A * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.datatype.DatatypeFactory</code>.
286N/A *
286N/A * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
286N/A * current <code>Thread</code>'s context classLoader is used to load the factory class.
286N/A *
286N/A * @return New instance of a <code>DatatypeFactory</code>
286N/A *
286N/A * @throws DatatypeConfigurationException if <code>factoryClassName</code> is <code>null</code>, or
286N/A * the factory class cannot be loaded, instantiated.
286N/A *
286N/A * @see #newInstance()
286N/A *
286N/A * @since 1.6
286N/A */
286N/A public static DatatypeFactory newInstance(String factoryClassName, ClassLoader classLoader)
286N/A throws DatatypeConfigurationException {
286N/A try {
286N/A return (DatatypeFactory) FactoryFinder.newInstance(factoryClassName, classLoader, false);
286N/A } catch (FactoryFinder.ConfigurationError e) {
286N/A throw new DatatypeConfigurationException(e.getMessage(), e.getException());
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>Duration</code>
286N/A * specifying the <code>Duration</code> as its string representation, "PnYnMnDTnHnMnS",
286N/A * as defined in XML Schema 1.0 section 3.2.6.1.</p>
286N/A *
286N/A * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
286N/A * <blockquote>
286N/A * duration represents a duration of time.
286N/A * The value space of duration is a six-dimensional space where the coordinates designate the
286N/A * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
286N/A * These components are ordered in their significance by their order of appearance i.e. as
286N/A * year, month, day, hour, minute, and second.
286N/A * </blockquote>
286N/A * <p>All six values are set and availabe from the created {@link Duration}</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * @param lexicalRepresentation <code>String</code> representation of a <code>Duration</code>.
286N/A *
286N/A * @return New <code>Duration</code> created from parsing the <code>lexicalRepresentation</code>.
286N/A *
286N/A * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code>.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A * @throws NullPointerException if <code>lexicalRepresentation</code> is <code>null</code>.
286N/A */
286N/A public abstract Duration newDuration(final String lexicalRepresentation);
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>Duration</code>
286N/A * specifying the <code>Duration</code> as milliseconds.</p>
286N/A *
286N/A * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
286N/A * <blockquote>
286N/A * duration represents a duration of time.
286N/A * The value space of duration is a six-dimensional space where the coordinates designate the
286N/A * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
286N/A * These components are ordered in their significance by their order of appearance i.e. as
286N/A * year, month, day, hour, minute, and second.
286N/A * </blockquote>
286N/A * <p>All six values are set by computing their values from the specified milliseconds
286N/A * and are availabe using the <code>get</code> methods of the created {@link Duration}.
286N/A * The values conform to and are defined by:</p>
286N/A * <ul>
286N/A * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
286N/A * <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
286N/A * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
286N/A * </li>
286N/A * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
286N/A * </ul>
286N/A *
286N/A * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
286N/A * {@link java.util.Calendar#YEAR} = 1970,
286N/A * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
286N/A * {@link java.util.Calendar#DATE} = 1, etc.
286N/A * This is important as there are variations in the Gregorian Calendar,
286N/A * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
286N/A * so the result of {@link Duration#getMonths()} and {@link Duration#getDays()} can be influenced.</p>
286N/A *
286N/A * @param durationInMilliSeconds Duration in milliseconds to create.
286N/A *
286N/A * @return New <code>Duration</code> representing <code>durationInMilliSeconds</code>.
286N/A */
286N/A public abstract Duration newDuration(final long durationInMilliSeconds);
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>Duration</code>
286N/A * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * <p>A <code>null</code> value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param years of this <code>Duration</code>
286N/A * @param months of this <code>Duration</code>
286N/A * @param days of this <code>Duration</code>
286N/A * @param hours of this <code>Duration</code>
286N/A * @param minutes of this <code>Duration</code>
286N/A * @param seconds of this <code>Duration</code>
286N/A *
286N/A * @return New <code>Duration</code> created from the specified values.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if all the fields (years, months, ...) are null or
286N/A * if any of the fields is negative.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A */
286N/A public abstract Duration newDuration(
286N/A final boolean isPositive,
286N/A final BigInteger years,
286N/A final BigInteger months,
286N/A final BigInteger days,
286N/A final BigInteger hours,
286N/A final BigInteger minutes,
286N/A final BigDecimal seconds);
286N/A
286N/A /**
286N/A * <p>Obtain a new instance of a <code>Duration</code>
286N/A * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param years of this <code>Duration</code>
286N/A * @param months of this <code>Duration</code>
286N/A * @param days of this <code>Duration</code>
286N/A * @param hours of this <code>Duration</code>
286N/A * @param minutes of this <code>Duration</code>
286N/A * @param seconds of this <code>Duration</code>
286N/A *
286N/A * @return New <code>Duration</code> created from the specified values.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if any of the fields is negative.
286N/A *
286N/A * @see #newDuration(
286N/A * boolean isPositive,
286N/A * BigInteger years,
286N/A * BigInteger months,
286N/A * BigInteger days,
286N/A * BigInteger hours,
286N/A * BigInteger minutes,
286N/A * BigDecimal seconds)
286N/A */
286N/A public Duration newDuration(
286N/A final boolean isPositive,
286N/A final int years,
286N/A final int months,
286N/A final int days,
286N/A final int hours,
286N/A final int minutes,
286N/A final int seconds) {
286N/A
286N/A // years may not be set
286N/A BigInteger realYears = (years != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) years) : null;
286N/A
286N/A // months may not be set
286N/A BigInteger realMonths = (months != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) months) : null;
286N/A
286N/A // days may not be set
286N/A BigInteger realDays = (days != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) days) : null;
286N/A
286N/A // hours may not be set
286N/A BigInteger realHours = (hours != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) hours) : null;
286N/A
286N/A // minutes may not be set
286N/A BigInteger realMinutes = (minutes != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) minutes) : null;
286N/A
286N/A // seconds may not be set
286N/A BigDecimal realSeconds = (seconds != DatatypeConstants.FIELD_UNDEFINED) ? BigDecimal.valueOf((long) seconds) : null;
286N/A
286N/A return newDuration(
286N/A isPositive,
286N/A realYears,
286N/A realMonths,
286N/A realDays,
286N/A realHours,
286N/A realMinutes,
286N/A realSeconds
286N/A );
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> by parsing its <code>String</code> representation,
286N/A * "<em>PnDTnHnMnS</em>", <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only day, hour, minute, and second components.
286N/A * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
286N/A *
286N/A * <p>All four values are set and availabe from the created {@link Duration}</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * @param lexicalRepresentation Lexical representation of a duration.
286N/A *
286N/A * @return New <code>Duration</code> created using the specified <code>lexicalRepresentation</code>.
286N/A *
286N/A * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code> expressed only in terms of days and time.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
286N/A */
286N/A public Duration newDurationDayTime(final String lexicalRepresentation) {
286N/A // lexicalRepresentation must be non-null
286N/A if (lexicalRepresentation == null) {
286N/A throw new NullPointerException(
286N/A "Trying to create an xdt:dayTimeDuration with an invalid"
286N/A + " lexical representation of \"null\"");
286N/A }
286N/A
286N/A // test lexicalRepresentation against spec regex
286N/A Matcher matcher = XDTSCHEMA_DTD.matcher(lexicalRepresentation);
286N/A if (!matcher.matches()) {
286N/A throw new IllegalArgumentException(
286N/A "Trying to create an xdt:dayTimeDuration with an invalid"
286N/A + " lexical representation of \"" + lexicalRepresentation
286N/A + "\", data model requires years and months only.");
286N/A }
286N/A
286N/A return newDuration(lexicalRepresentation);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified milliseconds as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only day, hour, minute, and second components.
286N/A * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
286N/A *
286N/A * <p>All four values are set by computing their values from the specified milliseconds
286N/A * and are availabe using the <code>get</code> methods of the created {@link Duration}.
286N/A * The values conform to and are defined by:</p>
286N/A * <ul>
286N/A * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
286N/A * <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
286N/A * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
286N/A * </li>
286N/A * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
286N/A * </ul>
286N/A *
286N/A * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
286N/A * {@link java.util.Calendar#YEAR} = 1970,
286N/A * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
286N/A * {@link java.util.Calendar#DATE} = 1, etc.
286N/A * This is important as there are variations in the Gregorian Calendar,
286N/A * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
286N/A * so the result of {@link Duration#getDays()} can be influenced.</p>
286N/A *
286N/A * <p>Any remaining milliseconds after determining the day, hour, minute and second are discarded.</p>
286N/A *
286N/A * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
286N/A *
286N/A * @return New <code>Duration</code> created with the specified <code>durationInMilliseconds</code>.
286N/A *
286N/A * @see <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>
286N/A */
286N/A public Duration newDurationDayTime(final long durationInMilliseconds) {
286N/A
286N/A return newDuration(durationInMilliseconds);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified
286N/A * <code>day</code>, <code>hour</code>, <code>minute</code> and <code>second</code> as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only day, hour, minute, and second components.
286N/A * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * <p>A <code>null</code> value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param day Day of <code>Duration</code>.
286N/A * @param hour Hour of <code>Duration</code>.
286N/A * @param minute Minute of <code>Duration</code>.
286N/A * @param second Second of <code>Duration</code>.
286N/A *
286N/A * @return New <code>Duration</code> created with the specified <code>day</code>, <code>hour</code>, <code>minute</code>
286N/A * and <code>second</code>.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if all the fields (day, hour, ...) are null or
286N/A * if any of the fields is negative.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A */
286N/A public Duration newDurationDayTime(
286N/A final boolean isPositive,
286N/A final BigInteger day,
286N/A final BigInteger hour,
286N/A final BigInteger minute,
286N/A final BigInteger second) {
286N/A
286N/A return newDuration(
286N/A isPositive,
286N/A null, // years
286N/A null, // months
286N/A day,
286N/A hour,
286N/A minute,
286N/A (second != null)? new BigDecimal(second):null
286N/A );
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified
286N/A * <code>day</code>, <code>hour</code>, <code>minute</code> and <code>second</code> as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#dayTimeDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only day, hour, minute, and second components.
286N/A * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param day Day of <code>Duration</code>.
286N/A * @param hour Hour of <code>Duration</code>.
286N/A * @param minute Minute of <code>Duration</code>.
286N/A * @param second Second of <code>Duration</code>.
286N/A *
286N/A * @return New <code>Duration</code> created with the specified <code>day</code>, <code>hour</code>, <code>minute</code>
286N/A * and <code>second</code>.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if any of the fields (day, hour, ...) is negative.
286N/A */
286N/A public Duration newDurationDayTime(
286N/A final boolean isPositive,
286N/A final int day,
286N/A final int hour,
286N/A final int minute,
286N/A final int second) {
286N/A
286N/A return newDurationDayTime(
286N/A isPositive,
286N/A BigInteger.valueOf((long) day),
286N/A BigInteger.valueOf((long) hour),
286N/A BigInteger.valueOf((long) minute),
286N/A BigInteger.valueOf((long) second)
286N/A );
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> by parsing its <code>String</code> representation,
286N/A * "<em>PnYnM</em>", <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only year and month components.
286N/A * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
286N/A *
286N/A * <p>Both values are set and availabe from the created {@link Duration}</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * @param lexicalRepresentation Lexical representation of a duration.
286N/A *
286N/A * @return New <code>Duration</code> created using the specified <code>lexicalRepresentation</code>.
286N/A *
286N/A * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code> expressed only in terms of years and months.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
286N/A */
286N/A public Duration newDurationYearMonth(
286N/A final String lexicalRepresentation) {
286N/A
286N/A // lexicalRepresentation must be non-null
286N/A if (lexicalRepresentation == null) {
286N/A throw new NullPointerException(
286N/A "Trying to create an xdt:yearMonthDuration with an invalid"
286N/A + " lexical representation of \"null\"");
286N/A }
286N/A
286N/A // test lexicalRepresentation against spec regex
286N/A Matcher matcher = XDTSCHEMA_YMD.matcher(lexicalRepresentation);
286N/A if (!matcher.matches()) {
286N/A throw new IllegalArgumentException(
286N/A "Trying to create an xdt:yearMonthDuration with an invalid"
286N/A + " lexical representation of \"" + lexicalRepresentation
286N/A + "\", data model requires days and times only.");
286N/A }
286N/A
286N/A return newDuration(lexicalRepresentation);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified milliseconds as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
286N/A *
286N/A * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
286N/A * whose lexical representation contains only year and month components.
286N/A * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
286N/A *
286N/A * <p>Both values are set by computing their values from the specified milliseconds
286N/A * and are availabe using the <code>get</code> methods of the created {@link Duration}.
286N/A * The values conform to and are defined by:</p>
286N/A * <ul>
286N/A * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
286N/A * <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
286N/A * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
286N/A * </li>
286N/A * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
286N/A * </ul>
286N/A *
286N/A * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
286N/A * {@link java.util.Calendar#YEAR} = 1970,
286N/A * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
286N/A * {@link java.util.Calendar#DATE} = 1, etc.
286N/A * This is important as there are variations in the Gregorian Calendar,
286N/A * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
286N/A * so the result of {@link Duration#getMonths()} can be influenced.</p>
286N/A *
286N/A * <p>Any remaining milliseconds after determining the year and month are discarded.</p>
286N/A *
286N/A * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
286N/A *
286N/A * @return New <code>Duration</code> created using the specified <code>durationInMilliseconds</code>.
286N/A */
286N/A public Duration newDurationYearMonth(
286N/A final long durationInMilliseconds) {
286N/A
286N/A // create a Duration that only has sign, year & month
286N/A // Duration is immutable, so need to create a new Duration
286N/A // implementations may override this method in a more efficient way
286N/A Duration fullDuration = newDuration(durationInMilliseconds);
286N/A boolean isPositive = (fullDuration.getSign() == -1) ? false : true;
286N/A BigInteger years =
286N/A (BigInteger) fullDuration.getField(DatatypeConstants.YEARS);
286N/A if (years == null) { years = BigInteger.ZERO; }
286N/A BigInteger months =
286N/A (BigInteger) fullDuration.getField(DatatypeConstants.MONTHS);
286N/A if (months == null) { months = BigInteger.ZERO; }
286N/A
286N/A return newDurationYearMonth(isPositive, years, months);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified
286N/A * <code>year</code> and <code>month</code> as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
286N/A *
286N/A * <p>The XML Schema specification states that values can be of an arbitrary size.
286N/A * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
286N/A * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
286N/A * if implementation capacities are exceeded.</p>
286N/A *
286N/A * <p>A <code>null</code> value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param year Year of <code>Duration</code>.
286N/A * @param month Month of <code>Duration</code>.
286N/A *
286N/A * @return New <code>Duration</code> created using the specified <code>year</code> and <code>month</code>.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if all of the fields (year, month) are null or
286N/A * if any of the fields is negative.
286N/A * @throws UnsupportedOperationException If implementation cannot support requested values.
286N/A */
286N/A public Duration newDurationYearMonth(
286N/A final boolean isPositive,
286N/A final BigInteger year,
286N/A final BigInteger month) {
286N/A
286N/A return newDuration(
286N/A isPositive,
286N/A year,
286N/A month,
286N/A null, // days
286N/A null, // hours
286N/A null, // minutes
286N/A null // seconds
286N/A );
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified
286N/A * <code>year</code> and <code>month</code> as defined in
286N/A * <a href="http://www.w3.org/TR/xpath-datamodel#yearMonthDuration">
286N/A * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param isPositive Set to <code>false</code> to create a negative duration. When the length
286N/A * of the duration is zero, this parameter will be ignored.
286N/A * @param year Year of <code>Duration</code>.
286N/A * @param month Month of <code>Duration</code>.
286N/A *
286N/A * @return New <code>Duration</code> created using the specified <code>year</code> and <code>month</code>.
286N/A *
286N/A * @throws IllegalArgumentException If the values are not a valid representation of a
286N/A * <code>Duration</code>: if any of the fields (year, month) is negative.
286N/A */
286N/A public Duration newDurationYearMonth(
286N/A final boolean isPositive,
286N/A final int year,
286N/A final int month) {
286N/A
286N/A return newDurationYearMonth(
286N/A isPositive,
286N/A BigInteger.valueOf((long) year),
286N/A BigInteger.valueOf((long) month));
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a new instance of an <code>XMLGregorianCalendar</code>.</p>
286N/A *
286N/A * <p>All date/time datatype fields set to {@link DatatypeConstants#FIELD_UNDEFINED} or null.</p>
286N/A *
286N/A * @return New <code>XMLGregorianCalendar</code> with all date/time datatype fields set to
286N/A * {@link DatatypeConstants#FIELD_UNDEFINED} or null.
286N/A */
286N/A public abstract XMLGregorianCalendar newXMLGregorianCalendar();
286N/A
286N/A /**
286N/A * <p>Create a new XMLGregorianCalendar by parsing the String as a lexical representation.</p>
286N/A *
286N/A * <p>Parsing the lexical string representation is defined in
286N/A * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
286N/A * <em>Lexical Representation</em>.</a></p>
286N/A *
286N/A * <p>The string representation may not have any leading and trailing whitespaces.</p>
286N/A *
286N/A * <p>The parsing is done field by field so that
286N/A * the following holds for any lexically correct String x:</p>
286N/A * <pre>
286N/A * newXMLGregorianCalendar(x).toXMLFormat().equals(x)
286N/A * </pre>
286N/A * <p>Except for the noted lexical/canonical representation mismatches
286N/A * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
286N/A * XML Schema 1.0 errata, Section 3.2.7.2</a>.</p>
286N/A *
286N/A * @param lexicalRepresentation Lexical representation of one the eight XML Schema date/time datatypes.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from the <code>lexicalRepresentation</code>.
286N/A *
286N/A * @throws IllegalArgumentException If the <code>lexicalRepresentation</code> is not a valid <code>XMLGregorianCalendar</code>.
286N/A * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
286N/A */
286N/A public abstract XMLGregorianCalendar newXMLGregorianCalendar(final String lexicalRepresentation);
286N/A
286N/A /**
286N/A * <p>Create an <code>XMLGregorianCalendar</code> from a {@link GregorianCalendar}.</p>
286N/A *
286N/A * <table border="2" rules="all" cellpadding="2">
286N/A * <thead>
286N/A * <tr>
286N/A * <th align="center" colspan="2">
286N/A * Field by Field Conversion from
286N/A * {@link GregorianCalendar} to an {@link XMLGregorianCalendar}
286N/A * </th>
286N/A * </tr>
286N/A * <tr>
286N/A * <th><code>java.util.GregorianCalendar</code> field</th>
286N/A * <th><code>javax.xml.datatype.XMLGregorianCalendar</code> field</th>
286N/A * </tr>
286N/A * </thead>
286N/A * <tbody>
286N/A * <tr>
286N/A * <td><code>ERA == GregorianCalendar.BC ? -YEAR : YEAR</code></td>
286N/A * <td>{@link XMLGregorianCalendar#setYear(int year)}</td>
286N/A * </tr>
286N/A * <tr>
286N/A * <td><code>MONTH + 1</code></td>
286N/A * <td>{@link XMLGregorianCalendar#setMonth(int month)}</td>
286N/A * </tr>
286N/A * <tr>
286N/A * <td><code>DAY_OF_MONTH</code></td>
286N/A * <td>{@link XMLGregorianCalendar#setDay(int day)}</td>
286N/A * </tr>
286N/A * <tr>
286N/A * <td><code>HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND</code></td>
286N/A * <td>{@link XMLGregorianCalendar#setTime(int hour, int minute, int second, BigDecimal fractional)}</td>
286N/A * </tr>
286N/A * <tr>
286N/A * <td>
286N/A * <code>(ZONE_OFFSET + DST_OFFSET) / (60*1000)</code><br/>
286N/A * <em>(in minutes)</em>
286N/A * </td>
286N/A * <td>{@link XMLGregorianCalendar#setTimezone(int offset)}<sup><em>*</em></sup>
286N/A * </td>
286N/A * </tr>
286N/A * </tbody>
286N/A * </table>
286N/A * <p><em>*</em>conversion loss of information. It is not possible to represent
286N/A * a <code>java.util.GregorianCalendar</code> daylight savings timezone id in the
286N/A * XML Schema 1.0 date/time datatype representation.</p>
286N/A *
286N/A * <p>To compute the return value's <code>TimeZone</code> field,
286N/A * <ul>
286N/A * <li>when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
286N/A * create a <code>java.util.TimeZone</code> with a custom timezone id
286N/A * using the <code>this.getTimezone()</code>.</li>
286N/A * <li>else use the <code>GregorianCalendar</code> default timezone value
286N/A * for the host is defined as specified by
286N/A * <code>java.util.TimeZone.getDefault()</code>.</li></p>
286N/A *
286N/A * @param cal <code>java.util.GregorianCalendar</code> used to create <code>XMLGregorianCalendar</code>
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from <code>java.util.GregorianCalendar</code>
286N/A *
286N/A * @throws NullPointerException If <code>cal</code> is <code>null</code>.
286N/A */
286N/A public abstract XMLGregorianCalendar newXMLGregorianCalendar(final GregorianCalendar cal);
286N/A
286N/A /**
286N/A * <p>Constructor allowing for complete value spaces allowed by
286N/A * W3C XML Schema 1.0 recommendation for xsd:dateTime and related
286N/A * builtin datatypes. Note that <code>year</code> parameter supports
286N/A * arbitrarily large numbers and fractionalSecond has infinite
286N/A * precision.</p>
286N/A *
286N/A * <p>A <code>null</code> value indicates that field is not set.</p>
286N/A *
286N/A * @param year of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param month of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param day of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param hour of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param minute of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param second of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param fractionalSecond of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param timezone of <code>XMLGregorianCalendar</code> to be created.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from specified values.
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A */
286N/A public abstract XMLGregorianCalendar newXMLGregorianCalendar(
286N/A final BigInteger year,
286N/A final int month,
286N/A final int day,
286N/A final int hour,
286N/A final int minute,
286N/A final int second,
286N/A final BigDecimal fractionalSecond,
286N/A final int timezone);
286N/A
286N/A /**
286N/A * <p>Constructor of value spaces that a
286N/A * <code>java.util.GregorianCalendar</code> instance would need to convert to an
286N/A * <code>XMLGregorianCalendar</code> instance.</p>
286N/A *
286N/A * <p><code>XMLGregorianCalendar eon</code> and
286N/A * <code>fractionalSecond</code> are set to <code>null</code></p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param year of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param month of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param day of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param hour of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param minute of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param second of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param millisecond of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param timezone of <code>XMLGregorianCalendar</code> to be created.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from specified values.
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A */
286N/A public XMLGregorianCalendar newXMLGregorianCalendar(
286N/A final int year,
286N/A final int month,
286N/A final int day,
286N/A final int hour,
286N/A final int minute,
286N/A final int second,
286N/A final int millisecond,
286N/A final int timezone) {
286N/A
286N/A // year may be undefined
286N/A BigInteger realYear = (year != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) year) : null;
286N/A
286N/A // millisecond may be undefined
286N/A // millisecond must be >= 0 millisecond <= 1000
286N/A BigDecimal realMillisecond = null; // undefined value
286N/A if (millisecond != DatatypeConstants.FIELD_UNDEFINED) {
286N/A if (millisecond < 0 || millisecond > 1000) {
286N/A throw new IllegalArgumentException(
286N/A "javax.xml.datatype.DatatypeFactory#newXMLGregorianCalendar("
286N/A + "int year, int month, int day, int hour, int minute, int second, int millisecond, int timezone)"
286N/A + "with invalid millisecond: " + millisecond
286N/A );
286N/A }
286N/A
286N/A realMillisecond = BigDecimal.valueOf((long) millisecond).movePointLeft(3);
286N/A }
286N/A
286N/A return newXMLGregorianCalendar(
286N/A realYear,
286N/A month,
286N/A day,
286N/A hour,
286N/A minute,
286N/A second,
286N/A realMillisecond,
286N/A timezone
286N/A );
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a Java representation of XML Schema builtin datatype <code>date</code> or <code>g*</code>.</p>
286N/A *
286N/A * <p>For example, an instance of <code>gYear</code> can be created invoking this factory
286N/A * with <code>month</code> and <code>day</code> parameters set to
286N/A * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param year of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param month of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param day of <code>XMLGregorianCalendar</code> to be created.
286N/A * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from parameter values.
286N/A *
286N/A * @see DatatypeConstants#FIELD_UNDEFINED
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A */
286N/A public XMLGregorianCalendar newXMLGregorianCalendarDate(
286N/A final int year,
286N/A final int month,
286N/A final int day,
286N/A final int timezone) {
286N/A
286N/A return newXMLGregorianCalendar(
286N/A year,
286N/A month,
286N/A day,
286N/A DatatypeConstants.FIELD_UNDEFINED, // hour
286N/A DatatypeConstants.FIELD_UNDEFINED, // minute
286N/A DatatypeConstants.FIELD_UNDEFINED, // second
286N/A DatatypeConstants.FIELD_UNDEFINED, // millisecond
286N/A timezone);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a Java instance of XML Schema builtin datatype <code>time</code>.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param hours number of hours
286N/A * @param minutes number of minutes
286N/A * @param seconds number of seconds
286N/A * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from parameter values.
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A *
286N/A * @see DatatypeConstants#FIELD_UNDEFINED
286N/A */
286N/A public XMLGregorianCalendar newXMLGregorianCalendarTime(
286N/A final int hours,
286N/A final int minutes,
286N/A final int seconds,
286N/A final int timezone) {
286N/A
286N/A return newXMLGregorianCalendar(
286N/A DatatypeConstants.FIELD_UNDEFINED, // Year
286N/A DatatypeConstants.FIELD_UNDEFINED, // Month
286N/A DatatypeConstants.FIELD_UNDEFINED, // Day
286N/A hours,
286N/A minutes,
286N/A seconds,
286N/A DatatypeConstants.FIELD_UNDEFINED, //Millisecond
286N/A timezone);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a Java instance of XML Schema builtin datatype time.</p>
286N/A *
286N/A * <p>A <code>null</code> value indicates that field is not set.</p>
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param hours number of hours
286N/A * @param minutes number of minutes
286N/A * @param seconds number of seconds
286N/A * @param fractionalSecond value of <code>null</code> indicates that this optional field is not set.
286N/A * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from parameter values.
286N/A *
286N/A * @see DatatypeConstants#FIELD_UNDEFINED
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A */
286N/A public XMLGregorianCalendar newXMLGregorianCalendarTime(
286N/A final int hours,
286N/A final int minutes,
286N/A final int seconds,
286N/A final BigDecimal fractionalSecond,
286N/A final int timezone) {
286N/A
286N/A return newXMLGregorianCalendar(
286N/A null, // year
286N/A DatatypeConstants.FIELD_UNDEFINED, // month
286N/A DatatypeConstants.FIELD_UNDEFINED, // day
286N/A hours,
286N/A minutes,
286N/A seconds,
286N/A fractionalSecond,
286N/A timezone);
286N/A }
286N/A
286N/A /**
286N/A * <p>Create a Java instance of XML Schema builtin datatype time.</p>
286N/A *
286N/A * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field is not set.</p>
286N/A *
286N/A * @param hours number of hours
286N/A * @param minutes number of minutes
286N/A * @param seconds number of seconds
286N/A * @param milliseconds number of milliseconds
286N/A * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
286N/A *
286N/A * @return <code>XMLGregorianCalendar</code> created from parameter values.
286N/A *
286N/A * @see DatatypeConstants#FIELD_UNDEFINED
286N/A *
286N/A * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
286N/A * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
286N/A * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
286N/A * as determined by {@link XMLGregorianCalendar#isValid()}.
286N/A */
286N/A public XMLGregorianCalendar newXMLGregorianCalendarTime(
286N/A final int hours,
286N/A final int minutes,
286N/A final int seconds,
286N/A final int milliseconds,
286N/A final int timezone) {
286N/A
286N/A // millisecond may be undefined
286N/A // millisecond must be >= 0 millisecond <= 1000
286N/A BigDecimal realMilliseconds = null; // undefined value
286N/A if (milliseconds != DatatypeConstants.FIELD_UNDEFINED) {
286N/A if (milliseconds < 0 || milliseconds > 1000) {
286N/A throw new IllegalArgumentException(
286N/A "javax.xml.datatype.DatatypeFactory#newXMLGregorianCalendarTime("
286N/A + "int hours, int minutes, int seconds, int milliseconds, int timezone)"
286N/A + "with invalid milliseconds: " + milliseconds
286N/A );
286N/A }
286N/A
286N/A realMilliseconds = BigDecimal.valueOf((long) milliseconds).movePointLeft(3);
286N/A }
286N/A
286N/A return newXMLGregorianCalendarTime(
286N/A hours,
286N/A minutes,
286N/A seconds,
286N/A realMilliseconds,
286N/A timezone
286N/A );
286N/A }
286N/A}