1: 
  37: 
  38: package ;
  39: 
  40: import ;
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: import ;
  46: import ;
  47: import ;
  48: 
  49: 
  56: public abstract class DatatypeFactory
  57: {
  58: 
  59:   
  62:   public static final String DATATYPEFACTORY_PROPERTY = "javax.xml.datatype.DatatypeFactory";
  63: 
  64:   
  67:   public static final String DATATYPEFACTORY_IMPLEMENTATION_CLASS = "gnu.xml.datatype.JAXPDatatypeFactory";
  68: 
  69:   protected DatatypeFactory()
  70:   {
  71:   }
  72: 
  73:   
  76:   public static DatatypeFactory newInstance()
  77:     throws DatatypeConfigurationException
  78:   {
  79:     try
  80:       {
  81:         
  82:         String className = System.getProperty(DATATYPEFACTORY_PROPERTY);
  83:         if (className != null)
  84:           return (DatatypeFactory) Class.forName(className).newInstance();
  85:         
  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())
  90:           {
  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();
  98:           }
  99:         
 100:         Iterator<DatatypeFactory> i = ServiceLoader.load(DatatypeFactory.class).iterator();
 101:         if (i.hasNext())
 102:           return i.next();
 103:         
 104:         Class<?> t = Class.forName(DATATYPEFACTORY_IMPLEMENTATION_CLASS);
 105:         return (DatatypeFactory) t.newInstance();
 106:       }
 107:     catch (Exception e)
 108:       {
 109:         throw new DatatypeConfigurationException(e);
 110:       }
 111:   }
 112: 
 113:   
 118:   public abstract Duration newDuration(String lexicalRepresentation);
 119: 
 120:   
 124:   public abstract Duration newDuration(long durationInMilliSeconds);
 125: 
 126:   
 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);
 143: 
 144:   
 154:   public Duration newDuration(boolean isPositive,
 155:                               int years,
 156:                               int months,
 157:                               int days,
 158:                               int hours,
 159:                               int minutes,
 160:                               int seconds)
 161:   {
 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));
 169:   }
 170: 
 171:   
 176:   public Duration newDurationDayTime(String lexicalRepresentation)
 177:   {
 178:     return newDuration(lexicalRepresentation);
 179:   }
 180: 
 181:   
 185:   public Duration newDurationDayTime(long durationInMilliseconds)
 186:   {
 187:     
 188:     return newDuration(durationInMilliseconds);
 189:   }
 190: 
 191:   
 199:   public Duration newDurationDayTime(boolean isPositive,
 200:                                      BigInteger days,
 201:                                      BigInteger hours,
 202:                                      BigInteger minutes,
 203:                                      BigInteger seconds)
 204:   {
 205:     return newDuration(isPositive,
 206:                        null,
 207:                        null,
 208:                        days,
 209:                        hours,
 210:                        minutes,
 211:                        new BigDecimal(seconds));
 212:   }
 213: 
 214:   
 222:   public Duration newDurationDayTime(boolean isPositive,
 223:                                      int days,
 224:                                      int hours,
 225:                                      int minutes,
 226:                                      int seconds)
 227:   {
 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));
 235:   }
 236: 
 237:   
 242:   public Duration newDurationYearMonth(String lexicalRepresentation)
 243:   {
 244:     return newDuration(lexicalRepresentation);
 245:   }
 246: 
 247:   
 251:   public Duration newDurationYearMonth(long durationInMilliseconds)
 252:   {
 253:     
 254:     return newDuration(durationInMilliseconds);
 255:   }
 256: 
 257:   
 263:   public Duration newDurationYearMonth(boolean isPositive,
 264:                                        BigInteger years,
 265:                                        BigInteger months)
 266:   {
 267:     return newDuration(isPositive,
 268:                        years,
 269:                        months,
 270:                        null,
 271:                        null,
 272:                        null,
 273:                        null);
 274:   }
 275: 
 276:   
 282:   public Duration newDurationYearMonth(boolean isPositive,
 283:                                        int years,
 284:                                        int months)
 285:   {
 286:     return newDuration(isPositive,
 287:                        BigInteger.valueOf((long) years),
 288:                        BigInteger.valueOf((long) months),
 289:                        null,
 290:                        null,
 291:                        null,
 292:                        null);
 293:   }
 294: 
 295:   
 298:   public abstract XMLGregorianCalendar newXMLGregorianCalendar();
 299: 
 300:   
 305:   public abstract XMLGregorianCalendar newXMLGregorianCalendar(String lexicalRepresentation);
 306: 
 307:   
 311:   public abstract XMLGregorianCalendar newXMLGregorianCalendar(GregorianCalendar cal);
 312: 
 313:   
 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);
 324: 
 325:   
 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)
 336:   {
 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);
 345:   }
 346: 
 347:   
 350:   public XMLGregorianCalendar newXMLGregorianCalendarDate(int year,
 351:                                                           int month,
 352:                                                           int day,
 353:                                                           int timezone)
 354:   {
 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);
 363:   }
 364: 
 365:   
 368:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 369:                                                           int minutes,
 370:                                                           int seconds,
 371:                                                           int timezone)
 372:   {
 373:     return newXMLGregorianCalendar(null,
 374:                                    DatatypeConstants.FIELD_UNDEFINED,
 375:                                    DatatypeConstants.FIELD_UNDEFINED,
 376:                                    hours,
 377:                                    minutes,
 378:                                    seconds,
 379:                                    null,
 380:                                    timezone);
 381:   }
 382: 
 383:   
 386:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 387:                                                           int minutes,
 388:                                                           int seconds,
 389:                                                           BigDecimal fractionalSecond,
 390:                                                           int timezone)
 391:   {
 392:     return newXMLGregorianCalendar(null,
 393:                                    DatatypeConstants.FIELD_UNDEFINED,
 394:                                    DatatypeConstants.FIELD_UNDEFINED,
 395:                                    hours,
 396:                                    minutes,
 397:                                    seconds,
 398:                                    fractionalSecond,
 399:                                    timezone);
 400:   }
 401: 
 402:   
 405:   public XMLGregorianCalendar newXMLGregorianCalendarTime(int hours,
 406:                                                           int minutes,
 407:                                                           int seconds,
 408:                                                           int milliseconds,
 409:                                                           int timezone)
 410:   {
 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);
 419:   }
 420: 
 421: }