1 /* DatatypeFactory.java --
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.xml
.datatype
;
41 import java
.io
.FileInputStream
;
42 import java
.math
.BigDecimal
;
43 import java
.math
.BigInteger
;
44 import java
.util
.GregorianCalendar
;
45 import java
.util
.Iterator
;
46 import java
.util
.Properties
;
47 import java
.util
.ServiceLoader
;
50 * Factory class to create new datatype objects mapping XML to and from Java
53 * @author Chris Burdess
56 public abstract class DatatypeFactory
60 * JAXP 1.3 default property name.
62 public static final String DATATYPEFACTORY_PROPERTY
= "javax.xml.datatype.DatatypeFactory";
65 * JAXP 1.3 default implementation class name.
67 public static final String DATATYPEFACTORY_IMPLEMENTATION_CLASS
= "gnu.xml.datatype.JAXPDatatypeFactory";
69 protected DatatypeFactory()
74 * Returns a new factory instance.
76 public static DatatypeFactory
newInstance()
77 throws DatatypeConfigurationException
82 String className
= System
.getProperty(DATATYPEFACTORY_PROPERTY
);
83 if (className
!= null)
84 return (DatatypeFactory
) Class
.forName(className
).newInstance();
85 // 2. jaxp.properties property
86 File javaHome
= new File(System
.getProperty("java.home"));
87 File javaHomeLib
= new File(javaHome
, "lib");
88 File jaxpProperties
= new File(javaHomeLib
, "jaxp.properties");
89 if (jaxpProperties
.exists())
91 FileInputStream in
= new FileInputStream(jaxpProperties
);
92 Properties p
= new Properties();
95 className
= p
.getProperty(DATATYPEFACTORY_PROPERTY
);
96 if (className
!= null)
97 return (DatatypeFactory
) Class
.forName(className
).newInstance();
100 Iterator
<DatatypeFactory
> i
= ServiceLoader
.load(DatatypeFactory
.class).iterator();
104 Class
<?
> t
= Class
.forName(DATATYPEFACTORY_IMPLEMENTATION_CLASS
);
105 return (DatatypeFactory
) t
.newInstance();
109 throw new DatatypeConfigurationException(e
);
114 * Returns a new duration from its string representation.
115 * @param lexicalRepresentation the lexical representation of the
116 * duration, as specified in XML Schema 1.0 section 3.2.6.1.
118 public abstract Duration
newDuration(String lexicalRepresentation
);
121 * Returns a new duration.
122 * @param durationInMilliSeconds the duration in milliseconds
124 public abstract Duration
newDuration(long durationInMilliSeconds
);
127 * Returns a new duration by specifying the individual components.
128 * @param isPositive whether the duration is positive
129 * @param years the number of years
130 * @param months the number of months
131 * @param days the number of days
132 * @param hours the number of hours
133 * @param minutes th number of minutes
134 * @param seconds the number of seconds
136 public abstract Duration
newDuration(boolean isPositive
,
145 * Returns a new duration by specifying the individual components.
146 * @param isPositive whether the duration is positive
147 * @param years the number of years
148 * @param months the number of months
149 * @param days the number of days
150 * @param hours the number of hours
151 * @param minutes th number of minutes
152 * @param seconds the number of seconds
154 public Duration
newDuration(boolean isPositive
,
162 return newDuration(isPositive
,
163 BigInteger
.valueOf((long) years
),
164 BigInteger
.valueOf((long) months
),
165 BigInteger
.valueOf((long) days
),
166 BigInteger
.valueOf((long) hours
),
167 BigInteger
.valueOf((long) minutes
),
168 BigDecimal
.valueOf((long) seconds
));
172 * Returns a new dayTimeDuration from its string representation.
173 * @param lexicalRepresentation the lexical representation of the
174 * duration, as specified in XML Schema 1.0 section 3.2.6.1.
176 public Duration
newDurationDayTime(String lexicalRepresentation
)
178 return newDuration(lexicalRepresentation
);
182 * Returns a new dayTimeDuration.
183 * @param durationInMilliseconds the duration in milliseconds
185 public Duration
newDurationDayTime(long durationInMilliseconds
)
187 // TODO xmlSchemaType
188 return newDuration(durationInMilliseconds
);
192 * Returns a new dayTimeDuration by specifying the individual components.
193 * @param isPositive whether the duration is positive
194 * @param days the number of days
195 * @param hours the number of hours
196 * @param minutes th number of minutes
197 * @param seconds the number of seconds
199 public Duration
newDurationDayTime(boolean isPositive
,
205 return newDuration(isPositive
,
211 new BigDecimal(seconds
));
215 * Returns a new dayTimeDuration by specifying the individual components.
216 * @param isPositive whether the duration is positive
217 * @param days the number of days
218 * @param hours the number of hours
219 * @param minutes th number of minutes
220 * @param seconds the number of seconds
222 public Duration
newDurationDayTime(boolean isPositive
,
228 return newDuration(isPositive
,
231 BigInteger
.valueOf((long) days
),
232 BigInteger
.valueOf((long) hours
),
233 BigInteger
.valueOf((long) minutes
),
234 BigDecimal
.valueOf((long) seconds
));
238 * Returns a new yearMonthDuration from its string representation.
239 * @param lexicalRepresentation the lexical representation of the
240 * duration, as specified in XML Schema 1.0 section 3.2.6.1.
242 public Duration
newDurationYearMonth(String lexicalRepresentation
)
244 return newDuration(lexicalRepresentation
);
248 * Returns a new yearMonthDuration.
249 * @param durationInMilliseconds the duration in milliseconds
251 public Duration
newDurationYearMonth(long durationInMilliseconds
)
253 // TODO xmlSchemaType
254 return newDuration(durationInMilliseconds
);
258 * Returns a new yearMonthDuration by specifying the individual components.
259 * @param isPositive whether the duration is positive
260 * @param years the number of years
261 * @param months the number of months
263 public Duration
newDurationYearMonth(boolean isPositive
,
267 return newDuration(isPositive
,
277 * Returns a new yearMonthDuration by specifying the individual components.
278 * @param isPositive whether the duration is positive
279 * @param years the number of years
280 * @param months the number of months
282 public Duration
newDurationYearMonth(boolean isPositive
,
286 return newDuration(isPositive
,
287 BigInteger
.valueOf((long) years
),
288 BigInteger
.valueOf((long) months
),
296 * Returns a new XMLGregorianCalendar with no fields initialized.
298 public abstract XMLGregorianCalendar
newXMLGregorianCalendar();
301 * Returns a new XMLGregorianCalendar from a string representation.
302 * @param lexicalRepresentation the lexical representation as specified in
303 * XML Schema 1.0 Part 2, section 3.2.[7-14].1.
305 public abstract XMLGregorianCalendar
newXMLGregorianCalendar(String lexicalRepresentation
);
308 * Returns a new XMLGregorianCalendar based on the specified Gregorian
311 public abstract XMLGregorianCalendar
newXMLGregorianCalendar(GregorianCalendar cal
);
314 * Returns a new XMLGregorianCalendar with the specified components.
316 public abstract XMLGregorianCalendar
newXMLGregorianCalendar(BigInteger year
,
322 BigDecimal fractionalSecond
,
326 * Returns a new XMLGregorianCalendar with the specified components.
328 public XMLGregorianCalendar
newXMLGregorianCalendar(int year
,
337 return newXMLGregorianCalendar(BigInteger
.valueOf((long) year
),
343 new BigDecimal(((double) millisecond
) / 1000.0),
348 * Returns a new XMLGregorianCalendar with the specified components.
350 public XMLGregorianCalendar
newXMLGregorianCalendarDate(int year
,
355 return newXMLGregorianCalendar(BigInteger
.valueOf((long) year
),
358 DatatypeConstants
.FIELD_UNDEFINED
,
359 DatatypeConstants
.FIELD_UNDEFINED
,
360 DatatypeConstants
.FIELD_UNDEFINED
,
366 * Returns a new XMLGregorianCalendar with the specified components.
368 public XMLGregorianCalendar
newXMLGregorianCalendarTime(int hours
,
373 return newXMLGregorianCalendar(null,
374 DatatypeConstants
.FIELD_UNDEFINED
,
375 DatatypeConstants
.FIELD_UNDEFINED
,
384 * Returns a new XMLGregorianCalendar with the specified components.
386 public XMLGregorianCalendar
newXMLGregorianCalendarTime(int hours
,
389 BigDecimal fractionalSecond
,
392 return newXMLGregorianCalendar(null,
393 DatatypeConstants
.FIELD_UNDEFINED
,
394 DatatypeConstants
.FIELD_UNDEFINED
,
403 * Returns a new XMLGregorianCalendar with the specified components.
405 public XMLGregorianCalendar
newXMLGregorianCalendarTime(int hours
,
411 return newXMLGregorianCalendar(null,
412 DatatypeConstants
.FIELD_UNDEFINED
,
413 DatatypeConstants
.FIELD_UNDEFINED
,
417 new BigDecimal(((double) milliseconds
) / 1000.0),