KickJava   Java API By Example, From Geeks To Geeks.

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


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 <gDay> datatype (W3C Schema datatypes)
27  *
28  * @xerces.internal
29  *
30  * @author Elena Litani
31  * @author Gopal Sharma, SUN Microsystem Inc.
32  * @version $Id: DayDV.java,v 1.18 2005/07/19 04:32:12 mrglavas Exp $
33  */

34 public class DayDV extends AbstractDateTimeDV {
35
36     //size without time zone: ---09
37
private final static int DAY_SIZE=5;
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, "gDay"});
44         }
45     }
46
47     /**
48      * Parses, validates and computes normalized version of gDay object
49      *
50      * @param str The lexical representation of gDay object ---DD
51      * with possible time zone Z or (-),(+)hh:mm
52      * Pattern: ---(\\d\\d)(Z|(([-+])(\\d\\d)(:(\\d\\d))?
53      * @return normalized date representation
54      * @exception SchemaDateTimeException Invalid lexical representation
55      */

56     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException {
57         DateTimeData date = new DateTimeData(str, this);
58         int len = str.length();
59
60         if (str.charAt(0)!='-' || str.charAt(1)!='-' || str.charAt(2)!='-') {
61             throw new SchemaDateTimeException ("Error in day parsing");
62         }
63
64         //initialize values
65
date.year=YEAR;
66         date.month=MONTH;
67
68         date.day=parseInt(str, 3,5);
69
70         if ( DAY_SIZE<len ) {
71             if (!isNextCharUTCSign(str, DAY_SIZE, len)) {
72                 throw new SchemaDateTimeException ("Error in day parsing");
73             }
74             else {
75                 getTimeZone(str, date, DAY_SIZE, len);
76             }
77         }
78
79        //validate and normalize
80
validateDateTime(date);
81
82         //save unnormalized values
83
saveUnnormalized(date);
84         
85         if ( date.utc!=0 && date.utc!='Z' ) {
86             normalize(date);
87         }
88         date.position = 2;
89         return date;
90     }
91
92     /**
93      * Converts gDay object representation to String
94      *
95      * @param date gDay object
96      * @return lexical representation of gDay: ---DD with an optional time zone sign
97      */

98     protected String JavaDoc dateToString(DateTimeData date) {
99         StringBuffer JavaDoc message = new StringBuffer JavaDoc(6);
100         message.append('-');
101         message.append('-');
102         message.append('-');
103         append(message, date.day, 2);
104         append(message, (char)date.utc, 0);
105         return message.toString();
106     }
107     
108     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
109         return factory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.unNormDay
110                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60 + date.timezoneMin);
111     }
112
113 }
114
115
Popular Tags