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 <gMonthDay> datatype (W3C Schema Datatypes)
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Elena Litani
  36  * @author Gopal Sharma, SUN Microsystem Inc.
  37  *
  38  * @version $Id: MonthDayDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
  39  */
  40 
  41 public class MonthDayDV extends AbstractDateTimeDV {
  42 
  43     //size without time zone: --MM-DD
  44     private final static int MONTHDAY_SIZE = 7;
  45 
  46     /**
  47      * Convert a string to a compiled form
  48      *
  49      * @param  content The lexical representation of gMonthDay
  50      * @return a valid and normalized gMonthDay object
  51      */
  52     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  53         try{
  54             return parse(content);
  55         } catch(Exception ex){
  56             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "gMonthDay"});
  57         }
  58     }
  59 
  60     /**
  61      * Parses, validates and computes normalized version of gMonthDay object
  62      *
  63      * @param str    The lexical representation of gMonthDay object --MM-DD
  64      *               with possible time zone Z or (-),(+)hh:mm
  65      * @return normalized date representation
  66      * @exception SchemaDateTimeException Invalid lexical representation
  67      */
  68     protected DateTimeData parse(String str) throws SchemaDateTimeException{
  69         DateTimeData date = new DateTimeData(str, this);
  70         int len = str.length();
  71 
  72         //initialize
  73         date.year=YEAR;
  74 
  75         if (str.charAt(0)!='-' || str.charAt(1)!='-') {
  76             throw new SchemaDateTimeException("Invalid format for gMonthDay: "+str);
  77         }
  78         date.month=parseInt(str, 2, 4);
  79         int start=4;
  80 
  81         if (str.charAt(start++)!='-') {
  82             throw new SchemaDateTimeException("Invalid format for gMonthDay: " + str);
  83         }
  84 
  85         date.day=parseInt(str, start, start+2);
  86 
  87         if ( MONTHDAY_SIZE<len ) {
  88             if (!isNextCharUTCSign(str, MONTHDAY_SIZE, len)) {
  89                 throw new SchemaDateTimeException ("Error in month parsing:" +str);
  90             }
  91             else {
  92                 getTimeZone(str, date, MONTHDAY_SIZE, len);
  93             }
  94         }
  95         //validate and normalize
  96 
  97         validateDateTime(date);
  98 
  99         //save unnormalized values
 100         saveUnnormalized(date);
 101 
 102         if ( date.utc!=0 && date.utc!='Z' ) {
 103             normalize(date);
 104         }
 105         date.position = 1;
 106         return date;
 107     }
 108 
 109     /**
 110      * Converts gMonthDay object representation to String
 111      *
 112      * @param date   gmonthDay object
 113      * @return lexical representation of month: --MM-DD with an optional time zone sign
 114      */
 115     protected String dateToString(DateTimeData date) {
 116         StringBuffer message = new StringBuffer(8);
 117         message.append('-');
 118         message.append('-');
 119         append(message, date.month, 2);
 120         message.append('-');
 121         append(message, date.day, 2);
 122         append(message, (char)date.utc, 0);
 123         return message.toString();
 124     }
 125 
 126     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
 127         return datatypeFactory.newXMLGregorianCalendar(DatatypeConstants.FIELD_UNDEFINED, date.unNormMonth, date.unNormDay,
 128                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
 129                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
 130                 date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
 131     }
 132 }