KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @author Elena Litani
65  * @author Ted Leung
66  * @author Jeffrey Rodriguez
67  * @author Mark Swinkles - List Validation refactoring
68  * @version $Id: DoubleDatatypeValidator.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
69  */

70
71 public class DoubleDatatypeValidator extends AbstractNumericValidator {
72
73     public DoubleDatatypeValidator () throws InvalidDatatypeFacetException {
74         this( null, null, false ); // Native, No Facets defined, Restriction
75
}
76
77     public DoubleDatatypeValidator ( DatatypeValidator base, Hashtable JavaDoc facets,
78                                      boolean derivedByList ) throws InvalidDatatypeFacetException {
79         super(base, facets, derivedByList);
80     }
81
82     public int compare( String JavaDoc value1, String JavaDoc value2) {
83         try {
84             double d1 = dValueOf(value1).doubleValue();
85             double d2 = dValueOf(value2).doubleValue();
86             return compareDoubles(d1, d2);
87         }
88         catch ( NumberFormatException JavaDoc e ) {
89             //REVISIT: should we throw exception??
90
return -1;
91         }
92
93     }
94
95     protected void assignAdditionalFacets(String JavaDoc key, Hashtable JavaDoc facets ) throws InvalidDatatypeFacetException{
96         throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_DOUBLE_FACET,
97                                                                 DatatypeMessageProvider.MSG_NONE, new Object JavaDoc[] { key}));
98     }
99     /**
100      * Compares 2 double values.
101      *
102      * @param value1 - Double
103      * @param value2 - Double
104      * @return value1<value2 return -1
105      * value1>value2 return 1
106      * value1==value2 return 0
107      */

108     protected int compareValues (Object JavaDoc value1, Object JavaDoc value2) {
109         double d1 = ((Double JavaDoc)value1).doubleValue();
110         double d2 = ((Double JavaDoc)value2).doubleValue();
111         return compareDoubles(d1, d2);
112     }
113
114     protected void setMaxInclusive (String JavaDoc value) {
115         fMaxInclusive = dValueOf(value);
116     }
117     protected void setMinInclusive (String JavaDoc value) {
118         fMinInclusive = dValueOf(value);
119
120     }
121     protected void setMaxExclusive (String JavaDoc value) {
122         fMaxExclusive = dValueOf(value);
123
124     }
125     protected void setMinExclusive (String JavaDoc value) {
126         fMinExclusive = dValueOf(value);
127
128     }
129     protected void setEnumeration (Vector JavaDoc enumeration) throws InvalidDatatypeValueException{
130         if ( enumeration != null ) {
131             fEnumeration = new Double JavaDoc[enumeration.size()];
132             Object JavaDoc baseEnum=null;
133             try {
134
135                 for ( int i = 0; i < enumeration.size(); i++ ) {
136                     fEnumeration[i] = dValueOf((String JavaDoc)enumeration.elementAt(i));
137                     ((DoubleDatatypeValidator)fBaseValidator).validate((String JavaDoc)enumeration.elementAt(i), null);
138
139                 }
140             }
141             catch ( Exception JavaDoc e ) {
142                 throw new InvalidDatatypeValueException(e.getMessage());
143             }
144         }
145     }
146
147
148     protected String JavaDoc getMaxInclusive (boolean isBase) {
149         return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMaxInclusive.toString())
150         :((Double JavaDoc)fMaxInclusive).toString();
151     }
152     protected String JavaDoc getMinInclusive (boolean isBase) {
153         return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMinInclusive.toString())
154         :((Double JavaDoc)fMinInclusive).toString();
155     }
156     protected String JavaDoc getMaxExclusive (boolean isBase) {
157         return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMaxExclusive.toString())
158         :((Double JavaDoc)fMaxExclusive).toString();
159     }
160     protected String JavaDoc getMinExclusive (boolean isBase) {
161         return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMinExclusive.toString())
162         :((Double JavaDoc)fMinExclusive).toString();
163     }
164
165     /**
166     * validate if the content is valid against base datatype and facets (if any)
167     * this function might be called directly from UnionDatatype or ListDatatype
168     *
169     * @param content A string containing the content to be validated
170     * @param enumeration A vector with enumeration strings
171     * @exception throws InvalidDatatypeException if the content is
172     * is not a W3C double type;
173     * @exception throws InvalidDatatypeFacetException if enumeration is not double
174     */

