001: /******************************************************************
002: * File: XSDDurationType.java
003: * Created by: Dave Reynolds
004: * Created on: 16-Dec-02
005: *
006: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: XSDDurationType.java,v 1.12 2008/01/02 12:04:04 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.datatypes.xsd.impl;
010:
011: import com.hp.hpl.jena.datatypes.*;
012: import com.hp.hpl.jena.datatypes.xsd.*;
013:
014: /**
015: * The XSD duration type, the only job of this extra layer is to
016: * wrap the return value in a more convenient accessor type. We could
017: * avoid this proliferation of trivial types by use of reflection but
018: * since that causes allergic reactions in some we use brute force.
019: * <p>
020: * This class includees code derived from Xerces 2.6.0 Copyright (c) 1999-2002 The Apache Software Foundation. All rights
021: * reserved.
022: * </p>
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:04:04 $
026: */
027: public class XSDDurationType extends XSDAbstractDateTimeType {
028:
029: /**
030: * Constructor
031: */
032: public XSDDurationType() {
033: super ("duration");
034: javaClass = XSDDuration.class;
035: }
036:
037: /**
038: * Parse a validated date. This is invoked from
039: * XSDDatatype.convertValidatedDataValue rather then from a local
040: * parse method to make the implementation of XSDGenericType easier.
041: */
042: public Object parseValidated(String str) {
043: int len = str.length();
044: int[] date = new int[TOTAL_SIZE];
045:
046: int start = 0;
047: char c = str.charAt(start++);
048: if (c != 'P' && c != '-') {
049: throw new DatatypeFormatException(
050: "Internal error: validated duration failed to parse(1)");
051: } else {
052: date[utc] = (c == '-') ? '-' : 0;
053: if (c == '-' && str.charAt(start++) != 'P') {
054: throw new DatatypeFormatException(
055: "Internal error: validated duration failed to parse(2)");
056: }
057: }
058:
059: int negate = 1;
060: //negative duration
061: if (date[utc] == '-') {
062: negate = -1;
063:
064: }
065:
066: int endDate = indexOf(str, start, len, 'T');
067: if (endDate == -1) {
068: endDate = len;
069: }
070: //find 'Y'
071: int end = indexOf(str, start, endDate, 'Y');
072: if (end != -1) {
073: //scan year
074: date[CY] = negate * parseInt(str, start, end);
075: start = end + 1;
076: }
077:
078: end = indexOf(str, start, endDate, 'M');
079: if (end != -1) {
080: //scan month
081: date[M] = negate * parseInt(str, start, end);
082: start = end + 1;
083: }
084:
085: end = indexOf(str, start, endDate, 'D');
086: if (end != -1) {
087: //scan day
088: date[D] = negate * parseInt(str, start, end);
089: start = end + 1;
090: }
091:
092: if (len == endDate && start != len) {
093: throw new DatatypeFormatException(
094: "Internal error: validated duration failed to parse(3)");
095: }
096: if (len != endDate) {
097:
098: end = indexOf(str, ++start, len, 'H');
099: if (end != -1) {
100: //scan hours
101: date[h] = negate * parseInt(str, start, end);
102: start = end + 1;
103: }
104:
105: end = indexOf(str, start, len, 'M');
106: if (end != -1) {
107: //scan min
108: date[m] = negate * parseInt(str, start, end);
109: start = end + 1;
110: }
111:
112: end = indexOf(str, start, len, 'S');
113: if (end != -1) {
114: //scan seconds
115: int mlsec = indexOf(str, start, end, '.');
116: if (mlsec > 0) {
117: date[s] = negate * parseInt(str, start, mlsec);
118: int msEnd = end;
119: while (str.charAt(msEnd - 1) == '0')
120: msEnd--;
121: date[ms] = negate * parseInt(str, mlsec + 1, msEnd);
122: date[msscale] = msEnd - mlsec - 1;
123: } else {
124: date[s] = negate * parseInt(str, start, end);
125: }
126: start = end + 1;
127: }
128: }
129: return new XSDDuration(date);
130: }
131:
132: }
133:
134: /*
135: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
136: All rights reserved.
137:
138: Redistribution and use in source and binary forms, with or without
139: modification, are permitted provided that the following conditions
140: are met:
141:
142: 1. Redistributions of source code must retain the above copyright
143: notice, this list of conditions and the following disclaimer.
144:
145: 2. Redistributions in binary form must reproduce the above copyright
146: notice, this list of conditions and the following disclaimer in the
147: documentation and/or other materials provided with the distribution.
148:
149: 3. The name of the author may not be used to endorse or promote products
150: derived from this software without specific prior written permission.
151:
152: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162: */
|