KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.datatypes.xsd.impl;
11
12 import java.math.BigDecimal JavaDoc;
13 import java.math.BigInteger JavaDoc;
14
15 import com.hp.hpl.jena.datatypes.*;
16 import com.hp.hpl.jena.datatypes.xsd.*;
17 import com.hp.hpl.jena.graph.impl.LiteralLabel;
18 import com.hp.hpl.jena.shared.impl.JenaParameters;
19
20 /**
21  * Base implementation for all numeric datatypes derived from
22  * xsd:decimal. The only purpose of this place holder is
23  * to support the isValidLiteral tests across numeric types. Note
24  * that float and double are not included in this set.
25  *
26  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27  * @version $Revision: 1.13 $ on $Date: 2005/02/21 12:02:20 $
28  */

29 public class XSDBaseNumericType extends XSDDatatype {
30
31     /**
32      * Constructor.
33      * @param typeName the name of the XSD type to be instantiated, this is
34      * used to lookup a type definition from the Xerces schema factory.
35      */

36     public XSDBaseNumericType(String JavaDoc typeName) {
37         super(typeName);
38     }
39     
40     /**
41      * Constructor.
42      * @param typeName the name of the XSD type to be instantiated, this is
43      * used to lookup a type definition from the Xerces schema factory.
44      * @param javaClass the java class for which this xsd type is to be
45      * treated as the cannonical representation
46      */

47     public XSDBaseNumericType(String JavaDoc typeName, Class JavaDoc javaClass) {
48         super(typeName, javaClass);
49     }
50
51     
52     /**
53      * Test whether the given LiteralLabel is a valid instance
54      * of this datatype. This takes into accound typing information
55      * as well as lexical form - for example an xsd:string is
56      * never considered valid as an xsd:integer (even if it is
57      * lexically legal like "1").
58      */

59     public boolean isValidLiteral(LiteralLabel lit) {
60         if (isBaseTypeCompatible(lit)) {
61             String JavaDoc lex = lit.getLexicalForm();
62             if (JenaParameters.enableWhitespaceCheckingOfTypedLiterals) {
63                 if (lex.trim().equals(lex)) {
64                     return isValid(lit.getLexicalForm());
65                 } else {
66                     return false;
67                 }
68             } else {
69                 return isValid(lit.getLexicalForm());
70             }
71         } else {
72             return false;
73         }
74     }
75      
76     /**
77      * Test whether the given object is a legal value form
78      * of this datatype. Brute force implementation.
79      */

80     public boolean isValidValue(Object JavaDoc valueForm) {
81         if (valueForm instanceof Number JavaDoc) {
82             return isValid(valueForm.toString());
83         } else {
84             return false;
85         }
86     }
87    
88     /**
89      * Parse a lexical form of this datatype to a value
90      * @throws DatatypeFormatException if the lexical form is not legal
91      */

92     public Object JavaDoc parse(String JavaDoc lexicalForm) throws DatatypeFormatException {
93         checkWhitespace(lexicalForm);
94         return super.parse(lexicalForm);
95     }
96     
97     /**
98      * Check for whitespace violations.
99      * Turned off by default.
100      */

101     protected void checkWhitespace(String JavaDoc lexicalForm) {
102         if (JenaParameters.enableWhitespaceCheckingOfTypedLiterals) {
103             if ( ! lexicalForm.trim().equals(lexicalForm)) {
104                 throw new DatatypeFormatException(lexicalForm, this, "whitespace violation");
105             }
106         }
107     }
108     
109     /**
110      * Compares two instances of values of the given datatype.
111      * This ignores lang tags and just uses the java.lang.Number
112      * equality.
113      */

114     public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
115         Object JavaDoc o1 = value1.getValue();
116         Object JavaDoc o2 = value2.getValue();
117         if (!(o1 instanceof Number JavaDoc) || !(o2 instanceof Number JavaDoc)) {
118             return false;
119         }
120         if (o1 instanceof Float JavaDoc || o1 instanceof Double JavaDoc) {
121             return (((Number JavaDoc)o1).doubleValue() == ((Number JavaDoc)o2).doubleValue());
122         } else if (o1 instanceof BigInteger JavaDoc || o1 instanceof BigDecimal JavaDoc) {
123             return o1.equals(o2);
124         } else {
125             return (((Number JavaDoc)o1).longValue() == ((Number JavaDoc)o2).longValue());
126         }
127     }
128 }
129
130 /*
131     (c) Copyright 2002, 2003, 2004 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