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 <date> datatype (W3C Schema datatypes)
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Elena Litani
  36  * @author Gopal Sharma, SUN Microsystems Inc.
  37  *
  38  * @version $Id: DateDV.java,v 1.7 2010-11-01 04:39:46 joehw Exp $
  39  */
  40 public class DateDV extends DateTimeDV {
  41 
  42     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  43         try{
  44             return parse(content);
  45         } catch(Exception ex){
  46             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "date"});
  47         }
  48     }
  49 
  50     /**
  51      * Parses, validates and computes normalized version of dateTime object
  52      *
  53      * @param str    The lexical representation of dateTime object CCYY-MM-DD
  54      *               with possible time zone Z or (-),(+)hh:mm
  55      * @return normalized dateTime representation
  56      * @exception SchemaDateTimeException Invalid lexical representation
  57      */
  58     protected DateTimeData parse(String str) throws SchemaDateTimeException {
  59         DateTimeData date = new DateTimeData(str, this);
  60         int len = str.length();
  61 
  62         int end = getDate(str, 0, len, date);
  63         parseTimeZone (str, end, len, date);
  64 
  65         //validate and normalize
  66         //REVISIT: do we need SchemaDateTimeException?
  67         validateDateTime(date);
  68 
  69         //save unnormalized values
  70         saveUnnormalized(date);
  71 
  72         if (date.utc!=0 && date.utc!='Z') {
  73             normalize(date);
  74         }
  75         return date;
  76     }
  77 
  78     protected String dateToString(DateTimeData date) {
  79         StringBuffer message = new StringBuffer(25);
  80         append(message, date.year, 4);
  81         message.append('-');
  82         append(message, date.month, 2);
  83         message.append('-');
  84         append(message, date.day, 2);
  85         append(message, (char)date.utc, 0);
  86         return message.toString();
  87     }
  88 
  89     protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
  90         return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, date.unNormMonth,
  91                 date.unNormDay, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
  92                 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
  93                 date.hasTimeZone() ? (date.timezoneHr * 60 + date.timezoneMin) : DatatypeConstants.FIELD_UNDEFINED);
  94     }
  95 
  96 }