01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.xerces.impl.dv.xs;
18:
19: import java.math.BigDecimal;
20: import java.math.BigInteger;
21:
22: import javax.xml.datatype.DatatypeConstants;
23: import javax.xml.datatype.Duration;
24:
25: import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
26: import org.apache.xerces.impl.dv.ValidationContext;
27:
28: /**
29: * Used to validate the <dayTimeDuration> type
30: *
31: * @xerces.internal
32: *
33: * @author Ankit Pasricha, IBM
34: *
35: * @version $Id: DayTimeDurationDV.java 446745 2006-09-15 21:43:58Z mrglavas $
36: */
37: class DayTimeDurationDV extends DurationDV {
38:
39: public Object getActualValue(String content,
40: ValidationContext context)
41: throws InvalidDatatypeValueException {
42: try {
43: return parse(content, DurationDV.DAYTIMEDURATION_TYPE);
44: } catch (Exception ex) {
45: throw new InvalidDatatypeValueException(
46: "cvc-datatype-valid.1.2.1", new Object[] { content,
47: "dayTimeDuration" });
48: }
49: }
50:
51: protected Duration getDuration(DateTimeData date) {
52: int sign = 1;
53: if (date.day < 0 || date.hour < 0 || date.minute < 0
54: || date.second < 0) {
55: sign = -1;
56: }
57: return factory
58: .newDuration(
59: sign == 1,
60: null,
61: null,
62: date.day != DatatypeConstants.FIELD_UNDEFINED ? BigInteger
63: .valueOf(sign * date.day)
64: : null,
65: date.hour != DatatypeConstants.FIELD_UNDEFINED ? BigInteger
66: .valueOf(sign * date.hour)
67: : null,
68: date.minute != DatatypeConstants.FIELD_UNDEFINED ? BigInteger
69: .valueOf(sign * date.minute)
70: : null,
71: date.second != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(
72: String.valueOf(sign * date.second))
73: : null);
74: }
75: }
|