KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > datatype > DatatypeFactory


1 // $Id: DatatypeFactory.java,v 1.1.6.2.4.3 2004/06/18 08:40:14 rameshm Exp $
2

3 /*
4  * @(#)DatatypeFactory.java 1.4 04/07/26
5  *
6  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */

9
10 package javax.xml.datatype;
11
12 import java.math.BigInteger JavaDoc;
13 import java.math.BigDecimal JavaDoc;
14 import java.util.GregorianCalendar JavaDoc;
15
16 /**
17  * <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>
18  *
19  * <p id="DatatypeFactory.newInstance">{@link #newInstance()} is used to create a new <code>DatatypeFactory</code>.
20  * The following implementation resolution mechanisms are used in the following order:</p>
21  * <ol>
22  * <li>
23  * If the system property specified by {@link #DATATYPEFACTORY_PROPERTY}, "<code>javax.xml.datatype.DatatypeFactory</code>",
24  * exists, a class with the name of the property's value is instantiated.
25  * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
26  * </li>
27  * <li>
28  * If the file ${JAVA_HOME}/lib/jaxp.properties exists, it is loaded in a {@link java.util.Properties} <code>Object</code>.
29  * The <code>Properties</code> <code>Object </code> is then queried for the property as documented in the prior step
30  * and processed as documented in the prior step.
31  * </li>
32  * <li>
33  * The services resolution mechanism is used, e.g. <code>META-INF/services/java.xml.datatype.DatatypeFactory</code>.
34  * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
35  * </li>
36  * <li>
37  * The final mechanism is to attempt to instantiate the <code>Class</code> specified by
38  * {@link #DATATYPEFACTORY_IMPLEMENTATION_CLASS}, "<code>javax.xml.datatype.DatatypeFactoryImpl</code>".
39  * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
40  * </li>
41  * </ol>
42  *
43  * @author <a HREF="mailto:Joseph.Fialli@Sun.COM">Joseph Fialli</a>
44  * @author <a HREF="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
45  * @version $Revision: 1.1.6.2.4.3 $, $Date: 2004/06/18 08:40:14 $
46  * @since 1.5
47  */

