KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > datatype > DatatypeFactoryImpl


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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

59 public class DatatypeFactoryImpl
60     extends DatatypeFactory JavaDoc {
61             
62     /**
63      * <p>Public constructor is empty..</p>
64      *
65      * <p>Use {@link DatatypeFactory#newInstance()} to create a <code>DatatypeFactory</code>.</p>
66      */

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

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

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

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

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

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

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

325     public XMLGregorianCalendar JavaDoc newXMLGregorianCalendar(
326         final BigInteger JavaDoc year,
327         final int month,
328         final int day,
329         final int hour,
330         final int minute,
331         final int second,
332         final BigDecimal JavaDoc fractionalSecond,
333         final int timezone) {
334             
335         return new XMLGregorianCalendarImpl(
336             year,
337             month,
338             day,
339             hour,
340             minute,
341             second,
342             fractionalSecond,
343             timezone
344         );
345     }
346 }
347
Popular Tags