MonthDayDV.java revision 286
4176N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
2362N/A * limitations under the License.
2362N/A */
2362N/A
0N/Apackage com.sun.org.apache.xerces.internal.impl.dv.xs;
4880N/A
0N/Aimport javax.xml.datatype.DatatypeConstants;
0N/Aimport javax.xml.datatype.XMLGregorianCalendar;
1178N/A
0N/Aimport com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
0N/Aimport com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
0N/A
0N/A/**
0N/A * Validator for <gMonthDay> datatype (W3C Schema Datatypes)
4880N/A *
4880N/A * @xerces.internal
4880N/A *
4880N/A * @author Elena Litani
4880N/A * @author Gopal Sharma, SUN Microsystem Inc.
4880N/A *
4880N/A * @version $Id: MonthDayDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
4880N/A */
4880N/A
4880N/Apublic class MonthDayDV extends AbstractDateTimeDV {
4880N/A
4880N/A //size without time zone: --MM-DD
4880N/A private final static int MONTHDAY_SIZE = 7;
4880N/A
4880N/A /**
0N/A * Convert a string to a compiled form
0N/A *
0N/A * @param content The lexical representation of gMonthDay
0N/A * @return a valid and normalized gMonthDay object
0N/A */
0N/A public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
0N/A try{
0N/A return parse(content);
4880N/A } catch(Exception ex){
4880N/A throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "gMonthDay"});
4880N/A }
4880N/A }
0N/A
0N/A /**
0N/A * Parses, validates and computes normalized version of gMonthDay object
0N/A *
0N/A * @param str The lexical representation of gMonthDay object --MM-DD
4880N/A * with possible time zone Z or (-),(+)hh:mm
4880N/A * @return normalized date representation
4880N/A * @exception SchemaDateTimeException Invalid lexical representation
4880N/A */
0N/A protected DateTimeData parse(String str) throws SchemaDateTimeException{
0N/A DateTimeData date = new DateTimeData(str, this);
0N/A int len = str.length();
0N/A
4880N/A //initialize
4880N/A date.year=YEAR;
4880N/A
4880N/A if (str.charAt(0)!='-' || str.charAt(1)!='-') {
4880N/A throw new SchemaDateTimeException("Invalid format for gMonthDay: "+str);
0N/A }
0N/A date.month=parseInt(str, 2, 4);
0N/A int start=4;
4880N/A
0N/A if (str.charAt(start++)!='-') {
4880N/A throw new SchemaDateTimeException("Invalid format for gMonthDay: " + str);
4880N/A }
0N/A
0N/A date.day=parseInt(str, start, start+2);
0N/A
4880N/A if ( MONTHDAY_SIZE<len ) {
0N/A if (!isNextCharUTCSign(str, MONTHDAY_SIZE, len)) {
4880N/A throw new SchemaDateTimeException ("Error in month parsing:" +str);
4880N/A }
0N/A else {
0N/A getTimeZone(str, date, MONTHDAY_SIZE, len);
0N/A }
4880N/A }
0N/A //validate and normalize
4880N/A
4880N/A validateDateTime(date);
0N/A
0N/A //save unnormalized values
0N/A saveUnnormalized(date);
4880N/A
0N/A if ( date.utc!=0 && date.utc!='Z' ) {
1178N/A normalize(date);
1178N/A }
0N/A date.position = 1;
return date;
}
/**
* Converts gMonthDay object representation to String
*
* @param date gmonthDay object
* @return lexical representation of month: --MM-DD with an optional time zone sign
*/
protected String dateToString(DateTimeData date) {
StringBuffer message = new StringBuffer(8);
message.append('-');
message.append('-');
append(message, date.month, 2);
message.append('-');
append(message, date.day, 2);
append(message, (char)date.utc, 0);
return message.toString();
}
protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
return datatypeFactory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth, date.unNormDay,
DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
}
}