48 public abstract class DatatypeFactory {
49         
50     /**
51      * <p>Default property name as defined in JSR 206: Java(TM) API for XML Processing (JAXP) 1.3.</p>
52      *
53      * <p>Default value is <code>javax.xml.datatype.DatatypeFactory</code>.</p>
54      */

55     public static final String JavaDoc DATATYPEFACTORY_PROPERTY = "javax.xml.datatype.DatatypeFactory";
56         
57     /**
58      * <p>Default implementation class name as defined in JSR 206: Java(TM) API for XML Processing (JAXP) 1.3.</p>
59      *
60      * <p>Default value is <code>com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl</code>.</p>
61      */

62     public static final String JavaDoc DATATYPEFACTORY_IMPLEMENTATION_CLASS = "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl";
63     
64     /**
65      * <p>Protected constructor to prevent instaniation outside of package.</p>
66      *
67      * <p>Use {@link #newInstance()} to create a <code>DatatypeFactory</code>.</p>
68      */

69     protected DatatypeFactory() {
70     }
71     
72     /**
73      * <p>Obtain a new instance of a <code>DatatypeFactory</code>.</p>
74      *
75      * <p>The implementation resolution mechanisms are <a HREF="#DatatypeFactory.newInstance">defined</a> in this
76      * <code>Class</code>'s documentation.</p>
77      *
78      * @return New instance of a <code>DocumentBuilderFactory</code>
79      *
80      * @throws DatatypeConfigurationException If the implementation is not
81      * available or cannot be instantiated.
82      */

83     public static DatatypeFactory JavaDoc newInstance()
84         throws DatatypeConfigurationException JavaDoc {
85             
86         try {
87             return (DatatypeFactory JavaDoc) FactoryFinder.find(
88                 /* The default property name according to the JAXP spec */
89                  DATATYPEFACTORY_PROPERTY,
90                 /* The fallback implementation class name */
91                 DATATYPEFACTORY_IMPLEMENTATION_CLASS);
92         } catch (FactoryFinder.ConfigurationError JavaDoc e) {
93             throw new DatatypeConfigurationException JavaDoc(e.getMessage(), e.getException());
94         }
95     }
96     
97     /**
98      * <p>Obtain a new instance of a <code>Duration</code>
99      * specifying the <code>Duration</code> as its string representation, "PnYnMnDTnHnMnS",
100      * as defined in XML Schema 1.0 section 3.2.6.1.</p>
101      *
102      * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
103      * <blockquote>
104      * duration represents a duration of time.
105      * The value space of duration is a six-dimensional space where the coordinates designate the
106      * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
107      * These components are ordered in their significance by their order of appearance i.e. as
108      * year, month, day, hour, minute, and second.
109      * </blockquote>
110      * <p>All six values are set and availabe from the created {@link Duration}</p>
111      *
112      * <p>The XML Schema specification states that values can be of an arbitrary size.
113      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
114      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
115      * if implementation capacities are exceeded.</p>
116      *
117      * @param lexicalRepresentation <code>String</code> representation of a <code>Duration</code>.
118      *
119      * @return New <code>Duration</code> created from parsing the <code>lexicalRepresentation</code>.
120      *
121      * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code>.
122      * @throws UnsupportedOperationException If implementation cannot support requested values.
123      * @throws NullPointerException if <code>lexicalRepresentation</code> is <code>null</code>.
124      */

125     public abstract Duration JavaDoc newDuration(final String JavaDoc lexicalRepresentation);
126     
127     /**
128      * <p>Obtain a new instance of a <code>Duration</code>
129      * specifying the <code>Duration</code> as milliseconds.</p>
130      *
131      * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
132      * <blockquote>
133      * duration represents a duration of time.
134      * The value space of duration is a six-dimensional space where the coordinates designate the
135      * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
136      * These components are ordered in their significance by their order of appearance i.e. as
137      * year, month, day, hour, minute, and second.
138      * </blockquote>
139      * <p>All six values are set by computing their values from the specified milliseconds
140      * and are availabe using the <code>get</code> methods of the created {@link Duration}.
141      * The values conform to and are defined by:</p>
142      * <ul>
143      * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
144      * <li><a HREF="http://www.w3.org/TR/xmlschema-2/#isoformats">
145      * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
146      * </li>
147      * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
148      * </ul>
149      *
150      * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
151      * {@link java.util.Calendar#YEAR} = 1970,
152      * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
153      * {@link java.util.Calendar#DATE} = 1, etc.
154      * This is important as there are variations in the Gregorian Calendar,
155      * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
156      * so the result of {@link Duration#getMonths()} and {@link Duration#getDays()} can be influenced.</p>
157      *
158      * @param durationInMilliSeconds Duration in milliseconds to create.
159      *
160      * @return New <code>Duration</code> representing <code>durationInMilliSeconds</code>.
161      */

162     public abstract Duration JavaDoc newDuration(final long durationInMilliSeconds);
163     
164     /**
165      * <p>Obtain a new instance of a <code>Duration</code>
166      * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
167      *
168      * <p>The XML Schema specification states that values can be of an arbitrary size.
169      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
170      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
171      * if implementation capacities are exceeded.</p>
172      *
173      * <p>A <code>null</code> value indicates that field isnot set.</p>
174      *
175      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
176      * of the duration is zero, this parameter will be ignored.
177      * @param years of this <code>Duration</code>
178      * @param months of this <code>Duration</code>
179      * @param days of this <code>Duration</code>
180      * @param hours of this <code>Duration</code>
181      * @param minutes of this <code>Duration</code>
182      * @param seconds of this <code>Duration</code>
183      *
184      * @return New <code>Duration</code> created from the specified values.
185      *
186      * @throws IllegalArgumentException If values are not a valid representation of a <code>Duration</code>.
187      * @throws UnsupportedOperationException If implementation cannot support requested values.
188      */

189     public abstract Duration JavaDoc newDuration(
190         final boolean isPositive,
191         final BigInteger JavaDoc years,
192         final BigInteger JavaDoc months,
193         final BigInteger JavaDoc days,
194         final BigInteger JavaDoc hours,
195         final BigInteger JavaDoc minutes,
196         final BigDecimal JavaDoc seconds);
197
198     /**
199      * <p>Obtain a new instance of a <code>Duration</code>
200      * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
201      *
202      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
203      *
204      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
205      * of the duration is zero, this parameter will be ignored.
206      * @param years of this <code>Duration</code>
207      * @param months of this <code>Duration</code>
208      * @param days of this <code>Duration</code>
209      * @param hours of this <code>Duration</code>
210      * @param minutes of this <code>Duration</code>
211      * @param seconds of this <code>Duration</code>
212      *
213      * @return New <code>Duration</code> created from the specified values.
214      *
215      * @throws IllegalArgumentException If values are not a valid representation of a <code>Duration</code>.
216      *
217      * @see #newDuration(
218      * boolean isPositive,
219      * BigInteger years,
220      * BigInteger months,
221      * BigInteger days,
222      * BigInteger hours,
223      * BigInteger minutes,
224      * BigDecimal seconds)
225      */

226     public Duration JavaDoc newDuration(
227         final boolean isPositive,
228         final int years,
229         final int months,
230         final int days,
231         final int hours,
232         final int minutes,
233         final int seconds) {
234             
235         // years may not be set
236
BigInteger JavaDoc realYears = (years != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) years) : null;
237             
238         // months may not be set
239
BigInteger JavaDoc realMonths = (months != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) months) : null;
240
241         // days may not be set
242
BigInteger JavaDoc realDays = (days != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) days) : null;
243
244         // hours may not be set
245
BigInteger JavaDoc realHours = (hours != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) hours) : null;
246
247         // minutes may not be set
248
BigInteger JavaDoc realMinutes = (minutes != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) minutes) : null;
249         
250         // seconds may not be set
251
BigDecimal JavaDoc realSeconds = (seconds != DatatypeConstants.FIELD_UNDEFINED) ? BigDecimal.valueOf((long) seconds) : null;
252
253             return newDuration(
254                 isPositive,
255                 realYears,
256                 realMonths,
257                 realDays,
258                 realHours,
259                 realMinutes,
260                 realSeconds
261             );
262         }
263         
264     /**
265      * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> by parsing its <code>String</code> representation,
266      * "<em>PnDTnHnMnS</em>", <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-dayTimeDuration">
267      * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
268      *
269      * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
270      * whose lexical representation contains only day, hour, minute, and second components.
271      * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
272      *
273      * <p>All four values are set and availabe from the created {@link Duration}</p>
274      *
275      * <p>The XML Schema specification states that values can be of an arbitrary size.
276      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
277      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
278      * if implementation capacities are exceeded.</p>
279      *
280      * @param lexicalRepresentation Lexical representation of a duration.
281      *
282      * @return New <code>Duration</code> created using the specified <code>lexicalRepresentation</code>.
283      *
284      * @throws IllegalArgumentException If the given string does not conform to the aforementioned specification.
285      * @throws UnsupportedOperationException If implementation cannot support requested values.
286      * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
287      */

