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