View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2004,2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package com.sun.org.apache.xerces.internal.impl.dv.xs;
21  
22  import java.math.BigDecimal;
23  import java.math.BigInteger;
24  
25  import javax.xml.datatype.DatatypeConstants;
26  import javax.xml.datatype.Duration;
27  
28  import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
29  import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
30  
31  /**
32   * Used to validate the <dayTimeDuration> type
33   *
34   * @xerces.internal
35   *
36   * @author Ankit Pasricha, IBM
37   *
38   * @version $Id: DayTimeDurationDV.java,v 1.6 2010-11-01 04:39:46 joehw Exp $
39   */
40  class DayTimeDurationDV extends DurationDV {
41  
42      public Object getActualValue(String content, ValidationContext context)
43          throws InvalidDatatypeValueException {
44          try {
45              return parse(content, DurationDV.DAYTIMEDURATION_TYPE);
46          }
47          catch (Exception ex) {
48              throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "dayTimeDuration"});
49          }
50      }
51  
52      protected Duration getDuration(DateTimeData date) {
53          int sign = 1;
54          if (date.day<0 || date.hour<0 || date.minute<0 || date.second<0) {
55              sign = -1;
56          }
57          return datatypeFactory.newDuration(sign == 1, null, null,
58                  date.day != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.day):null,
59                  date.hour != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.hour):null,
60                  date.minute != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.minute):null,
61                  date.second != DatatypeConstants.FIELD_UNDEFINED?new BigDecimal(String.valueOf(sign*date.second)):null);
62      }
63  }