libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / xml / datatype / DatatypeFactory.java
blob386d5b852864669997613178af7c4a793e279b1d
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
40 import java.io.File;
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;
49 /**
50 * Factory class to create new datatype objects mapping XML to and from Java
51 * objects.
53 * @author Chris Burdess
54 * @since 1.5
56 public abstract class DatatypeFactory
59 /**
60 * JAXP 1.3 default property name.
62 public static final String DATATYPEFACTORY_PROPERTY = "javax.xml.datatype.DatatypeFactory";
64 /**
65 * JAXP 1.3 default implementation class name.
67 public static final String DATATYPEFACTORY_IMPLEMENTATION_CLASS = "gnu.xml.datatype.JAXPDatatypeFactory";
69 protected DatatypeFactory()
73 /**
74 * Returns a new factory instance.
76 public static DatatypeFactory newInstance()
77 throws DatatypeConfigurationException
79 try
81 // 1. system property
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();
93 p.load(in);
94 in.close();
95 className = p.getProperty(DATATYPEFACTORY_PROPERTY);
96 if (className != null)
97 return (DatatypeFactory) Class.forName(className).newInstance();
99 // 3. services
100 Iterator<DatatypeFactory> i = ServiceLoader.load(DatatypeFactory.class).iterator();
101 if (i.hasNext())
102 return i.next();
103 // 4. fallback
104 Class<?> t = Class.forName(DATATYPEFACTORY_IMPLEMENTATION_CLASS);
105 return (DatatypeFactory) t.newInstance();
107 catch (Exception e)
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,
137 BigInteger years,
138 BigInteger months,
139 BigInteger days,
140 BigInteger hours,
141 BigInteger minutes,
142 BigDecimal seconds);
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,
155 int years,
156 int months,
157 int days,
158 int hours,
159 int minutes,
160 int seconds)
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,
200 BigInteger days,
201 BigInteger hours,
202 BigInteger minutes,
203 BigInteger seconds)
205 return newDuration(isPositive,
206 null,
207 null,
208 days,
209 hours,
210 minutes,
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,
223 int days,
224 int hours,
225 int minutes,
226 int seconds)
228 return newDuration(isPositive,
229 null,
230 null,
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,
264 BigInteger years,
265 BigInteger months)
267 return newDuration(isPositive,
268 years,
269 months,
270 null,
271 null,
272 null,
273 null);
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,
283 int years,
284 int months)
286 return newDuration(isPositive,
287 BigInteger.valueOf((long) years),
288 BigInteger.valueOf((long) months),
289 null,
290 null,
291 null,
292 null);
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
309 * calendar.
311 public abstract XMLGregorianCalendar newXMLGregorianCalendar(GregorianCalendar cal);
314 * Returns a new XMLGregorianCalendar with the specified components.
316 public abstract XMLGregorianCalendar newXMLGregorianCalendar(BigInteger year,
317 int month,
318 int day,
319 int hour,
320 int minute,
321 int second,
322 BigDecimal fractionalSecond,
323 int timezone);
326 * Returns a new XMLGregorianCalendar with the specified components.
328 public XMLGregorianCalendar newXMLGregorianCalendar(int year,
329 int month,
330 int day,
331 int hour,
332 int minute,
333 int second,
334 int millisecond,
335 int timezone)
337 return newXMLGregorianCalendar(BigInteger.valueOf((long) year),
338 month,
339 day,
340 hour,
341 minute,
342 second,
343 new BigDecimal(((double) millisecond) / 1000.0),
344 timezone);
348 * Returns a new XMLGregorianCalendar with the specified components.
350 public XMLGregorianCalendar newXMLGregorianCalendarDate(int year,
351 int month,
352 int day,
353 int timezone)
355 return newXMLGregorianCalendar(BigInteger.valueOf((long) year),
356 month,
357 day,
358 DatatypeConstants.FIELD_UNDEFINED,
359 DatatypeConstants.FIELD_UNDEFINED,
360 DatatypeConstants.FIELD_UNDEFINED,
361 null,
362 timezone);
366 * Returns a new XMLGregorianCalendar with the specified components.
368 public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
369 int minutes,
370 int seconds,
371 int timezone)
373 return newXMLGregorianCalendar(null,
374 DatatypeConstants.FIELD_UNDEFINED,
375 DatatypeConstants.FIELD_UNDEFINED,
376 hours,
377 minutes,
378 seconds,
379 null,
380 timezone);
384 * Returns a new XMLGregorianCalendar with the specified components.
386 public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
387 int minutes,
388 int seconds,
389 BigDecimal fractionalSecond,
390 int timezone)
392 return newXMLGregorianCalendar(null,
393 DatatypeConstants.FIELD_UNDEFINED,
394 DatatypeConstants.FIELD_UNDEFINED,
395 hours,
396 minutes,
397 seconds,
398 fractionalSecond,
399 timezone);
403 * Returns a new XMLGregorianCalendar with the specified components.
405 public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
406 int minutes,
407 int seconds,
408 int milliseconds,
409 int timezone)
411 return newXMLGregorianCalendar(null,
412 DatatypeConstants.FIELD_UNDEFINED,
413 DatatypeConstants.FIELD_UNDEFINED,
414 hours,
415 minutes,
416 seconds,
417 new BigDecimal(((double) milliseconds) / 1000.0),
418 timezone);