1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
22 
23 import javax.xml.datatype.DatatypeConstants;
24 import javax.xml.datatype.XMLGregorianCalendar;
25 
26 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
27 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
28 
29 /**
30  * Validator for <gMonthDay> datatype (W3C Schema Datatypes)
31  *
32  * @xerces.internal
33  *
34  * @author Elena Litani
35  * @author Gopal Sharma, SUN Microsystem Inc.
36  *
37  * @version $Id: MonthDayDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
38  */
39 
40 public class MonthDayDV extends AbstractDateTimeDV {
41 
42     //size without time zone: --MM-DD
43     private final static int MONTHDAY_SIZE = 7;
44 
45     /**
46      * Convert a string to a compiled form
47      *
48      * @param  content The lexical representation of gMonthDay
49      * @return a valid and normalized gMonthDay object
50      */
getActualValue(String content, ValidationContext context)51     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
52         try{
53             return parse(content);
54         } catch(Exception ex){
55             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "gMonthDay"});
56         }
57     }
58 
59     /**
60      * Parses, validates and computes normalized version of gMonthDay object
61      *
62      * @param str    The lexical representation of gMonthDay object --MM-DD
63      *               with possible time zone Z or (-),(+)hh:mm
64      * @return normalized date representation
65      * @exception SchemaDateTimeException Invalid lexical representation
66      */
parse(String str)67     protected DateTimeData parse(String str) throws SchemaDateTimeException{
68         DateTimeData date = new DateTimeData(str, this);
69         int len = str.length();
70 
71         //initialize
72         date.year=YEAR;
73 
74         if (str.charAt(0)!='-' || str.charAt(1)!='-') {
75             throw new SchemaDateTimeException("Invalid format for gMonthDay: "+str);
76         }
77         date.month=parseInt(str, 2, 4);
78         int start=4;
79 
80         if (str.charAt(start++)!='-') {
81             throw new SchemaDateTimeException("Invalid format for gMonthDay: " + str);
82         }
83 
84         date.day=parseInt(str, start, start+2);
85 
86         if ( MONTHDAY_SIZE<len ) {
87             if (!isNextCharUTCSign(str, MONTHDAY_SIZE, len)) {
88                 throw new SchemaDateTimeException ("Error in month parsing:" +str);
89             }
90             else {
91                 getTimeZone(str, date, MONTHDAY_SIZE, len);
92             }
93         }
94         //validate and normalize
95 
96         validateDateTime(date);
97 
98         //save unnormalized values
99         saveUnnormalized(date);
100 
101         if ( date.utc!=0 && date.utc!='Z' ) {
102             normalize(date);
103         }
104         date.position = 1;
105         return date;
106     }
107 
108     /**
109      * Converts gMonthDay object representation to String
110      *
111      * @param date   gmonthDay object
112      * @return lexical representation of month: --MM-DD with an optional time zone sign
113      */
dateToString(DateTimeData date)114     protected String dateToString(DateTimeData date) {
115         StringBuffer message = new StringBuffer(8);
116         message.append('-');
117         message.append('-');
118         append(message, date.month, 2);
119         message.append('-');
120         append(message, date.day, 2);
121         append(message, (char)date.utc, 0);
122         return message.toString();
123     }
124 
getXMLGregorianCalendar(DateTimeData date)125     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
126         return datatypeFactory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth, date.unNormDay,
127                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
128                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
129                 date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
130     }
131 }
132