175
176     protected void checkContentEnum(String JavaDoc content, Object JavaDoc state, Vector JavaDoc enumeration)
177     throws InvalidDatatypeValueException {
178         checkContent (content, state, enumeration, false);
179     }
180
181     protected void checkContent(String JavaDoc content, Object JavaDoc state, Vector JavaDoc enumeration, boolean asBase)
182     throws InvalidDatatypeValueException {
183         // validate against parent type if any
184
if ( this.fBaseValidator != null ) {
185             // validate content as a base type
186
((DoubleDatatypeValidator)fBaseValidator).checkContent(content, state, enumeration, true);
187         }
188
189         // we check pattern first
190
if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) {
191             if ( fRegex == null || fRegex.matches( content) == false )
192                 throw new InvalidDatatypeValueException("Value'"+content+
193                                                         "does not match regular expression facet" + fPattern );
194         }
195
196         // if this is a base validator, we only need to check pattern facet
197
// all other facet were inherited by the derived type
198
if ( asBase )
199             return;
200
201         Double JavaDoc d = null;
202         try {
203             d = dValueOf(content);
204         }
205         catch ( NumberFormatException JavaDoc nfe ) {
206             throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.NOT_DOUBLE,
207                                                                     DatatypeMessageProvider.MSG_NONE,
208                                                                     new Object JavaDoc [] { content}));
209         }
210
211         if ( enumeration != null ) { //the call was made from List or union
212
int size = enumeration.size();
213             Double JavaDoc[] enumDoubles = new Double JavaDoc[size];
214             int i=0;
215             try {
216                 for ( ; i < size; i++ )
217                     enumDoubles[i] = dValueOf((String JavaDoc) enumeration.elementAt(i));
218             }
219             catch ( NumberFormatException JavaDoc nfe ) {
220                 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.INVALID_ENUM_VALUE,
221                                                                         DatatypeMessageProvider.MSG_NONE,
222                                                                         new Object JavaDoc [] { enumeration.elementAt(i)}));
223             }
224
225             enumCheck(d.doubleValue(), enumDoubles);
226         }
227
228         boundsCheck(d);
229
230         if ( ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0 &&
231               (fEnumeration != null) ) )
232             enumCheck(d.doubleValue(), (Double JavaDoc[])fEnumeration);
233     }
234
235     protected int getInvalidFacetMsg (){
236         return DatatypeMessageProvider.ILLEGAL_DOUBLE_FACET;
237     }
238
239     //
240
// private methods
241
//
242
private static Double JavaDoc dValueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
243         Double JavaDoc d;
244         try {
245             d = Double.valueOf(s);
246         }
247         catch ( NumberFormatException JavaDoc nfe ) {
248             if ( s.equals("INF") ) {
249                 d = new Double JavaDoc(Double.POSITIVE_INFINITY);
250             }
251             else if ( s.equals("-INF") ) {
252                 d = new Double JavaDoc (Double.NEGATIVE_INFINITY);
253             }
254             else if ( s.equals("NaN" ) ) {
255                 d = new Double JavaDoc (Double.NaN);
256             }
257             else {
258                 throw nfe;
259             }
260         }
261         return d;
262     }
263
264     private void enumCheck(double v, Double JavaDoc[] enumDoubles) throws InvalidDatatypeValueException {
265         for ( int i = 0; i < enumDoubles.length; i++ ) {
266             if ( v == enumDoubles[i].doubleValue() ) return;
267         }
268         throw new InvalidDatatypeValueException(
269                                                getErrorString(DatatypeMessageProvider.NOT_ENUM_VALUE,
270                                                               DatatypeMessageProvider.MSG_NONE,
271                                                               new Object JavaDoc [] { new Double JavaDoc(v)}));
272     }
273
274     private int compareDoubles(double d1, double d2) {
275         long d1V = Double.doubleToLongBits(d1);
276         long d2V = Double.doubleToLongBits(d2);
277
278         if ( d1 > d2 ) {
279             return 1;
280         }
281         if ( d1 < d2 ) {
282             return -1;
283         }
284         if ( d1V == d2V ) {
285             return 0;
286         }
287         //REVISIT: NaN values.. revisit when new PR vs of Schema is available
288
return(d1V < d2V) ? -1 : 1;
289     }
290 }
291
Popular Tags