KickJava   Java API By Example, From Geeks To Geeks.

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


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

35
36 public class MonthDV extends AbstractDateTimeDV {
37
38     /**
39      * Convert a string to a compiled form
40      *
41      * @param content The lexical representation of gMonth
42      * @return a valid and normalized gMonth object
43      */

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

60     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException{
61         DateTimeData date = new DateTimeData(str, this);
62         int len = str.length();
63
64         //set constants
65
date.year=YEAR;
66         date.day=DAY;
67         if (str.charAt(0)!='-' || str.charAt(1)!='-') {
68             throw new SchemaDateTimeException("Invalid format for gMonth: "+str);
69         }
70         int stop = 4;
71         date.month=parseInt(str,2,stop);
72
73         // REVISIT: allow both --MM and --MM-- now.
74
// need to remove the following 4 lines to disallow --MM--
75
// when the errata is offically in the rec.
76
if (str.length() >= stop+2 &&
77             str.charAt(stop) == '-' && str.charAt(stop+1) == '-') {
78             stop += 2;
79         }
80         if (stop < len) {
81             if (!isNextCharUTCSign(str, stop, len)) {
82                 throw new SchemaDateTimeException ("Error in month parsing: "+str);
83             }
84             else {
85                 getTimeZone(str, date, stop, len);
86             }
87         }
88         //validate and normalize
89
validateDateTime(date);
90
91         //save unnormalized values
92
saveUnnormalized(date);
93         
94         if ( date.utc!=0 && date.utc!='Z' ) {
95             normalize(date);
96         }
97         date.position = 1;
98         return date;
99     }
100
101     /**
102      * Overwrite compare algorithm to optimize month comparison
103      *
104      * REVISIT: this one is lack of the third parameter: boolean strict, so it
105      * doesn't override the method in the base. But maybe this method
106      * is not correctly implemented, and I did encounter errors when
107      * trying to add the extra parameter. I'm leaving it as is. -SG
108      *
109      * @param date1
110      * @param date2
111      * @return less, greater, equal, indeterminate
112      */

113     /*protected short compareDates(DateTimeData date1, DateTimeData date2) {
114
115         if ( date1.utc==date2.utc ) {
116             return (short)((date1.month>=date2.month)?(date1.month>date2.month)?1:0:-1);
117         }
118
119         if ( date1.utc=='Z' || date2.utc=='Z' ) {
120
121             if ( date1.month==date2.month ) {
122                 //--05--Z and --05--
123                 return INDETERMINATE;
124             }
125             if ( (date1.month+1 == date2.month || date1.month-1 == date2.month) ) {
126                 //--05--Z and (--04-- or --05--)
127                 //REVISIT: should this case be less than or equal?
128                 // maxExclusive should fail but what about maxInclusive
129                 //
130                 return INDETERMINATE;
131             }
132         }
133
134         if ( date1.month<date2.month ) {
135             return -1;
136         }
137         else {
138             return 1;
139         }
140
141     }*/

142
143     /**
144      * Converts month object representation to String
145      *
146      * @param date month object
147      * @return lexical representation of month: --MM with an optional time zone sign
148      */

149     protected String JavaDoc dateToString(DateTimeData date) {
150         StringBuffer JavaDoc message = new StringBuffer JavaDoc(5);
151         message.append('-');
152         message.append('-');
153         append(message, date.month, 2);
154         append(message, (char)date.utc, 0);
155         return message.toString();
156     }
157     
158     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
159         return factory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth, DatatypeConstants.FIELD_UNDEFINED
160                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60 + date.timezoneMin);
161     }
162 }
163
Popular Tags