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  * @version $Id: DateTimeDV.java,v 1.7 2010-11-01 04:39:46 joehw Exp $
  40  */
  41 public class DateTimeDV extends AbstractDateTimeDV {
  42 
  43     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  44         try{
  45             return parse(content);
  46         } catch(Exception ex){
  47             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "dateTime"});
  48         }
  49     }
  50 
  51     /**
  52      * Parses, validates and computes normalized version of dateTime object
  53      *
  54      * @param str    The lexical representation of dateTime object CCYY-MM-DDThh:mm:ss.sss
  55      *               with possible time zone Z or (-),(+)hh:mm
  56      * @return normalized dateTime representation
  57      * @exception SchemaDateTimeException Invalid lexical representation
  58      */
  59     protected DateTimeData parse(String str) throws SchemaDateTimeException {
  60         DateTimeData date = new DateTimeData(str, this);
  61         int len = str.length();
  62 
  63         int end = indexOf (str, 0, len, 'T');
  64 
  65         // both time and date
  66         int dateEnd = getDate(str, 0, end, date);
  67         getTime(str, end+1, len, date);
  68 
  69         //Check the separator character between Date and Time
  70         if (dateEnd != end) {
  71             throw new RuntimeException(str
  72                     + " is an invalid dateTime dataype value. "
  73                     + "Invalid character(s) seprating date and time values.");
  74         }
  75 
  76         //validate and normalize
  77 
  78         //REVISIT: do we need SchemaDateTimeException?
  79         validateDateTime(date);
  80 
  81         //save unnormalized values
  82         saveUnnormalized(date);
  83 
  84         if (date.utc!=0 && date.utc!='Z') {
  85             normalize(date);
  86         }
  87         return date;
  88     }
  89 
  90     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
  91         return datatypeFactory.newXMLGregorianCalendar(BigInteger.valueOf(date.unNormYear), date.unNormMonth,
  92                 date.unNormDay, date.unNormHour, date.unNormMinute,
  93                 (int)date.unNormSecond, date.unNormSecond != 0 ? getFractionalSecondsAsBigDecimal(date) : null,
  94                 date.hasTimeZone() ? (date.timezoneHr * 60 + date.timezoneMin) : DatatypeConstants.FIELD_UNDEFINED);
  95     }
  96 }