KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > TimeDV


1 /*
2  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.xs;
18
19 import java.math.BigDecimal JavaDoc;
20
21 import javax.xml.datatype.DatatypeConstants JavaDoc;
22 import javax.xml.datatype.XMLGregorianCalendar JavaDoc;
23
24 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
25 import org.apache.xerces.impl.dv.ValidationContext;
26
27 /**
28  * Validator for <time> datatype (W3C Schema Datatypes)
29  *
30  * @xerces.internal
31  *
32  * @author Elena Litani
33  * @author Gopal Sharma, SUN Microsystem Inc.
34  *
35  * @version $Id: TimeDV.java,v 1.19 2005/05/06 15:31:14 ankitp Exp $
36  */

37 public class TimeDV extends AbstractDateTimeDV {
38
39     /**
40      * Convert a string to a compiled form
41      *
42      * @param content The lexical representation of time
43      * @return a valid and normalized time object
44      */

45     public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException{
46         try{
47             return parse(content);
48         } catch(Exception JavaDoc ex){
49             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "time"});
50         }
51     }
52
53     /**
54      * Parses, validates and computes normalized version of time object
55      *
56      * @param str The lexical representation of time object hh:mm:ss.sss
57      * with possible time zone Z or (-),(+)hh:mm
58      * Pattern: "(\\d\\d):(\\d\\d):(\\d\\d)(\\.(\\d)*)?(Z|(([-+])(\\d\\d)(:(\\d\\d))?))?")
59      * @return normalized time representation
60      * @exception SchemaDateTimeException Invalid lexical representation
61      */

62     protected DateTimeData parse(String JavaDoc str) throws SchemaDateTimeException{
63         DateTimeData date = new DateTimeData(str, this);
64         int len = str.length();
65
66         // time
67
// initialize to default values
68
date.year=YEAR;
69         date.month=MONTH;
70         date.day=15;
71         getTime(str, 0, len, date);
72
73         //validate and normalize
74

75         validateDateTime(date);
76
77         //save unnormalized values
78
saveUnnormalized(date);
79         
80         if ( date.utc!=0 && date.utc != 'Z') {
81             normalize(date);
82         }
83         date.position = 2;
84         return date;
85     }
86
87     /**
88      * Converts time object representation to String
89      *
90      * @param date time object
91      * @return lexical representation of time: hh:mm:ss.sss with an optional time zone sign
92      */

93     protected String JavaDoc dateToString(DateTimeData date) {
94         StringBuffer JavaDoc message = new StringBuffer JavaDoc(16);
95         append(message, date.hour, 2);
96         message.append(':');
97         append(message, date.minute, 2);
98         message.append(':');
99         append(message, date.second);
100
101         append(message, (char)date.utc, 0);
102         return message.toString();
103     }
104     
105     protected XMLGregorianCalendar JavaDoc getXMLGregorianCalendar(DateTimeData date) {
106         return factory.newXMLGregorianCalendar(null
107                 , DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, date.unNormHour, date.unNormMinute, (int)date.unNormSecond, date.unNormSecond != 0?new BigDecimal JavaDoc(date.unNormSecond - ((int)date.unNormSecond)):null, date.timezoneHr * 60 + date.timezoneMin);
108     }
109 }
110
Popular Tags