KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > datatypes > xsd > impl > XSDDurationType


1 /******************************************************************
2  * File: XSDDurationType.java
3  * Created by: Dave Reynolds
4  * Created on: 16-Dec-02
5  *
6  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: XSDDurationType.java,v 1.8 2005/02/21 12:02:22 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.datatypes.xsd.impl;
11
12 import com.hp.hpl.jena.datatypes.*;
13 import com.hp.hpl.jena.datatypes.xsd.*;
14
15 /**
16  * The XSD duration type, the only job of this extra layer is to
17  * wrap the return value in a more convenient accessor type. We could
18  * avoid this proliferation of trivial types by use of reflection but
19  * since that causes allergic reactions in some we use brute force.
20  * <p>
21  * This class includees code derived from Xerces 2.6.0 Copyright (c) 1999-2002 The Apache Software Foundation. All rights
22  * reserved.
23  * </p>
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:02:22 $
27  */

28 public class XSDDurationType extends XSDAbstractDateTimeType {
29     
30     /**
31      * Constructor
32      */

33     public XSDDurationType() {
34         super("duration");
35         javaClass = XSDDuration.class;
36     }
37         
38     /**
39     * Parse a validated date. This is invoked from
40     * XSDDatatype.convertValidatedDataValue rather then from a local
41     * parse method to make the implementation of XSDGenericType easier.
42     */

43    public Object JavaDoc parseValidated(String JavaDoc str) {
44        int len = str.length();
45        int[] date=new int[TOTAL_SIZE];
46
47        int start = 0;
48        char c=str.charAt(start++);
49        if ( c!='P' && c!='-' ) {
50            throw new DatatypeFormatException("Internal error: validated duration failed to parse(1)");
51        }
52        else {
53            date[utc]=(c=='-')?'-':0;
54            if ( c=='-' && str.charAt(start++)!='P' ) {
55                throw new DatatypeFormatException("Internal error: validated duration failed to parse(2)");
56            }
57        }
58
59        int negate = 1;
60        //negative duration
61
if ( date[utc]=='-' ) {
62            negate = -1;
63
64        }
65
66        int endDate = indexOf (str, start, len, 'T');
67        if ( endDate == -1 ) {
68            endDate = len;
69        }
70        //find 'Y'
71
int end = indexOf (str, start, endDate, 'Y');
72        if ( end!=-1 ) {
73            //scan year
74
date[CY]=negate * parseInt(str,start,end);
75            start = end+1;
76        }
77
78        end = indexOf (str, start, endDate, 'M');
79        if ( end!=-1 ) {
80            //scan month
81
date[M]=negate * parseInt(str,start,end);
82            start = end+1;
83        }
84
85        end = indexOf (str, start, endDate, 'D');
86        if ( end!=-1 ) {
87            //scan day
88
date[D]=negate * parseInt(str,start,end);
89            start = end+1;
90        }
91
92        if ( len == endDate && start!=len ) {
93            throw new DatatypeFormatException("Internal error: validated duration failed to parse(3)");
94        }
95        if ( len !=endDate ) {
96
97            end = indexOf (str, ++start, len, 'H');
98            if ( end!=-1 ) {
99                //scan hours
100
date[h]=negate * parseInt(str,start,end);
101                start=end+1;
102            }
103
104            end = indexOf (str, start, len, 'M');
105            if ( end!=-1 ) {
106                //scan min
107
date[m]=negate * parseInt(str,start,end);
108                start=end+1;
109            }
110
111            end = indexOf (str, start, len, 'S');
112            if ( end!=-1 ) {
113                //scan seconds
114
int mlsec = indexOf (str, start, end, '.');
115                if ( mlsec >0 ) {
116                    date[s] = negate * parseInt (str, start, mlsec);
117                    date[ms] = negate * parseInt (str, mlsec+1, end);
118                }
119                else {
120                    date[s]=negate * parseInt(str, start,end);
121                }
122                start=end+1;
123            }
124        }
125        return new XSDDuration(date);
126    }
127     
128 }
129
130 /*
131     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
132     All rights reserved.
133
134     Redistribution and use in source and binary forms, with or without
135     modification, are permitted provided that the following conditions
136     are met:
137
138     1. Redistributions of source code must retain the above copyright
139        notice, this list of conditions and the following disclaimer.
140
141     2. Redistributions in binary form must reproduce the above copyright
142        notice, this list of conditions and the following disclaimer in the
143        documentation and/or other materials provided with the distribution.
144
145     3. The name of the author may not be used to endorse or promote products
146        derived from this software without specific prior written permission.
147
148     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
149     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
151     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
152     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
153     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
154     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
155     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
156     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
157     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
158 */

159
Popular Tags