KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > datatype > FloatDatatypeValidator


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.validators.datatype;
59
60 import java.util.Hashtable JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 /**
64  *
65  * @author Elena Litani
66  * @author Ted Leung
67  * @author Jeffrey Rodriguez
68  * @author Mark Swinkles - List Validation refactoring
69  * @version $Id: FloatDatatypeValidator.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
70  */

71
72 public class FloatDatatypeValidator extends AbstractNumericValidator {
73
74     public FloatDatatypeValidator () throws InvalidDatatypeFacetException{
75         this( null, null, false ); // Native, No Facets defined, Restriction
76
}
77
78     public FloatDatatypeValidator ( DatatypeValidator base, Hashtable JavaDoc facets,
79                                     boolean derivedByList ) throws InvalidDatatypeFacetException {
80         // Set base type
81
super (base, facets, derivedByList);
82     }
83     
84    public int compare( String JavaDoc value1, String JavaDoc value2) {
85         try {
86             float f1 = fValueOf(value1).floatValue();
87             float f2 = fValueOf(value2).floatValue();
88             return compareFloats(f1, f2);
89         }
90         catch ( NumberFormatException JavaDoc e ) {
91             //REVISIT: should we throw exception??
92
return -1;
93         }
94     }
95
96     protected void assignAdditionalFacets(String JavaDoc key, Hashtable JavaDoc facets ) throws InvalidDatatypeFacetException{
97         throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_FLOAT_FACET,
98                                                                 DatatypeMessageProvider.MSG_NONE, new Object JavaDoc[] { key}));
99     }
100
101     protected int compareValues (Object JavaDoc value1, Object JavaDoc value2) {
102
103         float f1 = ((Float JavaDoc)value1).floatValue();
104         float f2 = ((Float JavaDoc)value2).floatValue();
105         return compareFloats(f1, f2);
106     }
107     
108     protected void setMaxInclusive (String JavaDoc value) {
109         fMaxInclusive = fValueOf(value);
110     }
111     protected void setMinInclusive (String JavaDoc value) {
112         fMinInclusive = fValueOf(value);
113
114     }
115     protected void setMaxExclusive (String JavaDoc value) {
116         fMaxExclusive = fValueOf(value);
117
118     }
119     protected void setMinExclusive (String JavaDoc value) {
120         fMinExclusive = fValueOf(value);
121
122     }
123     protected void setEnumeration (Vector JavaDoc enumeration) throws InvalidDatatypeValueException{
124         if ( enumeration != null ) {
125             fEnumeration = new Float JavaDoc[enumeration.size()];
126             Object JavaDoc[] baseEnum=null;
127             for ( int i = 0; i < enumeration.size(); i++ ){
128                     fEnumeration[i] = fValueOf((String JavaDoc) enumeration.elementAt(i));
129                     ((FloatDatatypeValidator)fBaseValidator).validate((String JavaDoc)enumeration.elementAt(i), null);
130             }
131         }
132     }
133
134
135     protected String JavaDoc getMaxInclusive (boolean isBase) {
136         return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMaxInclusive.toString())
137         :((Float JavaDoc)fMaxInclusive).toString();
138     }
139     protected String JavaDoc getMinInclusive (boolean isBase) {
140         return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMinInclusive.toString())
141         :((Float JavaDoc)fMinInclusive).toString();
142     }
143     protected String JavaDoc getMaxExclusive (boolean isBase) {
144         return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMaxExclusive.toString())
145         :((Float JavaDoc)fMaxExclusive).toString();
146     }
147     protected String JavaDoc getMinExclusive (boolean isBase) {
148         return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMinExclusive.toString())
149         :((Float JavaDoc)fMinExclusive).toString();
150     }
151
152     /**
153     * validate if the content is valid against base datatype and facets (if any)
154     * this function might be called directly from UnionDatatype or ListDatatype
155     *
156     * @param content A string containing the content to be validated
157     * @param enumeration A vector with enumeration strings
158     * @exception throws InvalidDatatypeException if the content is
159     * is not a W3C float type;
160     * @exception throws InvalidDatatypeFacetException if enumeration is not float
161     */

