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