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