KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.datatype.DatatypeConstants JavaDoc;
20 import javax.xml.datatype.XMLGregorianCalendar JavaDoc;
21
22 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
23 import org.apache.xerces.impl.dv.ValidationContext;
24
25 /**
26  * Validator for <date> datatype (W3C Schema datatypes)
27  *
28  * @xerces.internal
29  *
30  * @author Elena Litani
31  * @author Gopal Sharma, SUN Microsystems Inc.
32  *
33  * @version $Id: DateDV.java,v 1.20 2005/07/19 04:49:50 mrglavas Exp $
34  */

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

53     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException {
54         DateTimeData date = new DateTimeData(str, this);
55         int len = str.length();
56
57         int end = getDate(str, 0, len, date);
58         parseTimeZone (str, end, len, date);
59
60         //validate and normalize
61
//REVISIT: do we need SchemaDateTimeException?
62
validateDateTime(date);
63
64         //save unnormalized values
65
saveUnnormalized(date);
66         
67         if (date.utc!=0 && date.utc!='Z') {
68             normalize(date);
69         }
70         return date;
71     }
72
73     protected String JavaDoc dateToString(DateTimeData date) {
74         StringBuffer JavaDoc message = new StringBuffer JavaDoc(25);
75         append(message, date.year, 4);
76         message.append('-');
77         append(message, date.month, 2);
78         message.append('-');
79         append(message, date.day, 2);
80         append(message, (char)date.utc, 0);
81         return message.toString();
82     }
83     
84     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
85         return factory.newXMLGregorianCalendar(date.unNormYear, date.unNormMonth, date.unNormDay
86                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60 + date.timezoneMin);
87     }
88
89 }
90
Popular Tags