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 java.math.BigInteger;
  25 
  26 import javax.xml.datatype.DatatypeConstants;
  27 import javax.xml.datatype.Duration;
  28 
  29 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  30 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  31 
  32 /**
  33  * Used to validate the <yearMonthDuration> type
  34  *
  35  * @xerces.internal
  36  *
  37  * @author Ankit Pasricha, IBM
  38  *
  39  * @version $Id: YearMonthDurationDV.java,v 1.6 2010-11-01 04:39:47 joehw Exp $
  40  */
  41 class YearMonthDurationDV extends DurationDV {
  42 
  43     public Object getActualValue(String content, ValidationContext context)
  44         throws InvalidDatatypeValueException {
  45         try {
  46             return parse(content, DurationDV.YEARMONTHDURATION_TYPE);
  47         }
  48         catch (Exception ex) {
  49             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "yearMonthDuration"});
  50         }
  51     }
  52 
  53     protected Duration getDuration(DateTimeData date) {
  54         int sign = 1;
  55         if ( date.year<0 || date.month<0) {
  56             sign = -1;
  57         }
  58         return datatypeFactory.newDuration(sign == 1,
  59                 date.year != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.year):null,
  60                 date.month != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.month):null,
  61                 null,
  62                 null,
  63                 null,
  64                 null);
  65     }
  66 }