162
163     protected void checkContent(String JavaDoc content, Object JavaDoc state, Vector JavaDoc enumeration, boolean asBase)
164     throws InvalidDatatypeValueException {
165         // validate against parent type if any
166
if ( this.fBaseValidator != null ) {
167             // validate content as a base type
168
((FloatDatatypeValidator)fBaseValidator).checkContent(content, state, enumeration, true);
169         }
170
171         // we check pattern first
172
if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) {
173             if ( fRegex == null || fRegex.matches( content) == false )
174                 throw new InvalidDatatypeValueException("Value'"+content+
175                                                         "does not match regular expression facet" + fPattern );
176         }
177
178         // if this is a base validator, we only need to check pattern facet
179
// all other facet were inherited by the derived type
180
if ( asBase )
181             return;
182
183         Float JavaDoc f = null;
184         try {
185             f = fValueOf(content);
186         }
187         catch ( NumberFormatException JavaDoc nfe ) {
188             throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.NOT_FLOAT,
189                                                                     DatatypeMessageProvider.MSG_NONE,
190                                                                     new Object JavaDoc [] {content}));
191         }
192
193         //enumeration is passed from List or Union datatypes
194
if ( enumeration != null ) {
195             int size = enumeration.size();
196             Float JavaDoc[] enumFloats = new Float JavaDoc[size];
197             int i=0;
198             try {
199                 for ( ; i < size; i++ )
200                     enumFloats[i] = fValueOf((String JavaDoc) enumeration.elementAt(i));
201
202             }
203             catch ( NumberFormatException JavaDoc nfe ) {
204                 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.INVALID_ENUM_VALUE,
205                                                                         DatatypeMessageProvider.MSG_NONE,
206                                                                         new Object JavaDoc [] { enumeration.elementAt(i)}));
207             }
208             enumCheck(f.floatValue(), enumFloats);
209         }
210
211         boundsCheck(f);
212
213         if ( ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0 &&
214               (fEnumeration != null) ) ) {
215                enumCheck(f.floatValue(), (Float JavaDoc[])fEnumeration);
216         }
217     }
218
219     protected int getInvalidFacetMsg (){
220         return DatatypeMessageProvider.ILLEGAL_FLOAT_FACET;
221     }
222
223     private void enumCheck(float v, Float JavaDoc[] enumFloats) throws InvalidDatatypeValueException {
224         for ( int i = 0; i < enumFloats.length; i++ ) {
225             if ( v == ((Float JavaDoc)enumFloats[i]).floatValue() ) return;
226         }
227         throw new InvalidDatatypeValueException(
228                                                getErrorString(DatatypeMessageProvider.NOT_ENUM_VALUE,
229                                                               DatatypeMessageProvider.MSG_NONE,
230                                                               new Object JavaDoc [] { new Float JavaDoc(v)}));
231     }
232
233
234     private static Float JavaDoc fValueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
235         Float JavaDoc f=null;
236         try {
237             f = Float.valueOf(s);
238         }
239         catch ( NumberFormatException JavaDoc nfe ) {
240             if ( s.equals("INF") ) {
241                 f = new Float JavaDoc(Float.POSITIVE_INFINITY);
242             }
243             else if ( s.equals("-INF") ) {
244                 f = new Float JavaDoc (Float.NEGATIVE_INFINITY);
245             }
246             else if ( s.equals("NaN" ) ) {
247                 f = new Float JavaDoc (Float.NaN);
248             }
249             else {
250                 throw nfe;
251             }
252         }
253         return f;
254     }
255
256     private int compareFloats( float f1, float f2){
257         int f1V = Float.floatToIntBits(f1);
258         int f2V = Float.floatToIntBits(f2);
259         if ( f1 > f2 ) {
260             return 1;
261         }
262         if ( f1 < f2 ) {
263             return -1;
264         }
265         if ( f1V==f2V ) {
266             return 0;
267         }
268         //REVISIT: NaN values comparison..
269
return(f1V < f2V) ? -1 : 1;
270     }
271 }
272
Popular Tags