288     public Duration JavaDoc newDurationDayTime(final String JavaDoc lexicalRepresentation) {
289         
290         return newDuration(lexicalRepresentation);
291     }
292
293     /**
294      * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified milliseconds as defined in
295      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-dayTimeDuration">
296      * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
297      *
298      * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
299      * whose lexical representation contains only day, hour, minute, and second components.
300      * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
301      *
302      * <p>All four values are set by computing their values from the specified milliseconds
303      * and are availabe using the <code>get</code> methods of the created {@link Duration}.
304      * The values conform to and are defined by:</p>
305      * <ul>
306      * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
307      * <li><a HREF="http://www.w3.org/TR/xmlschema-2/#isoformats">
308      * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
309      * </li>
310      * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
311      * </ul>
312      *
313      * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
314      * {@link java.util.Calendar#YEAR} = 1970,
315      * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
316      * {@link java.util.Calendar#DATE} = 1, etc.
317      * This is important as there are variations in the Gregorian Calendar,
318      * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
319      * so the result of {@link Duration#getDays()} can be influenced.</p>
320      *
321      * <p>Any remaining milliseconds after determining the day, hour, minute and second are discarded.</p>
322      *
323      * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
324      *
325      * @return New <code>Duration</code> created with the specified <code>durationInMilliseconds</code>.
326      *
327      * @see <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-dayTimeDuration">
328      * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>
329      */

