001: /******************************************************************
002: * File: XSDFloat.java
003: * Created by: Dave Reynolds
004: * Created on: 03-Dec-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * All rights reserved.
008: * [See end of file]
009: * $Id: XSDFloat.java,v 1.7 2008/01/02 12:04:04 andy_seaborne Exp $
010: *****************************************************************/package com.hp.hpl.jena.datatypes.xsd.impl;
011:
012: import com.hp.hpl.jena.datatypes.DatatypeFormatException;
013: import com.hp.hpl.jena.datatypes.xsd.*;
014: import com.hp.hpl.jena.graph.impl.LiteralLabel;
015: import com.hp.hpl.jena.shared.impl.JenaParameters;
016:
017: /**
018: * Datatype representation for xsd:float. Can't just use XSDBaseNumericType
019: * because float, double and decimal are all disjoint in XSD. Can use plain
020: * XSDDatatype because the equality function needs overriding.
021: *
022: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
023: * @version $Revision: 1.7 $ on $Date: 2008/01/02 12:04:04 $
024: */
025: public class XSDFloat extends XSDDatatype {
026: /**
027: * Constructor.
028: * @param typeName the name of the XSD type to be instantiated, this is
029: * used to lookup a type definition from the Xerces schema factory.
030: */
031: public XSDFloat(String typeName) {
032: super (typeName);
033: }
034:
035: /**
036: * Constructor.
037: * @param typeName the name of the XSD type to be instantiated, this is
038: * used to lookup a type definition from the Xerces schema factory.
039: * @param javaClass the java class for which this xsd type is to be
040: * treated as the cannonical representation
041: */
042: public XSDFloat(String typeName, Class javaClass) {
043: super (typeName, javaClass);
044: }
045:
046: // /**
047: // * Test whether the given LiteralLabel is a valid instance
048: // * of this datatype. This takes into accound typing information
049: // * as well as lexical form - for example an xsd:string is
050: // * never considered valid as an xsd:integer (even if it is
051: // * lexically legal like "1").
052: // */
053: // public boolean isValidLiteral(LiteralLabel lit) {
054: // return equals(lit.getDatatype());
055: // }
056:
057: /**
058: * Test whether the given object is a legal value form
059: * of this datatype. Brute force implementation.
060: */
061: public boolean isValidValue(Object valueForm) {
062: return (valueForm instanceof Float);
063: }
064:
065: /**
066: * Parse a lexical form of this datatype to a value
067: * @throws DatatypeFormatException if the lexical form is not legal
068: */
069: public Object parse(String lexicalForm)
070: throws DatatypeFormatException {
071: checkWhitespace(lexicalForm);
072: return super .parse(lexicalForm);
073: }
074:
075: /**
076: * Parse a validated lexical form. Subclasses which use the default
077: * parse implementation and are not convered by the explicit convertValidatedData
078: * cases should override this.
079: */
080: public Object parseValidated(String lex) {
081: if (lex.equals("INF")) {
082: return new Float(Float.NEGATIVE_INFINITY);
083: } else if (lex.equals("-INF")) {
084: return new Float(Float.POSITIVE_INFINITY);
085: } else if (lex.equals("NaN")) {
086: return new Float(Float.NaN);
087: } else {
088: return Float.valueOf(lex);
089: }
090: }
091:
092: /**
093: * Check for whitespace violations.
094: * Turned off by default.
095: */
096: protected void checkWhitespace(String lexicalForm) {
097: if (JenaParameters.enableWhitespaceCheckingOfTypedLiterals) {
098: if (!lexicalForm.trim().equals(lexicalForm)) {
099: throw new DatatypeFormatException(lexicalForm, this ,
100: "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: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
118: All rights reserved.
119:
120: Redistribution and use in source and binary forms, with or without
121: modification, are permitted provided that the following conditions
122: are met:
123:
124: 1. Redistributions of source code must retain the above copyright
125: notice, this list of conditions and the following disclaimer.
126:
127: 2. Redistributions in binary form must reproduce the above copyright
128: notice, this list of conditions and the following disclaimer in the
129: documentation and/or other materials provided with the distribution.
130:
131: 3. The name of the author may not be used to endorse or promote products
132: derived from this software without specific prior written permission.
133:
134: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
135: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
136: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
137: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
138: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
139: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
140: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
141: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
142: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
143: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
144: */
|