KickJava   Java API By Example, From Geeks To Geeks.

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


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 <gYear> datatype (W3C Schema Datatypes)
27  *
28  * @xerces.internal
29  *
30  * @author Elena Litani
31  * @author Gopal Sharma, SUN Microsystem Inc.
32  *
33  * @version $Id: YearDV.java,v 1.17 2005/05/06 15:31:14 ankitp Exp $
34  */

35
36 public class YearDV extends AbstractDateTimeDV {
37
38     /**
39      * Convert a string to a compiled form
40      *
41      * @param content The lexical representation of time
42      * @return a valid and normalized time 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, "gYear"});
49         }
50     }
51
52     /**
53      * Parses, validates and computes normalized version of gYear object
54      *
55      * @param str The lexical representation of year object CCYY
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         // check for preceding '-' sign
65
int start = 0;
66         if (str.charAt(0)=='-') {
67             start = 1;
68         }
69         int sign = findUTCSign(str, start, len);
70         if (sign == -1) {
71             date.year=parseIntYear(str, len);
72         }
73         else {
74             date.year=parseIntYear(str, sign);
75             getTimeZone (str, date, sign, len);
76         }
77
78         //initialize values
79
date.month=MONTH;
80         date.day=1;
81
82         //validate and normalize
83
validateDateTime(date);
84
85         //save unnormalized values
86
saveUnnormalized(date);
87         
88         if ( date.utc!=0 && date.utc!='Z' ) {
89             normalize(date);
90         }
91         date.position = 0;
92         return date;
93     }
94
95     /**
96      * Converts year object representation to String
97      *
98      * @param date year object
99      * @return lexical representation of month: CCYY with optional time zone sign
100      */

101     protected String JavaDoc dateToString(DateTimeData date) {
102         StringBuffer JavaDoc message = new StringBuffer JavaDoc(5);
103         append(message, date.year, 4);
104         append(message, (char)date.utc, 0);
105         return message.toString();
106     }
107     
108     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
109         return factory.newXMLGregorianCalendar(date.unNormYear, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED
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