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 <gMonth> datatype (W3C Schema Datatypes)
  31  *
  32  * @xerces.internal
  33  *
  34  * @author Elena Litani
  35  * @author Gopal Sharma, SUN Microsystem Inc.
  36  *
  37  * @version $Id: MonthDV.java,v 1.8 2010-11-01 04:39:47 joehw Exp $
  38  */
  39 
  40 public class MonthDV extends AbstractDateTimeDV {
  41 
  42     /**
  43      * Convert a string to a compiled form
  44      *
  45      * @param  content The lexical representation of gMonth
  46      * @return a valid and normalized gMonth object
  47      */
  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, "gMonth"});
  53         }
  54     }
  55 
  56     /**
  57      * Parses, validates and computes normalized version of gMonth object
  58      *
  59      * @param str    The lexical representation of gMonth object --MM
  60      *               with possible time zone Z or (-),(+)hh:mm
  61      * @return normalized date representation
  62      * @exception SchemaDateTimeException Invalid lexical representation
  63      */
  64     protected DateTimeData parse(String str) throws SchemaDateTimeException{
  65         DateTimeData date = new DateTimeData(str, this);
  66         int len = str.length();
  67 
  68         //set constants
  69         date.year=YEAR;
  70         date.day=DAY;
  71         if (str.charAt(0)!='-' || str.charAt(1)!='-') {
  72             throw new SchemaDateTimeException("Invalid format for gMonth: "+str);
  73         }
  74         int stop = 4;
  75         date.month=parseInt(str,2,stop);
  76 
  77         // REVISIT: allow both --MM and --MM-- now.
  78         // need to remove the following 4 lines to disallow --MM--
  79         // when the errata is offically in the rec.
  80         if (str.length() >= stop+2 &&
  81             str.charAt(stop) == '-' && str.charAt(stop+1) == '-') {
  82             stop += 2;
  83         }
  84         if (stop < len) {
  85             if (!isNextCharUTCSign(str, stop, len)) {
  86                 throw new SchemaDateTimeException ("Error in month parsing: "+str);
  87             }
  88             else {
  89                 getTimeZone(str, date, stop, len);
  90             }
  91         }
  92         //validate and normalize
  93         validateDateTime(date);
  94 
  95         //save unnormalized values
  96         saveUnnormalized(date);
  97 
  98         if ( date.utc!=0 && date.utc!='Z' ) {
  99             normalize(date);
 100         }
 101         date.position = 1;
 102         return date;
 103     }
 104 
 105     /**
 106      * Overwrite compare algorithm to optimize month comparison
 107      *
 108      * REVISIT: this one is lack of the third parameter: boolean strict, so it
 109      *          doesn't override the method in the base. But maybe this method
 110      *          is not correctly implemented, and I did encounter errors when
 111      *          trying to add the extra parameter. I'm leaving it as is. -SG
 112      *
 113      * @param date1
 114      * @param date2
 115      * @return less, greater, equal, indeterminate
 116      */
 117     /*protected  short compareDates(DateTimeData date1, DateTimeData date2) {
 118 
 119         if ( date1.utc==date2.utc ) {
 120             return (short)((date1.month>=date2.month)?(date1.month>date2.month)?1:0:-1);
 121         }
 122 
 123         if ( date1.utc=='Z' || date2.utc=='Z' ) {
 124 
 125             if ( date1.month==date2.month ) {
 126                 //--05--Z and --05--
 127                 return INDETERMINATE;
 128             }
 129             if ( (date1.month+1 == date2.month || date1.month-1 == date2.month) ) {
 130                 //--05--Z and (--04-- or --05--)
 131                 //REVISIT: should this case be less than or equal?
 132                 //         maxExclusive should fail but what about maxInclusive
 133                 //
 134                 return INDETERMINATE;
 135             }
 136         }
 137 
 138         if ( date1.month<date2.month ) {
 139             return -1;
 140         }
 141         else {
 142             return 1;
 143         }
 144 
 145     }*/
 146 
 147     /**
 148      * Converts month object representation to String
 149      *
 150      * @param date   month object
 151      * @return lexical representation of month: --MM with an optional time zone sign
 152      */
 153     protected String dateToString(DateTimeData date) {
 154         StringBuffer message = new StringBuffer(5);
 155         message.append('-');
 156         message.append('-');
 157         append(message, date.month, 2);
 158         append(message, (char)date.utc, 0);
 159         return message.toString();
 160     }
 161 
 162     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
 163         return datatypeFactory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth,
 164                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
 165                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
 166                 date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
 167     }
 168 }