KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

59     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException{
60         DateTimeData date = new DateTimeData(str, this);
61         int len = str.length();
62
63         // get date
64
int end = getYearMonth(str, 0, len, date);
65         date.day = DAY;
66         parseTimeZone (str, end, len, date);
67
68         //validate and normalize
69

70         validateDateTime(date);
71
72         //save unnormalized values
73
saveUnnormalized(date);
74         
75         if ( date.utc!=0 && date.utc!='Z' ) {
76             normalize(date);
77         }
78         date.position = 0;
79         return date;
80     }
81
82     protected String JavaDoc dateToString(DateTimeData date) {
83         StringBuffer JavaDoc message = new StringBuffer JavaDoc(25);
84         append(message, date.year, 4);
85         message.append('-');
86         append(message, date.month, 2);
87         append(message, (char)date.utc, 0);
88         return message.toString();
89     }
90     
91     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
92         return factory.newXMLGregorianCalendar(date.unNormYear, date.unNormMonth, DatatypeConstants.FIELD_UNDEFINED
93                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60 + date.timezoneMin);
94     }
95 }
96
97
98
Popular Tags