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 <gYear> datatype (W3C Schema Datatypes)
32  *
33  * @xerces.internal
34  *
35  * @author Elena Litani
36  * @author Gopal Sharma, SUN Microsystem Inc.
37  *
38  */
39 
40 public class YearDV extends AbstractDateTimeDV {
41 
42     /**
43      * Convert a string to a compiled form
44      *
45      * @param  content The lexical representation of time
46      * @return a valid and normalized time object
47      */
getActualValue(String content, ValidationContext context)48     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{
49         try{
50             return parse(content);
51         } catch(Exception ex){
52             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "gYear"});
53         }
54     }
55 
56     /**
57      * Parses, validates and computes normalized version of gYear object
58      *
59      * @param str    The lexical representation of year object CCYY
60      *               with possible time zone Z or (-),(+)hh:mm
61      * @return normalized date 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         // check for preceding '-' sign
69         int start = 0;
70         if (str.charAt(0)=='-') {
71             start = 1;
72         }
73         int sign = findUTCSign(str, start, len);
74 
75         final int length = ((sign == -1) ? len : sign) - start;
76         if (length < 4) {
77             throw new RuntimeException("Year must have 'CCYY' format");
78         }
79         else if (length > 4 && str.charAt(start) == '0') {
80             throw new RuntimeException("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden");
81         }
82 
83         if (sign == -1) {
84             date.year=parseIntYear(str, len);
85         }
86         else {
87             date.year=parseIntYear(str, sign);
88             getTimeZone (str, date, sign, len);
89         }
90 
91         //initialize values
92         date.month=MONTH;
93         date.day=1;
94 
95         //validate and normalize
96         validateDateTime(date);
97 
98         //save unnormalized values
99         saveUnnormalized(date);
100 
101         if ( date.utc!=0 && date.utc!='Z' ) {
102             normalize(date);
103         }
104         date.position = 0;
105         return date;
106     }
107 
108     /**
109      * Converts year object representation to String
110      *
111      * @param date   year object
112      * @return lexical representation of month: CCYY with optional time zone sign
113      */
dateToString(DateTimeData date)114     protected String dateToString(DateTimeData date) {
115         StringBuffer message = new StringBuffer(5);
116         append(message, date.year, 4);
117         append(message, (char)date.utc, 0);
118         return message.toString();
119     }
120 
getXMLGregorianCalendar(DateTimeData date)121     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
122         return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, DatatypeConstants.FIELD_UNDEFINED,
123                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
124                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
125                 date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
126     }
127 }
128