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