286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.dv.xs;
286N/A
286N/Aimport javax.xml.datatype.DatatypeConstants;
286N/Aimport javax.xml.datatype.XMLGregorianCalendar;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
286N/Aimport com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
286N/A
286N/A/**
286N/A * Validator for <gYear> datatype (W3C Schema Datatypes)
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Elena Litani
286N/A * @author Gopal Sharma, SUN Microsystem Inc.
286N/A *
286N/A * @version $Id: YearDV.java,v 1.7 2010-11-01 04:39:47 joehw Exp $
286N/A */
286N/A
286N/Apublic class YearDV extends AbstractDateTimeDV {
286N/A
286N/A /**
286N/A * Convert a string to a compiled form
286N/A *
286N/A * @param content The lexical representation of time
286N/A * @return a valid and normalized time object
286N/A */
286N/A public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{
286N/A try{
286N/A return parse(content);
286N/A } catch(Exception ex){
286N/A throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "gYear"});
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Parses, validates and computes normalized version of gYear object
286N/A *
286N/A * @param str The lexical representation of year object CCYY
286N/A * with possible time zone Z or (-),(+)hh:mm
286N/A * @return normalized date representation
286N/A * @exception SchemaDateTimeException Invalid lexical representation
286N/A */
286N/A protected DateTimeData parse(String str) throws SchemaDateTimeException{
286N/A DateTimeData date = new DateTimeData(str, this);
286N/A int len = str.length();
286N/A
286N/A // check for preceding '-' sign
286N/A int start = 0;
286N/A if (str.charAt(0)=='-') {
286N/A start = 1;
286N/A }
286N/A int sign = findUTCSign(str, start, len);
286N/A
286N/A final int length = ((sign == -1) ? len : sign) - start;
286N/A if (length < 4) {
286N/A throw new RuntimeException("Year must have 'CCYY' format");
286N/A }
286N/A else if (length > 4 && str.charAt(start) == '0') {
286N/A throw new RuntimeException("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden");
286N/A }
286N/A
286N/A if (sign == -1) {
286N/A date.year=parseIntYear(str, len);
286N/A }
286N/A else {
286N/A date.year=parseIntYear(str, sign);
286N/A getTimeZone (str, date, sign, len);
286N/A }
286N/A
286N/A //initialize values
286N/A date.month=MONTH;
286N/A date.day=1;
286N/A
286N/A //validate and normalize
286N/A validateDateTime(date);
286N/A
286N/A //save unnormalized values
286N/A saveUnnormalized(date);
286N/A
286N/A if ( date.utc!=0 && date.utc!='Z' ) {
286N/A normalize(date);
286N/A }
286N/A date.position = 0;
286N/A return date;
286N/A }
286N/A
286N/A /**
286N/A * Converts year object representation to String
286N/A *
286N/A * @param date year object
286N/A * @return lexical representation of month: CCYY with optional time zone sign
286N/A */
286N/A protected String dateToString(DateTimeData date) {
286N/A StringBuffer message = new StringBuffer(5);
286N/A append(message, date.year, 4);
286N/A append(message, (char)date.utc, 0);
286N/A return message.toString();
286N/A }
286N/A
286N/A protected XMLGregorianCalendar getXMLGregorianCalendar(DateTimeData date) {
286N/A return datatypeFactory.newXMLGregorianCalendar(date.unNormYear, DatatypeConstants.FIELD_UNDEFINED,
286N/A DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
286N/A DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED,
286N/A date.hasTimeZone() ? date.timezoneHr * 60 + date.timezoneMin : DatatypeConstants.FIELD_UNDEFINED);
286N/A }
286N/A}