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 <gYear> datatype (W3C Schema Datatypes) 32 * 33 * @xerces.internal 34 * 35 * @author Elena Litani 36 * @author Gopal Sharma, SUN Microsystem Inc. 37 * 38 * @version $Id: YearDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $ 39 */ 40 41 public class YearDV extends AbstractDateTimeDV { 42 43 /** 44 * Convert a string to a compiled form 45 * 46 * @param content The lexical representation of time 47 * @return a valid and normalized time 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, "gYear"}); 54 } 55 } 56 57 /** 58 * Parses, validates and computes normalized version of gYear object 59 * 60 * @param str The lexical representation of year object CCYY 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 // check for preceding '-' sign 70 int start = 0; 71 if (str.charAt(0)=='-') { 72 start = 1; 73 } 74 int sign = findUTCSign(str, start, len); 75 76 final int length = ((sign == -1) ? len : sign) - start; 77 if (length < 4) { 78 throw new RuntimeException("Year must have 'CCYY' format"); 79 } 80 else if (length > 4 && str.charAt(start) == '0') { 81 throw new RuntimeException("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden"); 82 } 83 84 if (sign == -1) { 85 date.year=parseIntYear(str, len); 86 } 87 else { 88 date.year=parseIntYear(str, sign); 89 getTimeZone (str, date, sign, len); 90 } 91 92 //initialize values 93 date.month=MONTH; 94 date.day=1; 95 96 //validate and normalize 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 = 0; 106 return date; 107 } 108 109 /** 110 * Converts year object representation to String 111 * 112 * @param date year object 113 * @return lexical representation of month: CCYY with optional time zone sign 114 */ 115 protected String dateToString(DateTimeData date) { 116 StringBuffer message = new StringBuffer(5); 117 append(message, date.year, 4); 118 append(message, (char)date.utc, 0); 119 return message.toString(); 120 } 121 122 protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) { 123 return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, DatatypeConstants.FIELD_UNDEFINED, 124 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, 125 DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, 126 date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED); 127 } 128 }