1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.impl.dv.xs;
23 
24 import javax.xml.datatype.DatatypeConstants;
25 import javax.xml.datatype.XMLGregorianCalendar;
26 
27 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
28 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
29 
30 /**
31  * Validator for <time> datatype (W3C Schema Datatypes)
32  *
33  * @xerces.internal
34  *
35  * @author Elena Litani
36  * @author Gopal Sharma, SUN Microsystem Inc.
37  *
38  */
39 public class TimeDV extends AbstractDateTimeDV {
40 
41     /**
42      * Convert a string to a compiled form
43      *
44      * @param  content The lexical representation of time
45      * @return a valid and normalized time object
46      */
getActualValue(String content, ValidationContext context)47     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{
48         try{
49             return parse(content);
50         } catch(Exception ex){
51             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "time"});
52         }
53     }
54 
55     /**
56      * Parses, validates and computes normalized version of time object
57      *
58      * @param str    The lexical representation of time object hh:mm:ss.sss
59      *               with possible time zone Z or (-),(+)hh:mm
60      *               Pattern: "(\\d\\d):(\\d\\d):(\\d\\d)(\\.(\\d)*)?(Z|(([-+])(\\d\\d)(:(\\d\\d))?))?")
61      * @return normalized time representation
62      * @exception SchemaDateTimeException Invalid lexical representation
63      */
parse(String str)64     protected DateTimeData parse(String str) throws SchemaDateTimeException{
65         DateTimeData date = new DateTimeData(str, this);
66         int len = str.length();
67 
68         // time
69         // initialize to default values
70         date.year=YEAR;
71         date.month=MONTH;
72         date.day=15;
73         getTime(str, 0, len, date);
74 
75         //validate and normalize
76 
77         validateDateTime(date);
78 
79         //save unnormalized values
80         saveUnnormalized(date);
81 
82         if ( date.utc!=0 && date.utc != 'Z') {
83             normalize(date);
84         }
85         date.position = 2;
86         return date;
87     }
88 
89     /**
90      * Converts time object representation to String
91      *
92      * @param date   time object
93      * @return lexical representation of time: hh:mm:ss.sss with an optional time zone sign
94      */
dateToString(DateTimeData date)95     protected String dateToString(DateTimeData date) {
96         StringBuffer message = new StringBuffer(16);
97         append(message, date.hour, 2);
98         message.append(':');
99         append(message, date.minute, 2);
100         message.append(':');
101         append(message, date.second);
102 
103         append(message, (char)date.utc, 0);
104         return message.toString();
105     }
106 
getXMLGregorianCalendar(DateTimeData date)107     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
108         return datatypeFactory.newXMLGregorianCalendar(null, DatatypeConstants.FIELD_UNDEFINED,
109                 DatatypeConstants.FIELD_UNDEFINED, date.unNormHour, date.unNormMinute,
110                 (int)date.unNormSecond, date.unNormSecond != 0 ? getFractionalSecondsAsBigDecimal(date) : null,
111                 date.hasTimeZone() ? (date.timezoneHr * 60 + date.timezoneMin) : DatatypeConstants.FIELD_UNDEFINED);
112     }
113 }
114