KickJava   Java API By Example, From Geeks To Geeks.

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


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

35
36 public class MonthDayDV extends AbstractDateTimeDV {
37
38     //size without time zone: --MM-DD
39
private final static int MONTHDAY_SIZE = 7;
40
41     /**
42      * Convert a string to a compiled form
43      *
44      * @param content The lexical representation of gMonthDay
45      * @return a valid and normalized gMonthDay object
46      */

47     public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
48         try{
49             return parse(content);
50         } catch(Exception JavaDoc ex){
51             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "gMonthDay"});
52         }
53     }
54
55     /**
56      * Parses, validates and computes normalized version of gMonthDay object
57      *
58      * @param str The lexical representation of gMonthDay object --MM-DD
59      * with possible time zone Z or (-),(+)hh:mm
60      * @return normalized date representation
61      * @exception SchemaDateTimeException Invalid lexical representation
62      */

63     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException{
64         DateTimeData date = new DateTimeData(str, this);
65         int len = str.length();
66
67         //initialize
68
date.year=YEAR;
69
70         if (str.charAt(0)!='-' || str.charAt(1)!='-') {
71             throw new SchemaDateTimeException("Invalid format for gMonthDay: "+str);
72         }
73         date.month=parseInt(str, 2, 4);
74         int start=4;
75
76         if (str.charAt(start++)!='-') {
77             throw new SchemaDateTimeException("Invalid format for gMonthDay: " + str);
78         }
79
80         date.day=parseInt(str, start, start+2);
81
82         if ( MONTHDAY_SIZE<len ) {
83             if (!isNextCharUTCSign(str, MONTHDAY_SIZE, len)) {
84                 throw new SchemaDateTimeException ("Error in month parsing:" +str);
85             }
86             else {
87                 getTimeZone(str, date, MONTHDAY_SIZE, len);
88             }
89         }
90         //validate and normalize
91

92         validateDateTime(date);
93         
94         //save unnormalized values
95
saveUnnormalized(date);
96         
97         if ( date.utc!=0 && date.utc!='Z' ) {
98             normalize(date);
99         }
100         date.position = 1;
101         return date;
102     }
103
104     /**
105      * Converts gMonthDay object representation to String
106      *
107      * @param date gmonthDay object
108      * @return lexical representation of month: --MM-DD with an optional time zone sign
109      */

110     protected String JavaDoc dateToString(DateTimeData date) {
111         StringBuffer JavaDoc message = new StringBuffer JavaDoc(8);
112         message.append('-');
113         message.append('-');
114         append(message, date.month, 2);
115         message.append('-');
116         append(message, date.day, 2);
117         append(message, (char)date.utc, 0);
118         return message.toString();
119     }
120     
121     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
122         return factory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth, date.unNormDay
123                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60 + date.timezoneMin);
124     }
125 }
126
127
Popular Tags