330     public Duration JavaDoc newDurationDayTime(final long durationInMilliseconds) {
331         
332         return newDuration(durationInMilliseconds);
333     }
334         
335     /**
336      * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified
337      * <code>day</code>, <code>hour</code>, <code>minute</code> and <code>second</code> as defined in
338      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-dayTimeDuration">
339      * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
340      *
341      * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
342      * whose lexical representation contains only day, hour, minute, and second components.
343      * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
344      *
345      * <p>The XML Schema specification states that values can be of an arbitrary size.
346      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
347      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
348      * if implementation capacities are exceeded.</p>
349      *
350      * <p>A <code>null</code> value indicates that field isnot set.</p>
351      *
352      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
353      * of the duration is zero, this parameter will be ignored.
354      * @param day Day of <code>Duration</code>.
355      * @param hour Hour of <code>Duration</code>.
356      * @param minute Minute of <code>Duration</code>.
357      * @param second Second of <code>Duration</code>.
358      *
359      * @return New <code>Duration</code> created with the specified <code>day</code>, <code>hour</code>, <code>minute</code>
360      * and <code>second</code>.
361      *
362      * @throws IllegalArgumentException If any values would create an invalid <code>Duration</code>.
363      * @throws UnsupportedOperationException If implementation cannot support requested values.
364      */

365     public Duration JavaDoc newDurationDayTime(
366         final boolean isPositive,
367         final BigInteger JavaDoc day,
368         final BigInteger JavaDoc hour,
369         final BigInteger JavaDoc minute,
370         final BigInteger JavaDoc second) {
371             
372         return newDuration(
373             isPositive,
374             null, // years
375
null, // months
376
day,
377             hour,
378             minute,
379             (second != null)? new BigDecimal JavaDoc(second):null
380         );
381     }
382         
383     /**
384      * <p>Create a <code>Duration</code> of type <code>xdt:dayTimeDuration</code> using the specified
385      * <code>day</code>, <code>hour</code>, <code>minute</code> and <code>second</code> as defined in
386      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-dayTimeDuration">
387      * XQuery 1.0 and XPath 2.0 Data Model, xdt:dayTimeDuration</a>.</p>
388      *
389      * <p>The datatype <code>xdt:dayTimeDuration</code> is a subtype of <code>xs:duration</code>
390      * whose lexical representation contains only day, hour, minute, and second components.
391      * This datatype resides in the namespace <code>http://www.w3.org/2003/11/xpath-datatypes</code>.</p>
392      *
393      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
394      *
395      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
396      * of the duration is zero, this parameter will be ignored.
397      * @param day Day of <code>Duration</code>.
398      * @param hour Hour of <code>Duration</code>.
399      * @param minute Minute of <code>Duration</code>.
400      * @param second Second of <code>Duration</code>.
401      *
402      * @return New <code>Duration</code> created with the specified <code>day</code>, <code>hour</code>, <code>minute</code>
403      * and <code>second</code>.
404      *
405      * @throws IllegalArgumentException If any values would create an invalid <code>Duration</code>.
406      */

407     public Duration JavaDoc newDurationDayTime(
408         final boolean isPositive,
409         final int day,
410         final int hour,
411         final int minute,
412         final int second) {
413             
414             return newDurationDayTime(
415                 isPositive,
416                 BigInteger.valueOf((long) day),
417                 BigInteger.valueOf((long) hour),
418                 BigInteger.valueOf((long) minute),
419                 BigInteger.valueOf((long) second)
420                 );
421         }
422
423     /**
424      * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> by parsing its <code>String</code> representation,
425      * "<em>PnYnM</em>", <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-yearMonthDuration">
426      * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
427      *
428      * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
429      * whose lexical representation contains only year and month components.
430      * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
431      *
432      * <p>Both values are set and availabe from the created {@link Duration}</p>
433      *
434      * <p>The XML Schema specification states that values can be of an arbitrary size.
435      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
436      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
437      * if implementation capacities are exceeded.</p>
438      *
439      * @param lexicalRepresentation Lexical representation of a duration.
440      *
441      * @return New <code>Duration</code> created using the specified <code>lexicalRepresentation</code>.
442      *
443      * @throws IllegalArgumentException If the <code>lexicalRepresentation</code> does not conform to the specification.
444      * @throws UnsupportedOperationException If implementation cannot support requested values.
445      * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
446      */

