KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > DateTimeDV


1 /*
2  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.xs;
18
19 import java.math.BigDecimal JavaDoc;
20 import java.math.BigInteger JavaDoc;
21
22 import javax.xml.datatype.XMLGregorianCalendar JavaDoc;
23
24 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
25 import org.apache.xerces.impl.dv.ValidationContext;
26
27 /**
28  * Validator for <dateTime> datatype (W3C Schema Datatypes)
29  *
30  * @xerces.internal
31  *
32  * @author Elena Litani
33  * @author Gopal Sharma, SUN Microsystem Inc.
34  *
35  * @version $Id: DateTimeDV.java,v 1.16 2005/05/06 15:31:14 ankitp Exp $
36  */

37 public class DateTimeDV extends AbstractDateTimeDV {
38
39     public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
40         try{
41             return parse(content);
42         } catch(Exception JavaDoc ex){
43             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "dateTime"});
44         }
45     }
46
47     /**
48      * Parses, validates and computes normalized version of dateTime object
49      *
50      * @param str The lexical representation of dateTime object CCYY-MM-DDThh:mm:ss.sss
51      * with possible time zone Z or (-),(+)hh:mm
52      * @return normalized dateTime representation
53      * @exception SchemaDateTimeException Invalid lexical representation
54      */

55     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException {
56         DateTimeData date = new DateTimeData(str, this);
57         int len = str.length();
58
59         int end = indexOf (str, 0, len, 'T');
60
61         // both time and date
62
int dateEnd = getDate(str, 0, end, date);
63         getTime(str, end+1, len, date);
64
65         //Check the separator character between Date and Time
66
if (dateEnd != end) {
67             throw new RuntimeException JavaDoc(str
68                     + " is an invalid dateTime dataype value. "
69                     + "Invalid character(s) seprating date and time values.");
70         }
71
72         //validate and normalize
73

74         //REVISIT: do we need SchemaDateTimeException?
75
validateDateTime(date);
76
77         //save unnormalized values
78
saveUnnormalized(date);
79         
80         if (date.utc!=0 && date.utc!='Z') {
81             normalize(date);
82         }
83         return date;
84     }
85     
86     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
87         return factory.newXMLGregorianCalendar(BigInteger.valueOf(date.unNormYear), date.unNormMonth, date.unNormDay
88                 , date.unNormHour, date.unNormMinute, (int)date.unNormSecond, date.unNormSecond != 0?new BigDecimal JavaDoc(date.unNormSecond - ((int)date.unNormSecond)):null, date.timezoneHr * 60 + date.timezoneMin);
89     }
90 }
91
Popular Tags