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 <gYearMonth> datatype (W3C Schema Datatypes)
31   *
32   * @xerces.internal
33   *
34   * @author Elena Litani
35   * @author Gopal Sharma, SUN Microsystem Inc.
36   *
37   * @version $Id: YearMonthDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
38   */
39  public class YearMonthDV extends AbstractDateTimeDV{
40  
41      /**
42       * Convert a string to a compiled form
43       *
44       * @param  content The lexical representation of gYearMonth
45       * @return a valid and normalized gYearMonth object
46       */
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, "gYearMonth"});
52          }
53      }
54  
55      /**
56       * Parses, validates and computes normalized version of gYearMonth object
57       *
58       * @param str    The lexical representation of gYearMonth object CCYY-MM
59       *               with possible time zone Z or (-),(+)hh:mm
60       * @return normalized date representation
61       * @exception SchemaDateTimeException Invalid lexical representation
62       */
63      protected DateTimeData parse(String str) throws SchemaDateTimeException{
64          DateTimeData date = new DateTimeData(str, this);
65          int len = str.length();
66  
67          // get date
68          int end = getYearMonth(str, 0, len, date);
69          date.day = DAY;
70          parseTimeZone (str, end, len, date);
71  
72          //validate and normalize
73  
74          validateDateTime(date);
75  
76          //save unnormalized values
77          saveUnnormalized(date);
78  
79          if ( date.utc!=0 && date.utc!='Z' ) {
80              normalize(date);
81          }
82          date.position = 0;
83          return date;
84      }
85  
86      protected String dateToString(DateTimeData date) {
87          StringBuffer message = new StringBuffer(25);
88          append(message, date.year, 4);
89          message.append('-');
90          append(message, date.month, 2);
91          append(message, (char)date.utc, 0);
92          return message.toString();
93      }
94  
95      protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
96          return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, date.unNormMonth, DatatypeConstants.FIELD_UNDEFINED,
97                  DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
98                  DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
99                  date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
100     }
101 }