447     public Duration JavaDoc newDurationYearMonth(final String JavaDoc lexicalRepresentation) {
448         
449         return newDuration(lexicalRepresentation);
450     }
451
452     /**
453      * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified milliseconds as defined in
454      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-yearMonthDuration">
455      * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
456      *
457      * <p>The datatype <code>xdt:yearMonthDuration</code> is a subtype of <code>xs:duration</code>
458      * whose lexical representation contains only year and month components.
459      * This datatype resides in the namespace {@link javax.xml.XMLConstants#W3C_XPATH_DATATYPE_NS_URI}.</p>
460      *
461      * <p>Both values are set by computing their values from the specified milliseconds
462      * and are availabe using the <code>get</code> methods of the created {@link Duration}.
463      * The values conform to and are defined by:</p>
464      * <ul>
465      * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
466      * <li><a HREF="http://www.w3.org/TR/xmlschema-2/#isoformats">
467      * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
468      * </li>
469      * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
470      * </ul>
471      *
472      * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
473      * {@link java.util.Calendar#YEAR} = 1970,
474      * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
475      * {@link java.util.Calendar#DATE} = 1, etc.
476      * This is important as there are variations in the Gregorian Calendar,
477      * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
478      * so the result of {@link Duration#getMonths()} can be influenced.</p>
479      *
480      * <p>Any remaining milliseconds after determining the year and month are discarded.</p>
481      *
482      * @param durationInMilliseconds Milliseconds of <code>Duration</code> to create.
483      *
484      * @return New <code>Duration</code> created using the specified <code>durationInMilliseconds</code>.
485      */

486     public Duration JavaDoc newDurationYearMonth(final long durationInMilliseconds) {
487         
488         return newDuration(durationInMilliseconds);
489     }
490                 
491     /**
492      * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified
493      * <code>year</code> and <code>month</code> as defined in
494      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-yearMonthyDuration">
495      * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
496      *
497      * <p>The XML Schema specification states that values can be of an arbitrary size.
498      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
499      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
500      * if implementation capacities are exceeded.</p>
501      *
502      * <p>A <code>null</code> value indicates that field isnot set.</p>
503      *
504      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
505      * of the duration is zero, this parameter will be ignored.
506      * @param year Year of <code>Duration</code>.
507      * @param month Month of <code>Duration</code>.
508      *
509      * @return New <code>Duration</code> created using the specified <code>year</code> and <code>month</code>.
510      *
511      * @throws IllegalArgumentException If any values would create an invalid <code>Duration</code>.
512      * @throws UnsupportedOperationException If implementation cannot support requested values.
513      */

514     public Duration JavaDoc newDurationYearMonth(
515         final boolean isPositive,
516         final BigInteger JavaDoc year,
517         final BigInteger JavaDoc month) {
518             
519         return newDuration(
520             isPositive,
521             year,
522             month,
523             null, // days
524
null, // hours
525
null, // minutes
526
null // seconds
527
);
528     }
529
530     /**
531      * <p>Create a <code>Duration</code> of type <code>xdt:yearMonthDuration</code> using the specified
532      * <code>year</code> and <code>month</code> as defined in
533      * <a HREF="http://www.w3.org/TR/xpath-datamodel#dt-yearMonthyDuration">
534      * XQuery 1.0 and XPath 2.0 Data Model, xdt:yearMonthDuration</a>.</p>
535      *
536      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
537      *
538      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
539      * of the duration is zero, this parameter will be ignored.
540      * @param year Year of <code>Duration</code>.
541      * @param month Month of <code>Duration</code>.
542      *
543      * @return New <code>Duration</code> created using the specified <code>year</code> and <code>month</code>.
544      *
545      * @throws IllegalArgumentException If any values would create an invalid <code>Duration</code>.
546      */

547     public Duration JavaDoc newDurationYearMonth(
548         final boolean isPositive,
549         final int year,
550         final int month) {
551             
552         return newDurationYearMonth(
553             isPositive,
554             BigInteger.valueOf((long) year),
555             BigInteger.valueOf((long) month));
556         }
557
558     /**
559      * <p>Create a new instance of an <code>XMLGregorianCalendar</code>.</p>
560      *
561      * <p>All date/time datatype fields set to {@link DatatypeConstants#FIELD_UNDEFINED} or null.</p>
562      *
563      * @return New <code>XMLGregorianCalendar</code> with all date/time datatype fields set to
564      * {@link DatatypeConstants#FIELD_UNDEFINED} or null.
565      */

