1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.dv.xs;
23
24import javax.xml.datatype.DatatypeConstants;
25import javax.xml.datatype.XMLGregorianCalendar;
26
27import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
28import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
29
30/**
31 * Validator for <gMonthDay> datatype (W3C Schema Datatypes)
32 *
33 * @xerces.internal
34 *
35 * @author Elena Litani
36 * @author Gopal Sharma, SUN Microsystem Inc.
37 *
38 */
39
40public 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     */
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     */
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     */
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
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