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