566     public abstract XMLGregorianCalendar JavaDoc newXMLGregorianCalendar();
567     
568     /**
569      * <p>Create a new XMLGregorianCalendar by parsing the String as a lexical representation.</p>
570      *
571      * <p>Parsing the lexical string representation is defined in
572      * <a HREF="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
573      * <em>Lexical Representation</em>.</a></p>
574      *
575      * <p>The string representation may not have any leading and trailing whitespaces.</p>
576      *
577      * <p>The parsing is done field by field so that
578      * the following holds for any lexically correct String x:</p>
579      * <pre>
580      * newXMLGregorianCalendar(x).toXMLFormat().equals(x)
581      * </pre>
582      * <p>Except for the noted lexical/canonical representation mismatches
583      * listed in <a HREF="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
584      * XML Schema 1.0 errata, Section 3.2.7.2</a>.</p>
585      *
586      * @param lexicalRepresentation Lexical representation of one the eight XML Schema date/time datatypes.
587      *
588      * @return <code>XMLGregorianCalendar</code> created from the <code>lexicalRepresentation</code>.
589      *
590      * @throws IllegalArgumentException If the <code>lexicalRepresentation</code> is not a valid <code>XMLGregorianCalendar</code>.
591      * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
592      */

593     public abstract XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(final String JavaDoc lexicalRepresentation);
594     
595     /**
596      * <p>Create an <code>XMLGregorianCalendar</code> from a {@link GregorianCalendar}.</p>
597      *
598      * <table border="2" rules="all" cellpadding="2">
599      * <thead>
600      * <tr>
601      * <th align="center" colspan="2">
602      * Field by Field Conversion from
603      * {@link GregorianCalendar} to an {@link XMLGregorianCalendar}
604      * </th>
605      * </tr>
606      * <tr>
607      * <th><code>java.util.GregorianCalendar</code> field</th>
608      * <th><code>javax.xml.datatype.XMLGregorianCalendar</code> field</th>
609      * </tr>
610      * </thead>
611      * <tbody>
612      * <tr>
613      * <td><code>ERA == GregorianCalendar.BC ? -YEAR : YEAR</code></td>
614      * <td>{@link XMLGregorianCalendar#setYear(int year)}</td>
615      * </tr>
616      * <tr>
617      * <td><code>MONTH + 1</code></td>
618      * <td>{@link XMLGregorianCalendar#setMonth(int month)}</td>
619      * </tr>
620      * <tr>
621      * <td><code>DAY_OF_MONTH</code></td>
622      * <td>{@link XMLGregorianCalendar#setDay(int day)}</td>
623      * </tr>
624      * <tr>
625      * <td><code>HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND</code></td>
626      * <td>{@link XMLGregorianCalendar#setTime(int hour, int minute, int second, BigDecimal fractional)}</td>
627      * </tr>
628      * <tr>
629      * <td>
630      * <code>(ZONE_OFFSET + DST_OFFSET) / (60*1000)</code><br/>
631      * <em>(in minutes)</em>
632      * </td>
633      * <td>{@link XMLGregorianCalendar#setTimezone(int offset)}<sup><em>*</em></sup>
634      * </td>
635      * </tr>
636      * </tbody>
637      * </table>
638      * <p><em>*</em>conversion loss of information. It is not possible to represent
639      * a <code>java.util.GregorianCalendar</code> daylight savings timezone id in the
640      * XML Schema 1.0 date/time datatype representation.</p>
641      *
642      * <p>To compute the return value's <code>TimeZone</code> field,
643      * <ul>
644      * <li>when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
645      * create a <code>java.util.TimeZone</code> with a custom timezone id
646      * using the <code>this.getTimezone()</code>.</li>
647      * <li>else use the <code>GregorianCalendar</code> default timezone value
648      * for the host is defined as specified by
649      * <code>java.util.TimeZone.getDefault()</code>.</li></p>
650      *
651      * @param cal <code>java.util.GregorianCalendar</code> used to create <code>XMLGregorianCalendar</code>
652      *
653      * @return <code>XMLGregorianCalendar</code> created from <code>java.util.GregorianCalendar</code>
654      *
655      * @throws NullPointerException If <code>cal</code> is <code>null</code>.
656      */

