001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv.xs;
019:
020: import javax.xml.datatype.DatatypeConstants;
021: import javax.xml.datatype.XMLGregorianCalendar;
022:
023: import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
024: import org.apache.xerces.impl.dv.ValidationContext;
025:
026: /**
027: * Validator for <gMonthDay> datatype (W3C Schema Datatypes)
028: *
029: * @xerces.internal
030: *
031: * @author Elena Litani
032: * @author Gopal Sharma, SUN Microsystem Inc.
033: *
034: * @version $Id: MonthDayDV.java 446745 2006-09-15 21:43:58Z mrglavas $
035: */
036:
037: public class MonthDayDV extends AbstractDateTimeDV {
038:
039: //size without time zone: --MM-DD
040: private final static int MONTHDAY_SIZE = 7;
041:
042: /**
043: * Convert a string to a compiled form
044: *
045: * @param content The lexical representation of gMonthDay
046: * @return a valid and normalized gMonthDay object
047: */
048: public Object getActualValue(String content,
049: ValidationContext context)
050: throws InvalidDatatypeValueException {
051: try {
052: return parse(content);
053: } catch (Exception ex) {
054: throw new InvalidDatatypeValueException(
055: "cvc-datatype-valid.1.2.1", new Object[] { content,
056: "gMonthDay" });
057: }
058: }
059:
060: /**
061: * Parses, validates and computes normalized version of gMonthDay object
062: *
063: * @param str The lexical representation of gMonthDay object --MM-DD
064: * with possible time zone Z or (-),(+)hh:mm
065: * @return normalized date representation
066: * @exception SchemaDateTimeException Invalid lexical representation
067: */
068: protected DateTimeData parse(String str)
069: throws SchemaDateTimeException {
070: DateTimeData date = new DateTimeData(str, this );
071: int len = str.length();
072:
073: //initialize
074: date.year = YEAR;
075:
076: if (str.charAt(0) != '-' || str.charAt(1) != '-') {
077: throw new SchemaDateTimeException(
078: "Invalid format for gMonthDay: " + str);
079: }
080: date.month = parseInt(str, 2, 4);
081: int start = 4;
082:
083: if (str.charAt(start++) != '-') {
084: throw new SchemaDateTimeException(
085: "Invalid format for gMonthDay: " + str);
086: }
087:
088: date.day = parseInt(str, start, start + 2);
089:
090: if (MONTHDAY_SIZE < len) {
091: if (!isNextCharUTCSign(str, MONTHDAY_SIZE, len)) {
092: throw new SchemaDateTimeException(
093: "Error in month parsing:" + str);
094: } else {
095: getTimeZone(str, date, MONTHDAY_SIZE, len);
096: }
097: }
098: //validate and normalize
099:
100: validateDateTime(date);
101:
102: //save unnormalized values
103: saveUnnormalized(date);
104:
105: if (date.utc != 0 && date.utc != 'Z') {
106: normalize(date);
107: }
108: date.position = 1;
109: return date;
110: }
111:
112: /**
113: * Converts gMonthDay object representation to String
114: *
115: * @param date gmonthDay object
116: * @return lexical representation of month: --MM-DD with an optional time zone sign
117: */
118: protected String dateToString(DateTimeData date) {
119: StringBuffer message = new StringBuffer(8);
120: message.append('-');
121: message.append('-');
122: append(message, date.month, 2);
123: message.append('-');
124: append(message, date.day, 2);
125: append(message, (char) date.utc, 0);
126: return message.toString();
127: }
128:
129: protected XMLGregorianCalendar getXMLGregorianCalendar(
130: DateTimeData date) {
131: return factory.newXMLGregorianCalendar(
132: DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth,
133: date.unNormDay, DatatypeConstants.FIELD_UNDEFINED,
134: DatatypeConstants.FIELD_UNDEFINED,
135: DatatypeConstants.FIELD_UNDEFINED,
136: DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60
137: + date.timezoneMin);
138: }
139: }
|