1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
  22 
  23 import java.math.BigInteger;
  24 
  25 import javax.xml.datatype.DatatypeConstants;
  26 import javax.xml.datatype.XMLGregorianCalendar;
  27 
  28 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  29 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  30 
  31 /**
  32  * Validator for <dateTime> datatype (W3C Schema Datatypes)
  33  *
  34  * @xerces.internal
  35  *
  36  * @author Elena Litani
  37  * @author Gopal Sharma, SUN Microsystem Inc.
  38  *
  39  */
  40 public class DateTimeDV extends AbstractDateTimeDV {
  41 
  42     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  43         try{
  44             return parse(content);
  45         } catch(Exception ex){
  46             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "dateTime"});
  47         }
  48     }
  49 
  50     /**
  51      * Parses, validates and computes normalized version of dateTime object
  52      *
  53      * @param str    The lexical representation of dateTime object CCYY-MM-DDThh:mm:ss.sss
  54      *               with possible time zone Z or (-),(+)hh:mm
  55      * @return normalized dateTime representation
  56      * @exception SchemaDateTimeException Invalid lexical representation
  57      */
  58     protected DateTimeData parse(String str) throws SchemaDateTimeException {
  59         DateTimeData date = new DateTimeData(str, this);
  60         int len = str.length();
  61 
  62         int end = indexOf (str, 0, len, 'T');
  63 
  64         // both time and date
  65         int dateEnd = getDate(str, 0, end, date);
  66         getTime(str, end+1, len, date);
  67 
  68         //Check the separator character between Date and Time
  69         if (dateEnd != end) {
  70             throw new RuntimeException(str
  71                     + " is an invalid dateTime dataype value. "
  72                     + "Invalid character(s) seprating date and time values.");
  73         }
  74 
  75         //validate and normalize
  76 
  77         //REVISIT: do we need SchemaDateTimeException?
  78         validateDateTime(date);
  79 
  80         //save unnormalized values
  81         saveUnnormalized(date);
  82 
  83         if (date.utc!=0 && date.utc!='Z') {
  84             normalize(date);
  85         }
  86         return date;
  87     }
  88 
  89     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
  90         return datatypeFactory.newXMLGregorianCalendar(BigInteger.valueOf(date.unNormYear), date.unNormMonth,
  91                 date.unNormDay, date.unNormHour, date.unNormMinute,
  92                 (int)date.unNormSecond, date.unNormSecond != 0 ? getFractionalSecondsAsBigDecimal(date) : null,
  93                 date.hasTimeZone() ? (date.timezoneHr * 60 + date.timezoneMin) : DatatypeConstants.FIELD_UNDEFINED);
  94     }
  95 }