DateTime.java

  1. /**
  2.  * **********************************************************************
  3.  *
  4.  * <p>Licensed to the Apache Software Foundation (ASF) under one or more contributor license
  5.  * agreements. See the NOTICE file distributed with this work for additional information regarding
  6.  * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance with the License. You may obtain a
  8.  * copy of the License at
  9.  *
  10.  * <p>http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
  13.  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  14.  * express or implied. See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  *
  17.  * <p>**********************************************************************
  18.  */
  19. package org.odftoolkit.odfdom.type;

  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.xml.datatype.DatatypeFactory;
  23. import javax.xml.datatype.XMLGregorianCalendar;

  24. /** This class represents the in OpenDocument format used data type {@odf.datatype dateTime} */
  25. public class DateTime implements OdfDataType {

  26.   private XMLGregorianCalendar mDateTime;

  27.   /**
  28.    * Construct an newly DateTime object that represents the specified XMLGregorianCalendar value
  29.    *
  30.    * @param dateTime the value to be represented by the DateTime Object
  31.    * @throws IllegalArgumentException if the given argument is not a valid DateTime
  32.    */
  33.   public DateTime(XMLGregorianCalendar dateTime) throws IllegalArgumentException {
  34.     if (dateTime == null) {
  35.       throw new IllegalArgumentException("parameter can not be null for DateTime");
  36.     }
  37.     // validate 'dateTime' type which is defined in W3C schema
  38.     // http://www.w3.org/TR/xmlschema-2/#dateTime
  39.     if (!W3CSchemaType.isValid("dateTime", dateTime.toXMLFormat())) {
  40.       throw new IllegalArgumentException("parameter is invalid for datatype dateTime");
  41.     }
  42.     mDateTime = dateTime;
  43.   }

  44.   /**
  45.    * Returns a String Object representing this DateTime value
  46.    *
  47.    * @return return a string representation of the value of this DateTime object
  48.    */
  49.   @Override
  50.   public String toString() {
  51.     return mDateTime.toXMLFormat();
  52.   }

  53.   /**
  54.    * Returns a DateTime instance representing the specified String value
  55.    *
  56.    * @param stringValue a String value
  57.    * @return return a DateTime instance representing stringValue
  58.    * @throws IllegalArgumentException if the given argument is not a valid DateTime
  59.    */
  60.   public static DateTime valueOf(String stringValue) throws IllegalArgumentException {
  61.     try {
  62.       DatatypeFactory aFactory = new org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl();
  63.       return new DateTime(aFactory.newXMLGregorianCalendar(stringValue));
  64.     } catch (IllegalArgumentException ex) {
  65.       Logger.getLogger(DateTime.class.getName())
  66.           .log(Level.SEVERE, "parameter is invalid for datatype DateTime", ex);
  67.       throw new IllegalArgumentException("parameter is invalid for datatype DateTime");
  68.     }
  69.   }

  70.   /**
  71.    * Returns the value of this DateTime object as an XMLGregorianCalendar
  72.    *
  73.    * @return the XMLGregorianCalendar value of this DateTime object.
  74.    */
  75.   public XMLGregorianCalendar getXMLGregorianCalendar() {
  76.     return mDateTime;
  77.   }

  78.   /**
  79.    * check if the specified XMLGregorianCalendar instance is a valid {@odf.datatype dateTime} data
  80.    * type
  81.    *
  82.    * @param date the value to be tested
  83.    * @return true if the value of argument is valid for {@odf.datatype dateTime} data type false
  84.    *     otherwise
  85.    */
  86.   public static boolean isValid(XMLGregorianCalendar date) {
  87.     if (date == null) {
  88.       return false;
  89.     } else {
  90.       return W3CSchemaType.isValid("dateTime", date.toString());
  91.     }
  92.   }
  93. }