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 <gYearMonth> datatype (W3C Schema Datatypes)
028: *
029: * @xerces.internal
030: *
031: * @author Elena Litani
032: * @author Gopal Sharma, SUN Microsystem Inc.
033: *
034: * @version $Id: YearMonthDV.java 446745 2006-09-15 21:43:58Z mrglavas $
035: */
036: public class YearMonthDV extends AbstractDateTimeDV {
037:
038: /**
039: * Convert a string to a compiled form
040: *
041: * @param content The lexical representation of gYearMonth
042: * @return a valid and normalized gYearMonth object
043: */
044: public Object getActualValue(String content,
045: ValidationContext context)
046: throws InvalidDatatypeValueException {
047: try {
048: return parse(content);
049: } catch (Exception ex) {
050: throw new InvalidDatatypeValueException(
051: "cvc-datatype-valid.1.2.1", new Object[] { content,
052: "gYearMonth" });
053: }
054: }
055:
056: /**
057: * Parses, validates and computes normalized version of gYearMonth object
058: *
059: * @param str The lexical representation of gYearMonth object CCYY-MM
060: * with possible time zone Z or (-),(+)hh:mm
061: * @return normalized date representation
062: * @exception SchemaDateTimeException Invalid lexical representation
063: */
064: protected DateTimeData parse(String str)
065: throws SchemaDateTimeException {
066: DateTimeData date = new DateTimeData(str, this );
067: int len = str.length();
068:
069: // get date
070: int end = getYearMonth(str, 0, len, date);
071: date.day = DAY;
072: parseTimeZone(str, end, len, date);
073:
074: //validate and normalize
075:
076: validateDateTime(date);
077:
078: //save unnormalized values
079: saveUnnormalized(date);
080:
081: if (date.utc != 0 && date.utc != 'Z') {
082: normalize(date);
083: }
084: date.position = 0;
085: return date;
086: }
087:
088: protected String dateToString(DateTimeData date) {
089: StringBuffer message = new StringBuffer(25);
090: append(message, date.year, 4);
091: message.append('-');
092: append(message, date.month, 2);
093: append(message, (char) date.utc, 0);
094: return message.toString();
095: }
096:
097: protected XMLGregorianCalendar getXMLGregorianCalendar(
098: DateTimeData date) {
099: return factory.newXMLGregorianCalendar(date.unNormYear,
100: date.unNormMonth, DatatypeConstants.FIELD_UNDEFINED,
101: DatatypeConstants.FIELD_UNDEFINED,
102: DatatypeConstants.FIELD_UNDEFINED,
103: DatatypeConstants.FIELD_UNDEFINED,
104: DatatypeConstants.FIELD_UNDEFINED, date.timezoneHr * 60
105: + date.timezoneMin);
106: }
107: }
|