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 <date> datatype (W3C Schema datatypes)
32  *
33  * @xerces.internal
34  *
35  * @author Elena Litani
36  * @author Gopal Sharma, SUN Microsystems Inc.
37  *
38  */
39 public class DateDV extends DateTimeDV {
40 
getActualValue(String content, ValidationContext context)41     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
42         try{
43             return parse(content);
44         } catch(Exception ex){
45             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "date"});
46         }
47     }
48 
49     /**
50      * Parses, validates and computes normalized version of dateTime object
51      *
52      * @param str    The lexical representation of dateTime object CCYY-MM-DD
53      *               with possible time zone Z or (-),(+)hh:mm
54      * @return normalized dateTime representation
55      * @exception SchemaDateTimeException Invalid lexical representation
56      */
parse(String str)57     protected DateTimeData parse(String str) throws SchemaDateTimeException {
58         DateTimeData date = new DateTimeData(str, this);
59         int len = str.length();
60 
61         int end = getDate(str, 0, len, date);
62         parseTimeZone (str, end, len, date);
63 
64         //validate and normalize
65         //REVISIT: do we need SchemaDateTimeException?
66         validateDateTime(date);
67 
68         //save unnormalized values
69         saveUnnormalized(date);
70 
71         if (date.utc!=0 && date.utc!='Z') {
72             normalize(date);
73         }
74         return date;
75     }
76 
dateToString(DateTimeData date)77     protected String dateToString(DateTimeData date) {
78         StringBuffer message = new StringBuffer(25);
79         append(message, date.year, 4);
80         message.append('-');
81         append(message, date.month, 2);
82         message.append('-');
83         append(message, date.day, 2);
84         append(message, (char)date.utc, 0);
85         return message.toString();
86     }
87 
getXMLGregorianCalendar(DateTimeData date)88     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
89         return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, date.unNormMonth,
90                 date.unNormDay, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
91                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
92                 date.hasTimeZone() ? (date.timezoneHr * 60 + date.timezoneMin) : DatatypeConstants.FIELD_UNDEFINED);
93     }
94 
95 }
96