Source for javax.xml.datatype.DatatypeFactory

   1: /* DatatypeFactory.java -- 
   2:    Copyright (C) 2004, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: package javax.xml.datatype;
  39: 
  40: import java.io.BufferedReader;
  41: import java.io.File;
  42: import java.io.FileInputStream;
  43: import java.io.InputStream;
  44: import java.io.InputStreamReader;
  45: import java.math.BigDecimal;
  46: import java.math.BigInteger;
  47: import java.util.GregorianCalendar;
  48: import java.util.Iterator;
  49: import java.util.Properties;
  50: import gnu.classpath.ServiceFactory;
  51: 
  52: /**
  53:  * Factory class to create new datatype objects mapping XML to and from Java
  54:  * objects.
  55:  *
  56:  * @author Chris Burdess
  57:  * @since 1.5
  58:  */
  59: public abstract class DatatypeFactory
  60: {
  61: 
  62:   /**
  63:    * JAXP 1.3 default property name.
  64:    */
  65:   public static final String DATATYPEFACTORY_PROPERTY = "javax.xml.datatype.DatatypeFactory";
  66: 
  67:   /**
  68:    * JAXP 1.3 default implementation class name.
  69:    */
  70:   public static final String DATATYPEFACTORY_IMPLEMENTATION_CLASS = "gnu.xml.datatype.JAXPDatatypeFactory";
  71: 
  72:   protected DatatypeFactory()
  73:   {
  74:   }
  75: 
  76:   /**
  77:    * Returns a new factory instance.
  78:    */
  79:   public static DatatypeFactory newInstance()
  80:     throws DatatypeConfigurationException
  81:   {
  82:     try
  83:       {
  84:         // 1. system property
  85:         String className = System.getProperty(DATATYPEFACTORY_PROPERTY);
  86:         if (className != null)
  87:           return (DatatypeFactory) Class.forName(className).newInstance();
  88:         // 2. jaxp.properties property
  89:         File javaHome = new File(System.getProperty("java.home"));
  90:         File javaHomeLib = new File(javaHome, "lib");
  91:         File jaxpProperties = new File(javaHomeLib, "jaxp.properties");
  92:         if (jaxpProperties.exists())
  93:           {
  94:             FileInputStream in = new FileInputStream(jaxpProperties);
  95:             Properties p = new Properties();
  96:             p.load(in);
  97:             in.close();
  98:             className = p.getProperty(DATATYPEFACTORY_PROPERTY);
  99:             if (className != null)
 100:               return (DatatypeFactory) Class.forName(className).newInstance();
 101:           }
 102:         // 3. services
 103:         Iterator i = ServiceFactory.lookupProviders(DatatypeFactory.class);
 104:         if (i.hasNext())
 105:           return (DatatypeFactory) i.next();
 106:         // 4. fallback
 107:         Class t = Class.forName(DATATYPEFACTORY_IMPLEMENTATION_CLASS);
 108:         return (DatatypeFactory) t.newInstance();
 109:       }
 110:     catch (Exception e)
 111:       {
 112:         throw new DatatypeConfigurationException(e);
 113:       }
 114:   }
 115: 
 116:   /**
 117:    * Returns a new duration from its string representation.
 118:    * @param lexicalRepresentation the lexical representation of the
 119:    * duration, as specified in XML Schema 1.0 section 3.2.6.1.
 120:    */
 121:   public abstract Duration newDuration(String lexicalRepresentation);
 122: 
 123:   /**
 124:    * Returns a new duration.
 125:    * @param durationInMilliSeconds the duration in milliseconds
 126:    */
 127:   public abstract Duration newDuration(long durationInMilliSeconds);
 128: 
 129:   /**
 130:    * Returns a new duration by specifying the individual components.
 131:    * @param isPositive whether the duration is positive
 132:    * @param years the number of years
 133:    * @param months the number of months
 134:    * @param days the number of days
 135:    * @param hours the number of hours
 136:    * @param minutes th number of minutes
 137:    * @param seconds the number of seconds
 138:    */
 139:   public abstract Duration newDuration(boolean isPositive,
 140:                                        BigInteger years,
 141:                                        BigInteger months,
 142:                                        BigInteger days,
 143:                                        BigInteger hours,
 144:                                        BigInteger minutes,
 145:                                        BigDecimal seconds);
 146: 
 147:   /**
 148:    * Returns a new duration by specifying the individual components.
 149:    * @param isPositive whether the duration is positive
 150:    * @param years the number of years
 151:    * @param months the number of months
 152:    * @param days the number of days
 153:    * @param hours the number of hours
 154:    * @param minutes th number of minutes
 155:    * @param seconds the number of seconds
 156:    */
 157:   public Duration newDuration(boolean isPositive,
 158:                               int years,
 159:                               int months,
 160:                               int days,
 161:                               int hours,
 162:                               int minutes,
 163:                               int seconds)
 164:   {
 165:     return newDuration(isPositive,
 166:                        BigInteger.valueOf((long) years),
 167:                        BigInteger.valueOf((long) months),
 168:                        BigInteger.valueOf((long) days),
 169:                        BigInteger.valueOf((long) hours),
 170:                        BigInteger.valueOf((long) minutes),
 171:                        BigDecimal.valueOf((long) seconds));
 172:   }
 173: 
 174:   /**
 175:    * Returns a new dayTimeDuration from its string representation.
 176:    * @param lexicalRepresentation the lexical representation of the
 177:    * duration, as specified in XML Schema 1.0 section 3.2.6.1.
 178:    */
 179:   public Duration newDurationDayTime(String lexicalRepresentation)
 180:   {
 181:     return newDuration(lexicalRepresentation);
 182:   }
 183: 
 184:   /**
 185:    * Returns a new dayTimeDuration.
 186:    * @param durationInMilliseconds the duration in milliseconds
 187:    */
 188:   public Duration newDurationDayTime(long durationInMilliseconds)
 189:   {
 190:     // TODO xmlSchemaType
 191:     return newDuration(durationInMilliseconds);
 192:   }
 193: 
 194:   /**
 195:    * Returns a new dayTimeDuration by specifying the individual components.
 196:    * @param isPositive whether the duration is positive
 197:    * @param days the number of days
 198:    * @param hours the number of hours
 199:    * @param minutes th number of minutes
 200:    * @param seconds the number of seconds
 201:    */
 202:   public Duration newDurationDayTime(boolean isPositive,
 203:                                      BigInteger days,
 204:                                      BigInteger hours,
 205:                                      BigInteger minutes,
 206:                                      BigInteger seconds)
 207:   {
 208:     return newDuration(isPositive,
 209:                        null,
 210:                        null,
 211:                        days,
 212:                        hours,
 213:                        minutes,
 214:                        new BigDecimal(seconds));
 215:   }
 216: 
 217:   /**
 218:    * Returns a new dayTimeDuration by specifying the individual components.
 219:    * @param isPositive whether the duration is positive
 220:    * @param days the number of days
 221:    * @param hours the number of hours
 222:    * @param minutes th number of minutes
 223:    * @param seconds the number of seconds
 224:    */
 225:   public Duration newDurationDayTime(boolean isPositive,
 226:                                      int days,
 227:                                      int hours,
 228:                                      int minutes,
 229:                                      int seconds)
 230:   {
 231:     return newDuration(isPositive,
 232:                        null,
 233:                        null,
 234:                        BigInteger.valueOf((long) days),
 235:                        BigInteger.valueOf((long) hours),
 236:                        BigInteger.valueOf((long) minutes),
 237:                        BigDecimal.valueOf((long) seconds));
 238:   }
 239: 
 240:   /**
 241:    * Returns a new yearMonthDuration from its string representation.
 242:    * @param lexicalRepresentation the lexical representation of the
 243:    * duration, as specified in XML Schema 1.0 section 3.2.6.1.
 244:    */
 245:   public Duration newDurationYearMonth(String lexicalRepresentation)
 246:   {
 247:     return newDuration(lexicalRepresentation);
 248:   }
 249: 
 250:   /**
 251:    * Returns a new yearMonthDuration.
 252:    * @param durationInMilliseconds the duration in milliseconds
 253:    */
 254:   public Duration newDurationYearMonth(long durationInMilliseconds)
 255:   {
 256:     // TODO xmlSchemaType
 257:     return newDuration(durationInMilliseconds);
 258:   }
 259: 
 260:   /**
 261:    * Returns a new yearMonthDuration by specifying the individual components.
 262:    * @param isPositive whether the duration is positive
 263:    * @param years the number of years
 264:    * @param months the number of months
 265:    */
 266:   public Duration newDurationYearMonth(boolean isPositive,
 267:                                        BigInteger years,
 268:                                        BigInteger months)
 269:   {
 270:     return newDuration(isPositive,
 271:                        years,
 272:                        months,
 273:                        null,
 274:                        null,
 275:                        null,
 276:                        null);
 277:   }
 278: 
 279:   /**
 280:    * Returns a new yearMonthDuration by specifying the individual components.
 281:    * @param isPositive whether the duration is positive
 282:    * @param years the number of years
 283:    * @param months the number of months
 284:    */
 285:   public Duration newDurationYearMonth(boolean isPositive,
 286:                                        int years,
 287:                                        int months)
 288:   {
 289:     return newDuration(isPositive,
 290:                        BigInteger.valueOf((long) years),
 291:                        BigInteger.valueOf((long) months),
 292:                        null,
 293:                        null,
 294:                        null,
 295:                        null);
 296:   }
 297: 
 298:   /**
 299:    * Returns a new XMLGregorianCalendar with no fields initialized.
 300:    */
 301:   public abstract XMLGregorianCalendar newXMLGregorianCalendar();
 302: 
 303:   /**
 304:    * Returns a new XMLGregorianCalendar from a string representation.
 305:    * @param lexicalRepresentation the lexical representation as specified in
 306:    * XML Schema 1.0 Part 2, section 3.2.[7-14].1.
 307:    */
 308:   public abstract XMLGregorianCalendar newXMLGregorianCalendar(String lexicalRepresentation);
 309:   
 310:   /**
 311:    * Returns a new XMLGregorianCalendar based on the specified Gregorian
 312:    * calendar.
 313:    */
 314:   public abstract XMLGregorianCalendar newXMLGregorianCalendar(GregorianCalendar cal);
 315: 
 316:   /**
 317:    * Returns a new XMLGregorianCalendar with the specified components.
 318:    */
 319:   public abstract XMLGregorianCalendar newXMLGregorianCalendar(BigInteger year,
 320:                                                                int month,
 321:                                                                int day,
 322:                                                                int hour,
 323:                                                                int minute,
 324:                                                                int second,
 325:                                                                BigDecimal fractionalSecond,
 326:                                                                int timezone);
 327: 
 328:   /**
 329:    * Returns a new XMLGregorianCalendar with the specified components.
 330:    */
 331:   public XMLGregorianCalendar newXMLGregorianCalendar(int year,
 332:                                                       int month,
 333:                                                       int day,
 334:                                                       int hour,
 335:                                                       int minute,
 336:                                                       int second,
 337:                                                       int millisecond,
 338:                                                       int timezone)
 339:   {
 340:     return newXMLGregorianCalendar(BigInteger.valueOf((long) year),
 341:                                    month,
 342:                                    day,
 343:                                    hour,
 344:                                    minute,
 345:                                    second,
 346:                                    new BigDecimal(((double) millisecond) / 1000.0),
 347:                                    timezone);
 348:   }
 349: 
 350:   /**
 351:    * Returns a new XMLGregorianCalendar with the specified components.
 352:    */
 353:   public XMLGregorianCalendar newXMLGregorianCalendarDate(int year,
 354:                                                           int month,
 355:                                                           int day,
 356:                                                           int timezone)
 357:   {
 358:     return newXMLGregorianCalendar(BigInteger.valueOf((long) year),
 359:                                    month,
 360:                                    day,
 361:                                    DatatypeConstants.FIELD_UNDEFINED,
 362:                                    DatatypeConstants.FIELD_UNDEFINED,
 363:                                    DatatypeConstants.FIELD_UNDEFINED,
 364:                                    null,
 365:                                    timezone);
 366:   }
 367: 
 368:   /**
 369:    * Returns a new XMLGregorianCalendar with the specified components.
 370:    */
 371:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 372:                                                           int minutes,
 373:                                                           int seconds,
 374:                                                           int timezone)
 375:   {
 376:     return newXMLGregorianCalendar(null,
 377:                                    DatatypeConstants.FIELD_UNDEFINED,
 378:                                    DatatypeConstants.FIELD_UNDEFINED,
 379:                                    hours,
 380:                                    minutes,
 381:                                    seconds,
 382:                                    null,
 383:                                    timezone);
 384:   }
 385: 
 386:   /**
 387:    * Returns a new XMLGregorianCalendar with the specified components.
 388:    */
 389:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 390:                                                           int minutes,
 391:                                                           int seconds,
 392:                                                           BigDecimal fractionalSecond,
 393:                                                           int timezone)
 394:   {
 395:     return newXMLGregorianCalendar(null,
 396:                                    DatatypeConstants.FIELD_UNDEFINED,
 397:                                    DatatypeConstants.FIELD_UNDEFINED,
 398:                                    hours,
 399:                                    minutes,
 400:                                    seconds,
 401:                                    fractionalSecond,
 402:                                    timezone);
 403:   }
 404: 
 405:   /**
 406:    * Returns a new XMLGregorianCalendar with the specified components.
 407:    */
 408:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 409:                                                           int minutes,
 410:                                                           int seconds,
 411:                                                           int milliseconds,
 412:                                                           int timezone)
 413:   {
 414:     return newXMLGregorianCalendar(null,
 415:                                    DatatypeConstants.FIELD_UNDEFINED,
 416:                                    DatatypeConstants.FIELD_UNDEFINED,
 417:                                    hours,
 418:                                    minutes,
 419:                                    seconds,
 420:                                    new BigDecimal(((double) milliseconds) / 1000.0),
 421:                                    timezone);
 422:   }
 423:     
 424: }