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