1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.impl.dv.xs;
23 
24 import java.math.BigInteger;
25 
26 import javax.xml.datatype.DatatypeConstants;
27 import javax.xml.datatype.XMLGregorianCalendar;
28 
29 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
30 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
31 
32 /**
33  * Validator for <dateTime> datatype (W3C Schema Datatypes)
34  *
35  * @xerces.internal
36  *
37  * @author Elena Litani
38  * @author Gopal Sharma, SUN Microsystem Inc.
39  *
40  */
41 public class DateTimeDV extends AbstractDateTimeDV {
42 
getActualValue(String content, ValidationContext context)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      */
parse(String str)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 
getXMLGregorianCalendar(DateTimeData date)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 }
97