KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: XSDFloat.java
3  * Created by: Dave Reynolds
4  * Created on: 03-Dec-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * All rights reserved.
8  * [See end of file]
9  * $Id: XSDFloat.java,v 1.4 2005/02/21 12:02:23 andy_seaborne Exp $
10  *****************************************************************/

11 package com.hp.hpl.jena.datatypes.xsd.impl;
12
13 import com.hp.hpl.jena.datatypes.DatatypeFormatException;
14 import com.hp.hpl.jena.datatypes.xsd.*;
15 import com.hp.hpl.jena.graph.impl.LiteralLabel;
16 import com.hp.hpl.jena.shared.impl.JenaParameters;
17
18 /**
19  * Datatype representation for xsd:float. Can't just use XSDBaseNumericType
20  * because float, double and decimal are all disjoint in XSD. Can use plain
21  * XSDDatatype because the equality function needs overriding.
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:02:23 $
25  */

26 public class XSDFloat extends XSDDatatype {
27     /**
28       * Constructor.
29       * @param typeName the name of the XSD type to be instantiated, this is
30       * used to lookup a type definition from the Xerces schema factory.
31       */

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

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

59      /**
60       * Test whether the given object is a legal value form
61       * of this datatype. Brute force implementation.
62       */

63      public boolean isValidValue(Object JavaDoc valueForm) {
64          return (valueForm instanceof Float JavaDoc);
65      }
66    
67      /**
68       * Parse a lexical form of this datatype to a value
69       * @throws DatatypeFormatException if the lexical form is not legal
70       */

71      public Object JavaDoc parse(String JavaDoc lexicalForm) throws DatatypeFormatException {
72          checkWhitespace(lexicalForm);
73          return super.parse(lexicalForm);
74      }
75
76     /**
77      * Parse a validated lexical form. Subclasses which use the default
78      * parse implementation and are not convered by the explicit convertValidatedData
79      * cases should override this.
80      */

81     public Object JavaDoc parseValidated(String JavaDoc lex) {
82         if (lex.equals("INF")) {
83             return new Float JavaDoc(Float.NEGATIVE_INFINITY);
84         } else if (lex.equals("-INF")) {
85             return new Float JavaDoc(Float.POSITIVE_INFINITY);
86         } else if (lex.equals("NaN")) {
87             return new Float JavaDoc(Float.NaN);
88         } else {
89             return Float.valueOf(lex);
90         }
91     }
92      
93     /**
94      * Check for whitespace violations.
95      * Turned off by default.
96      */

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

110      public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
111          return value1.getDatatype() == value2.getDatatype()
112               && value1.getValue().equals(value2.getValue());
113      }
114 }
115
116
117 /*
118     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
119     All rights reserved.
120
121     Redistribution and use in source and binary forms, with or without
122     modification, are permitted provided that the following conditions
123     are met:
124
125     1. Redistributions of source code must retain the above copyright
126        notice, this list of conditions and the following disclaimer.
127
128     2. Redistributions in binary form must reproduce the above copyright
129        notice, this list of conditions and the following disclaimer in the
130        documentation and/or other materials provided with the distribution.
131
132     3. The name of the author may not be used to endorse or promote products
133        derived from this software without specific prior written permission.
134
135     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
136     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
137     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
138     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
139     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
140     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
144     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145 */

146
Popular Tags