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