KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > FloatDV


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.xs;
18
19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
20 import org.apache.xerces.impl.dv.ValidationContext;
21 import org.apache.xerces.xs.datatypes.XSFloat;
22
23 /**
24  * Represent the schema type "float"
25  *
26  * @xerces.internal
27  *
28  * @author Neeraj Bajaj, Sun Microsystems, inc.
29  * @author Sandy Gao, IBM
30  *
31  * @version $Id: FloatDV.java,v 1.12 2005/07/14 04:20:15 mrglavas Exp $
32  */

33 public class FloatDV extends TypeValidator {
34
35     public short getAllowedFacets(){
36         return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE | XSSimpleTypeDecl.FACET_MINEXCLUSIVE );
37     }//getAllowedFacets()
38

39     //convert a String to Float form, we have to take care of cases specified in spec like INF, -INF and NaN
40
public Object JavaDoc getActualValue(String JavaDoc content, ValidationContext context) throws InvalidDatatypeValueException {
41         try{
42             return new XFloat(content);
43         } catch (NumberFormatException JavaDoc ex){
44             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object JavaDoc[]{content, "float"});
45         }
46     }//getActualValue()
47

48     // Can't call Float#compareTo method, because it's introduced in jdk 1.2
49
public int compare(Object JavaDoc value1, Object JavaDoc value2){
50         return ((XFloat)value1).compareTo((XFloat)value2);
51     }//compare()
52

53     //distinguishes between identity and equality for float datatype
54
//0.0 is equal but not identical to -0.0
55
public boolean isIdentical (Object JavaDoc value1, Object JavaDoc value2) {
56         if (value2 instanceof XFloat) {
57             return ((XFloat)value1).isIdentical((XFloat)value2);
58         }
59         return false;
60     }//isIdentical()
61

62     private static final class XFloat implements XSFloat {
63
64         private float value;
65         public XFloat(String JavaDoc s) throws NumberFormatException JavaDoc {
66             if (DoubleDV.isPossibleFP(s)) {
67                 value = Float.parseFloat(s);
68             }
69             else if ( s.equals("INF") ) {
70                 value = Float.POSITIVE_INFINITY;
71             }
72             else if ( s.equals("-INF") ) {
73                 value = Float.NEGATIVE_INFINITY;
74             }
75             else if ( s.equals("NaN") ) {
76                 value = Float.NaN;
77             }
78             else {
79                 throw new NumberFormatException JavaDoc(s);
80             }
81         }
82
83         public boolean equals(Object JavaDoc val) {
84             if (val == this)
85                 return true;
86
87             if (!(val instanceof XFloat))
88                 return false;
89             XFloat oval = (XFloat)val;
90
91             // NOTE: we don't distinguish 0.0 from -0.0
92
if (value == oval.value)
93                 return true;
94
95             if (value != value && oval.value != oval.value)
96                 return true;
97
98             return false;
99         }
100         
101         // NOTE: 0.0 is equal but not identical to -0.0
102
public boolean isIdentical (XFloat val) {
103             if (val == this) {
104                 return true;
105             }
106             
107             if (value == val.value) {
108                 return (value != 0.0f ||
109                     (Float.floatToIntBits(value) == Float.floatToIntBits(val.value)));
110             }
111             
112             if (value != value && val.value != val.value)
113                 return true;
114
115             return false;
116         }
117
118         private int compareTo(XFloat val) {
119             float oval = val.value;
120
121             // this < other
122
if (value < oval)
123                 return -1;
124             // this > other
125
if (value > oval)
126                 return 1;
127             // this == other
128
// NOTE: we don't distinguish 0.0 from -0.0
129
if (value == oval)
130                 return 0;
131
132             // one of the 2 values or both is/are NaN(s)
133

134             if (value != value) {
135                 // this = NaN = other
136
if (oval != oval)
137                     return 0;
138                 // this is NaN <> other
139
return INDETERMINATE;
140             }
141
142             // other is NaN <> this
143
return INDETERMINATE;
144         }
145
146         private String JavaDoc canonical;
147         public synchronized String JavaDoc toString() {
148             if (canonical == null) {
149                 if (value == Float.POSITIVE_INFINITY)
150                     canonical = "INF";
151                 else if (value == Float.NEGATIVE_INFINITY)
152                     canonical = "-INF";
153                 else if (value != value)
154                     canonical = "NaN";
155                 // NOTE: we don't distinguish 0.0 from -0.0
156
else if (value == 0)
157                     canonical = "0.0E1";
158                 else {
159                     // REVISIT: use the java algorithm for now, because we
160
// don't know what to output for 1.1f (which is no
161
// actually 1.1)
162
canonical = Float.toString(value);
163                     // if it contains 'E', then it should be a valid schema
164
// canonical representation
165
if (canonical.indexOf('E') == -1) {
166                         int len = canonical.length();
167                         // at most 3 longer: E, -, 9
168
char[] chars = new char[len+3];
169                         canonical.getChars(0, len, chars, 0);
170                         // expected decimal point position
171
int edp = chars[0] == '-' ? 2 : 1;
172                         // for non-zero integer part
173
if (value >= 1 || value <= -1) {
174                             // decimal point position
175
int dp = canonical.indexOf('.');
176                             // move the digits: ddd.d --> d.ddd
177
for (int i = dp; i > edp; i--) {
178                                 chars[i] = chars[i-1];
179                             }
180                             chars[edp] = '.';
181                             // trim trailing zeros: d00.0 --> d.000 --> d.
182
while (chars[len-1] == '0')
183                                 len--;
184                             // add the last zero if necessary: d. --> d.0
185
if (chars[len-1] == '.')
186                                 len++;
187                             // append E: d.dd --> d.ddE
188
chars[len++] = 'E';
189                             // how far we shifted the decimal point
190
int shift = dp - edp;
191                             // append the exponent --> d.ddEd
192
// the exponent is at most 7
193
chars[len++] = (char)(shift + '0');
194                         }
195                         else {
196                             // non-zero digit point
197
int nzp = edp + 1;
198                             // skip zeros: 0.003
199
while (chars[nzp] == '0')
200                                 nzp++;
201                             // put the first non-zero digit to the left of '.'
202
chars[edp-1] = chars[nzp];
203                             chars[edp] = '.';
204                             // move other digits (non-zero) to the right of '.'
205
for (int i = nzp+1, j = edp+1; i < len; i++, j++)
206                                 chars[j] = chars[i];
207                             // adjust the length
208
len -= nzp - edp;
209                             // append 0 if nessary: 0.03 --> 3. --> 3.0
210
if (len == edp + 1)
211                                 chars[len++] = '0';
212                             // append E-: d.dd --> d.ddE-
213
chars[len++] = 'E';
214                             chars[len++] = '-';
215                             // how far we shifted the decimal point
216
int shift = nzp - edp;
217                             // append the exponent --> d.ddEd
218
// the exponent is at most 3
219
chars[len++] = (char)(shift + '0');
220                         }
221                         canonical = new String JavaDoc(chars, 0, len);
222                     }
223                 }
224             }
225             return canonical;
226         }
227         
228         public float getValue() {
229             return value;
230         }
231     }
232 } // class FloatDV
233
Popular Tags