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