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