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