657     public abstract XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(final GregorianCalendar JavaDoc cal);
658
659     /**
660      * <p>Constructor allowing for complete value spaces allowed by
661      * W3C XML Schema 1.0 recommendation for xsd:dateTime and related
662      * builtin datatypes. Note that <code>year</code> parameter supports
663      * arbitrarily large numbers and fractionalSecond has infinite
664      * precision.</p>
665      *
666      * <p>A <code>null</code> value indicates that field isnot set.</p>
667      *
668      * @param year of <code>XMLGregorianCalendar</code> to be created.
669      * @param month of <code>XMLGregorianCalendar</code> to be created.
670      * @param day of <code>XMLGregorianCalendar</code> to be created.
671      * @param hour of <code>XMLGregorianCalendar</code> to be created.
672      * @param minute of <code>XMLGregorianCalendar</code> to be created.
673      * @param second of <code>XMLGregorianCalendar</code> to be created.
674      * @param fractionalSecond of <code>XMLGregorianCalendar</code> to be created.
675      * @param timezone of <code>XMLGregorianCalendar</code> to be created.
676      *
677      * @return <code>XMLGregorianCalendar</code> created from specified values.
678      *
679      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
680      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
681      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
682      * as determined by {@link XMLGregorianCalendar#isValid()}.
683      */

684     public abstract XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(
685         final BigInteger JavaDoc year,
686         final int month,
687         final int day,
688         final int hour,
689         final int minute,
690         final int second,
691         final BigDecimal JavaDoc fractionalSecond,
692         final int timezone);
693     
694     /**
695      * <p>Constructor of value spaces that a
696      * <code>java.util.GregorianCalendar</code> instance would need to convert to an
697      * <code>XMLGregorianCalendar</code> instance.</p>
698      *
699      * <p><code>XMLGregorianCalendar eon</code> and
700      * <code>fractionalSecond</code> are set to <code>null</code></p>
701      *
702      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
703      *
704      * @param year of <code>XMLGregorianCalendar</code> to be created.
705      * @param month of <code>XMLGregorianCalendar</code> to be created.
706      * @param day of <code>XMLGregorianCalendar</code> to be created.
707      * @param hour of <code>XMLGregorianCalendar</code> to be created.
708      * @param minute of <code>XMLGregorianCalendar</code> to be created.
709      * @param second of <code>XMLGregorianCalendar</code> to be created.
710      * @param millisecond of <code>XMLGregorianCalendar</code> to be created.
711      * @param timezone of <code>XMLGregorianCalendar</code> to be created.
712      *
713      * @return <code>XMLGregorianCalendar</code> created from specified values.
714      *
715      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
716      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
717      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
718      * as determined by {@link XMLGregorianCalendar#isValid()}.
719      */

720     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(
721         final int year,
722         final int month,
723         final int day,
724         final int hour,
725         final int minute,
726         final int second,
727         final int millisecond,
728         final int timezone) {
729             
730         // year may be undefined
731
BigInteger JavaDoc realYear = (year != DatatypeConstants.FIELD_UNDEFINED) ? BigInteger.valueOf((long) year) : null;
732             
733         // millisecond may be undefined
734
// millisecond must be >= 0 millisecond <= 1000
735
BigDecimal JavaDoc realMillisecond = null; // undefined value
736
if (millisecond != DatatypeConstants.FIELD_UNDEFINED) {
737             if (millisecond < 0 || millisecond > 1000) {
738                 throw new IllegalArgumentException JavaDoc(
739                             "javax.xml.datatype.DatatypeFactory#newXMLGregorianCalendar("
740                             + "int year, int month, int day, int hour, int minute, int second, int millisecond, int timezone)"
741                             + "with invalid millisecond: " + millisecond
742                             );
743             }
744             
745             realMillisecond = BigDecimal.valueOf((long) millisecond).movePointLeft(3);
746         }
747         
748         return newXMLGregorianCalendar(
749             realYear,
750             month,
751             day,
752             hour,
753             minute,
754             second,
755             realMillisecond,
756             timezone
757         );
758     }
759
760     /**
761      * <p>Create a Java representation of XML Schema builtin datatype <code>date</code> or <code>g*</code>.</p>
762      *
763      * <p>For example, an instance of <code>gYear</code> can be created invoking this factory
764      * with <code>month</code> and <code>day</code> parameters set to
765      * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
766      *
767      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
768      *
769      * @param year of <code>XMLGregorianCalendar</code> to be created.
770      * @param month of <code>XMLGregorianCalendar</code> to be created.
771      * @param day of <code>XMLGregorianCalendar</code> to be created.
772      * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
773      *
774      * @return <code>XMLGregorianCalendar</code> created from parameter values.
775      *
776      * @see DatatypeConstants#FIELD_UNDEFINED
777      *
778      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
779      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
780      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
781      * as determined by {@link XMLGregorianCalendar#isValid()}.
782      */

