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 java.math.BigDecimal;
021: import java.math.BigInteger;
022:
023: import javax.xml.datatype.XMLGregorianCalendar;
024:
025: import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
026: import org.apache.xerces.impl.dv.ValidationContext;
027:
028: /**
029: * Validator for <dateTime> datatype (W3C Schema Datatypes)
030: *
031: * @xerces.internal
032: *
033: * @author Elena Litani
034: * @author Gopal Sharma, SUN Microsystem Inc.
035: *
036: * @version $Id: DateTimeDV.java 446745 2006-09-15 21:43:58Z mrglavas $
037: */
038: public class DateTimeDV extends AbstractDateTimeDV {
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: "dateTime" });
049: }
050: }
051:
052: /**
053: * Parses, validates and computes normalized version of dateTime object
054: *
055: * @param str The lexical representation of dateTime object CCYY-MM-DDThh:mm:ss.sss
056: * with possible time zone Z or (-),(+)hh:mm
057: * @return normalized dateTime representation
058: * @exception SchemaDateTimeException Invalid lexical representation
059: */
060: protected DateTimeData parse(String str)
061: throws SchemaDateTimeException {
062: DateTimeData date = new DateTimeData(str, this );
063: int len = str.length();
064:
065: int end = indexOf(str, 0, len, 'T');
066:
067: // both time and date
068: int dateEnd = getDate(str, 0, end, date);
069: getTime(str, end + 1, len, date);
070:
071: //Check the separator character between Date and Time
072: if (dateEnd != end) {
073: throw new RuntimeException(
074: str
075: + " is an invalid dateTime dataype value. "
076: + "Invalid character(s) seprating date and time values.");
077: }
078:
079: //validate and normalize
080:
081: //REVISIT: do we need SchemaDateTimeException?
082: validateDateTime(date);
083:
084: //save unnormalized values
085: saveUnnormalized(date);
086:
087: if (date.utc != 0 && date.utc != 'Z') {
088: normalize(date);
089: }
090: return date;
091: }
092:
093: protected XMLGregorianCalendar getXMLGregorianCalendar(
094: DateTimeData date) {
095: return factory
096: .newXMLGregorianCalendar(BigInteger
097: .valueOf(date.unNormYear), date.unNormMonth,
098: date.unNormDay, date.unNormHour,
099: date.unNormMinute, (int) date.unNormSecond,
100: date.unNormSecond != 0 ? new BigDecimal(
101: date.unNormSecond
102: - ((int) date.unNormSecond))
103: : null, date.timezoneHr * 60
104: + date.timezoneMin);
105: }
106: }
|