KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > datatype > DatatypeFactoryImpl


1 // $Id: DatatypeFactoryImpl.java,v 1.4 2004/04/30 01:40:53 jsuttor Exp $
2

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

53 public class DatatypeFactoryImpl
54     extends DatatypeFactory JavaDoc {
55             
56     /**
57      * <p>Public constructor is empty..</p>
58      *
59      * <p>Use {@link DatatypeFactory#newInstance()} to create a <code>DatatypeFactory<code>.</p>
60      */

61     public DatatypeFactoryImpl() {
62     }
63
64     /**
65      * <p>Obtain a new instance of a <code>Duration</code>
66      * specifying the <code>Duration</code> as its string representation, "PnYnMnDTnHnMnS",
67      * as defined in XML Schema 1.0 section 3.2.6.1.</p>
68      *
69      * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
70      * <blockquote>
71      * duration represents a duration of time.
72      * The value space of duration is a six-dimensional space where the coordinates designate the
73      * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
74      * These components are ordered in their significance by their order of appearance i.e. as
75      * year, month, day, hour, minute, and second.
76      * </blockquote>
77      * <p>All six values are set and availabe from the created {@link Duration}</p>
78      *
79      * <p>The XML Schema specification states that values can be of an arbitrary size.
80      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
81      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
82      * if implementation capacities are exceeded.</p>
83      *
84      * @param lexicalRepresentation <code>String</code> representation of a <code>Duration</code>.
85      *
86      * @return New <code>Duration</code> created from parsing the <code>lexicalRepresentation</code>.
87      *
88      * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code>.
89      * @throws UnsupportedOperationException If implementation cannot support requested values.
90      * @throws NullPointerException if <code>lexicalRepresentation</code> is <code>null</code>.
91      */

92     public Duration JavaDoc newDuration(final String JavaDoc lexicalRepresentation) {
93         
94         return new DurationImpl(lexicalRepresentation);
95     }
96     
97     /**
98      * <p>Obtain a new instance of a <code>Duration</code>
99      * specifying the <code>Duration</code> as milliseconds.</p>
100      *
101      * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
102      * <blockquote>
103      * duration represents a duration of time.
104      * The value space of duration is a six-dimensional space where the coordinates designate the
105      * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
106      * These components are ordered in their significance by their order of appearance i.e. as
107      * year, month, day, hour, minute, and second.
108      * </blockquote>
109      * <p>All six values are set by computing their values from the specified milliseconds
110      * and are availabe using the <code>get</code> methods of the created {@link Duration}.
111      * The values conform to and are defined by:</p>
112      * <ul>
113      * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
114      * <li><a HREF="http://www.w3.org/TR/xmlschema-2/#isoformats">
115      * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
116      * </li>
117      * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
118      * </ul>
119      *
120      * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
121      * {@link java.util.Calendar#YEAR} = 1970,
122      * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
123      * {@link java.util.Calendar#DATE} = 1, etc.
124      * This is important as there are variations in the Gregorian Calendar,
125      * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
126      * so the result of {@link Duration#getMonths()} and {@link Duration#getDays()} can be influenced.</p>
127      *
128      * @param durationInMilliseconds Duration in milliseconds to create.
129      *
130      * @return New <code>Duration</code> representing <code>durationInMilliseconds</code>.
131      */

132     public Duration JavaDoc newDuration(final long durationInMilliseconds) {
133         
134         return new DurationImpl(durationInMilliseconds);
135     }
136     
137     /**
138      * <p>Obtain a new instance of a <code>Duration</code>
139      * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
140      *
141      * <p>The XML Schema specification states that values can be of an arbitrary size.
142      * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
143      * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
144      * if implementation capacities are exceeded.</p>
145      *
146      * @param isPositive Set to <code>false</code> to create a negative duration. When the length
147      * of the duration is zero, this parameter will be ignored.
148      * @param years of this <code>Duration</code>
149      * @param months of this <code>Duration</code>
150      * @param days of this <code>Duration</code>
151      * @param hours of this <code>Duration</code>
152      * @param minutes of this <code>Duration</code>
153      * @param seconds of this <code>Duration</code>
154      *
155      * @return New <code>Duration</code> created from the specified values.
156      *
157      * @throws IllegalArgumentException If values are not a valid representation of a <code>Duration</code>.
158      * @throws UnsupportedOperationException If implementation cannot support requested values.
159      * @throws NullPointerException If any values are <code>null</code>.
160      *
161      * @see #newDuration(boolean isPositive, BigInteger years, BigInteger months, BigInteger days,
162      * BigInteger hours, BigInteger minutes, BigDecimal seconds)
163      */

164     public Duration JavaDoc newDuration(
165         final boolean isPositive,
166         final BigInteger JavaDoc years,
167         final BigInteger JavaDoc months,
168         final BigInteger JavaDoc days,
169         final BigInteger JavaDoc hours,
170         final BigInteger JavaDoc minutes,
171         final BigDecimal JavaDoc seconds) {
172             
173         return new DurationImpl(
174                 isPositive,
175                 years,
176                 months,
177                 days,
178                 hours,
179                 minutes,
180                 seconds
181             );
182         }
183
184     /**
185      * <p>Create a new instance of an <code>XMLGregorianCalendar</code>.</p>
186      *
187      * <p>All date/time datatype fields set to {@link DatatypeConstants#FIELD_UNDEFINED} or null.</p>
188      *
189      * @return New <code>XMLGregorianCalendar</code> with all date/time datatype fields set to
190      * {@link DatatypeConstants#FIELD_UNDEFINED} or null.
191      */

192     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar() {
193         
194         return new XMLGregorianCalendarImpl();
195     }
196     
197     /**
198      * <p>Create a new XMLGregorianCalendar by parsing the String as a lexical representation.</p>
199      *
200      * <p>Parsing the lexical string representation is defined in
201      * <a HREF="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
202      * <em>Lexical Representation</em>.</a></p>
203      *
204      * <p>The string representation may not have any leading and trailing whitespaces.</p>
205      *
206      * <p>The parsing is done field by field so that
207      * the following holds for any lexically correct String x:</p>
208      * <pre>
209      * newXMLGregorianCalendar(x).toXMLFormat().equals(x)
210      * </pre>
211      * <p>Except for the noted lexical/canonical representation mismatches
212      * listed in <a HREF="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
213      * XML Schema 1.0 errata, Section 3.2.7.2</a>.</p>
214      *
215      * @param lexicalRepresentation Lexical representation of one the eight XML Schema date/time datatypes.
216      *
217      * @return <code>XMLGregorianCalendar</code> created from the <code>lexicalRepresentation</code>.
218      *
219      * @throws IllegalArgumentException If the <code>lexicalRepresentation</code> is not a valid <code>XMLGregorianCalendar</code>.
220      * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
221      */

222     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(final String JavaDoc lexicalRepresentation) {
223         
224         return new XMLGregorianCalendarImpl(lexicalRepresentation);
225     }
226     
227     /**
228      * <p>Create an <code>XMLGregorianCalendar</code> from a {@link GregorianCalendar}.</p>
229      *
230      * <table border="2" rules="all" cellpadding="2">
231      * <thead>
232      * <tr>
233      * <th align="center" colspan="2">
234      * Field by Field Conversion from
235      * {@link GregorianCalendar} to an {@link XMLGregorianCalendar}
236      * </th>
237      * </tr>
238      * <tr>
239      * <th><code>java.util.GregorianCalendar</code> field</th>
240      * <th><code>javax.xml.datatype.XMLGregorianCalendar</code> field</th>
241      * </tr>
242      * </thead>
243      * <tbody>
244      * <tr>
245      * <td><code>ERA == GregorianCalendar.BC ? -YEAR : YEAR</code></td>
246      * <td>{@link XMLGregorianCalendar#setYear(int year)}</td>
247      * </tr>
248      * <tr>
249      * <td><code>MONTH + 1</code></td>
250      * <td>{@link XMLGregorianCalendar#setMonth(int month)}</td>
251      * </tr>
252      * <tr>
253      * <td><code>DAY_OF_MONTH</code></td>
254      * <td>{@link XMLGregorianCalendar#setDay(int day)}</td>
255      * </tr>
256      * <tr>
257      * <td><code>HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND</code></td>
258      * <td>{@link XMLGregorianCalendar#setTime(int hour, int minute, int second, BigDecimal fractional)}</td>
259      * </tr>
260      * <tr>
261      * <td>
262      * <code>(ZONE_OFFSET + DST_OFFSET) / (60*1000)</code><br/>
263      * <em>(in minutes)</em>
264      * </td>
265      * <td>{@link XMLGregorianCalendar#setTimezone(int offset)}<sup><em>*</em></sup>
266      * </td>
267      * </tr>
268      * </tbody>
269      * </table>
270      * <p><em>*</em>conversion loss of information. It is not possible to represent
271      * a <code>java.util.GregorianCalendar</code> daylight savings timezone id in the
272      * XML Schema 1.0 date/time datatype representation.</p>
273      *
274      * <p>To compute the return value's <code>TimeZone</code> field,
275      * <ul>
276      * <li>when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
277      * create a <code>java.util.TimeZone</code> with a custom timezone id
278      * using the <code>this.getTimezone()</code>.</li>
279      * <li>else use the <code>GregorianCalendar</code> default timezone value
280      * for the host is defined as specified by
281      * <code>java.util.TimeZone.getDefault()</code>.</li></p>
282      *
283      * @param cal <code>java.util.GregorianCalendar</code> used to create <code>XMLGregorianCalendar</code>
284      *
285      * @return <code>XMLGregorianCalendar</code> created from <code>java.util.GregorianCalendar</code>
286      *
287      * @throws NullPointerException If <code>cal</code> is <code>null</code>.
288      */

289     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(final GregorianCalendar JavaDoc cal) {
290         
291         return new XMLGregorianCalendarImpl(cal);
292     }
293
294     /**
295      * <p>Constructor allowing for complete value spaces allowed by
296      * W3C XML Schema 1.0 recommendation for xsd:dateTime and related
297      * builtin datatypes. Note that <code>year</code> parameter supports
298      * arbitrarily large numbers and fractionalSecond has infinite
299      * precision.</p>
300      *
301      * @param year of <code>XMLGregorianCalendar</code> to be created.
302      * @param month of <code>XMLGregorianCalendar</code> to be created.
303      * @param day of <code>XMLGregorianCalendar</code> to be created.
304      * @param hour of <code>XMLGregorianCalendar</code> to be created.
305      * @param minute of <code>XMLGregorianCalendar</code> to be created.
306      * @param second of <code>XMLGregorianCalendar</code> to be created.
307      * @param fractionalSecond of <code>XMLGregorianCalendar</code> to be created.
308      * @param timezone of <code>XMLGregorianCalendar</code> to be created.
309      *
310      * @return <code>XMLGregorianCalendar</code> created from specified values.
311      *
312      * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
313      * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
314      * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
315      * as determined by {@link XMLGregorianCalendar#isValid()}.
316      * @throws NullPointerException If any parameters are <code>null</code>.
317      *
318      */

319     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(
320         final BigInteger JavaDoc year,
321         final int month,
322         final int day,
323         final int hour,
324         final int minute,
325         final int second,
326         final BigDecimal JavaDoc fractionalSecond,
327         final int timezone) {
328             
329         return new XMLGregorianCalendarImpl(
330             year,
331             month,
332             day,
333             hour,
334             minute,
335             second,
336             fractionalSecond,
337             timezone
338         );
339     }
340 }
341
Popular Tags