783     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendarDate(
784         final int year,
785         final int month,
786         final int day,
787         final int timezone) {
788             
789         return newXMLGregorianCalendar(
790             year,
791             month,
792             day,
793             DatatypeConstants.FIELD_UNDEFINED, // hour
794
DatatypeConstants.FIELD_UNDEFINED, // minute
795
DatatypeConstants.FIELD_UNDEFINED, // second
796
DatatypeConstants.FIELD_UNDEFINED, // millisecond
797
timezone);
798         }
799                     
800     /**
801      * <p>Create a Java instance of XML Schema builtin datatype <code>time</code>.</p>
802      *
803      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
804      *
805      * @param hours number of hours
806      * @param minutes number of minutes
807      * @param seconds number of seconds
808      * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
809      *
810      * @return <code>XMLGregorianCalendar</code> created from parameter values.
811      *
812      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
813      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
814      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
815      * as determined by {@link XMLGregorianCalendar#isValid()}.
816      *
817      * @see DatatypeConstants#FIELD_UNDEFINED
818      */

819     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendarTime(
820         final int hours,
821         final int minutes,
822         final int seconds,
823         final int timezone) {
824                 
825         return newXMLGregorianCalendar(
826             DatatypeConstants.FIELD_UNDEFINED, // Year
827
DatatypeConstants.FIELD_UNDEFINED, // Month
828
DatatypeConstants.FIELD_UNDEFINED, // Day
829
hours,
830             minutes,
831             seconds,
832             DatatypeConstants.FIELD_UNDEFINED, //Millisecond
833
timezone);
834     }
835                 
836     /**
837      * <p>Create a Java instance of XML Schema builtin datatype time.</p>
838      *
839      * <p>A <code>null</code> value indicates that field isnot set.</p>
840      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
841      *
842      * @param hours number of hours
843      * @param minutes number of minutes
844      * @param seconds number of seconds
845      * @param fractionalSecond value of <code>null</code> indicates that this optional field is not set.
846      * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
847      *
848      * @return <code>XMLGregorianCalendar</code> created from parameter values.
849      *
850      * @see DatatypeConstants#FIELD_UNDEFINED
851      *
852      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
853      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
854      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
855      * as determined by {@link XMLGregorianCalendar#isValid()}.
856      */

857     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendarTime(
858         final int hours,
859         final int minutes,
860         final int seconds,
861         final BigDecimal JavaDoc fractionalSecond,
862         final int timezone) {
863             
864         return newXMLGregorianCalendar(
865             null, // year
866
DatatypeConstants.FIELD_UNDEFINED, // month
867
DatatypeConstants.FIELD_UNDEFINED, // day
868
hours,
869             minutes,
870             seconds,
871             fractionalSecond,
872             timezone);
873         }
874
875     /**
876      * <p>Create a Java instance of XML Schema builtin datatype time.</p>
877      *
878      * <p>A {@link DatatypeConstants#FIELD_UNDEFINED} value indicates that field isnot set.</p>
879      *
880      * @param hours number of hours
881      * @param minutes number of minutes
882      * @param seconds number of seconds
883      * @param milliseconds number of milliseconds
884      * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.
885      *
886      * @return <code>XMLGregorianCalendar</code> created from parameter values.
887      *
888      * @see DatatypeConstants#FIELD_UNDEFINED
889      *
890      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
891      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
892      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
893      * as determined by {@link XMLGregorianCalendar#isValid()}.
894      */

895     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendarTime(
896         final int hours,
897         final int minutes,
898         final int seconds,
899         final int milliseconds,
900         final int timezone) {
901             
902         // millisecond may be undefined
903
// millisecond must be >= 0 millisecond <= 1000
904
BigDecimal JavaDoc realMilliseconds = null; // undefined value
905
if (milliseconds != DatatypeConstants.FIELD_UNDEFINED) {
906             if (milliseconds < 0 || milliseconds > 1000) {
907                 throw new IllegalArgumentException JavaDoc(
908                             "javax.xml.datatype.DatatypeFactory#newXMLGregorianCalendarTime("
909                             + "int hours, int minutes, int seconds, int milliseconds, int timezone)"
910                             + "with invalid milliseconds: " + milliseconds
911                             );
912             }
913             
914             realMilliseconds = BigDecimal.valueOf((long) milliseconds).movePointLeft(3);
915         }
916         
917         return newXMLGregorianCalendarTime(
918             hours,
919             minutes,
920             seconds,
921             realMilliseconds,
922             timezone
923         );
924     }
925 }
926
Popular Tags