KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > parser > NumberParser


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

18 package org.apache.batik.parser;
19
20 import java.io.IOException JavaDoc;
21
22 /**
23  * This class represents a parser with support for numbers.
24  *
25  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
26  * @version $Id: NumberParser.java,v 1.11 2005/03/18 00:38:12 deweese Exp $
27  */

28 public abstract class NumberParser extends AbstractParser {
29
30     /**
31      * Parses the content of the buffer and converts it to a float.
32      */

33     protected float parseFloat() throws ParseException, IOException JavaDoc {
34         int mant = 0;
35         int mantDig = 0;
36         boolean mantPos = true;
37         boolean mantRead = false;
38
39         int exp = 0;
40         int expDig = 0;
41         int expAdj = 0;
42         boolean expPos = true;
43
44         switch (current) {
45         case '-':
46             mantPos = false;
47         case '+':
48             current = reader.read();
49         }
50
51         m1: switch (current) {
52         default:
53             reportError("character.unexpected",
54                         new Object JavaDoc[] { new Integer JavaDoc(current) });
55             return 0f;
56
57         case '.':
58             break;
59
60         case '0':
61             mantRead = true;
62             l: for (;;) {
63                 current = reader.read();
64                 switch (current) {
65                 case '1': case '2': case '3': case '4':
66                 case '5': case '6': case '7': case '8': case '9':
67                     break l;
68                 case '.': case 'e': case 'E':
69                     break m1;
70                 default:
71                     return 0f;
72                 case '0':
73                 }
74             }
75
76         case '1': case '2': case '3': case '4':
77         case '5': case '6': case '7': case '8': case '9':
78             mantRead = true;
79             l: for (;;) {
80                 if (mantDig < 9) {
81                     mantDig++;
82                     mant = mant * 10 + (current - '0');
83                 } else {
84                     expAdj++;
85                 }
86                 current = reader.read();
87                 switch (current) {
88                 default:
89                     break l;
90                 case '0': case '1': case '2': case '3': case '4':
91                 case '5': case '6': case '7': case '8': case '9':
92                 }
93             }
94         }
95         
96         if (current == '.') {
97             current = reader.read();
98             m2: switch (current) {
99             default:
100             case 'e': case 'E':
101                 if (!mantRead) {
102                     reportError("character.unexpected",
103                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
104                     return 0f;
105                 }
106                 break;
107
108             case '0':
109                 if (mantDig == 0) {
110                     l: for (;;) {
111                         current = reader.read();
112                         expAdj--;
113                         switch (current) {
114                         case '1': case '2': case '3': case '4':
115                         case '5': case '6': case '7': case '8': case '9':
116                             break l;
117                         default:
118                             if (!mantRead) {
119                                 return 0f;
120                             }
121                             break m2;
122                         case '0':
123                         }
124                     }
125                 }
126             case '1': case '2': case '3': case '4':
127             case '5': case '6': case '7': case '8': case '9':
128                 l: for (;;) {
129                     if (mantDig < 9) {
130                         mantDig++;
131                         mant = mant * 10 + (current - '0');
132                         expAdj--;
133                     }
134                     current = reader.read();
135                     switch (current) {
136                     default:
137                         break l;
138                     case '0': case '1': case '2': case '3': case '4':
139                     case '5': case '6': case '7': case '8': case '9':
140                     }
141                 }
142             }
143         }
144
145         switch (current) {
146         case 'e': case 'E':
147             current = reader.read();
148             switch (current) {
149             default:
150                 reportError("character.unexpected",
151                             new Object JavaDoc[] { new Integer JavaDoc(current) });
152                 return 0f;
153             case '-':
154                 expPos = false;
155             case '+':
156                 current = reader.read();
157                 switch (current) {
158                 default:
159                     reportError("character.unexpected",
160                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
161                     return 0f;
162                 case '0': case '1': case '2': case '3': case '4':
163                 case '5': case '6': case '7': case '8': case '9':
164                 }
165             case '0': case '1': case '2': case '3': case '4':
166             case '5': case '6': case '7': case '8': case '9':
167             }
168             
169             en: switch (current) {
170             case '0':
171                 l: for (;;) {
172                     current = reader.read();
173                     switch (current) {
174                     case '1': case '2': case '3': case '4':
175                     case '5': case '6': case '7': case '8': case '9':
176                         break l;
177                     default:
178                         break en;
179                     case '0':
180                     }
181                 }
182
183             case '1': case '2': case '3': case '4':
184             case '5': case '6': case '7': case '8': case '9':
185                 l: for (;;) {
186                     if (expDig < 3) {
187                         expDig++;
188                         exp = exp * 10 + (current - '0');
189                     }
190                     current = reader.read();
191                     switch (current) {
192                     default:
193                         break l;
194                     case '0': case '1': case '2': case '3': case '4':
195                     case '5': case '6': case '7': case '8': case '9':
196                     }
197                 }
198             }
199         default:
200         }
201
202         if (!expPos) {
203             exp = -exp;
204         }
205         exp += expAdj;
206         if (!mantPos) {
207             mant = -mant;
208         }
209
210         return buildFloat(mant, exp);
211     }
212
213     /**
214      * Computes a float from mantissa and exponent.
215      */

216     public static float buildFloat(int mant, int exp) {
217         if (exp < -125 || mant == 0) {
218             return 0f;
219         }
220
221         if (exp >= 128) {
222             return (mant > 0)
223                 ? Float.POSITIVE_INFINITY
224                 : Float.NEGATIVE_INFINITY;
225         }
226
227         if (exp == 0) {
228             return mant;
229         }
230             
231         if (mant >= (1 << 26)) {
232             mant++; // round up trailing bits if they will be dropped.
233
}
234
235         return (exp > 0) ? mant * pow10[exp] : mant / pow10[-exp];
236     }
237
238     /**
239      * Array of powers of ten.
240      */

241     private static final float pow10[] = new float [128];
242     static {
243         for (int i = 0; i < pow10.length; i++) {
244             pow10[i] = (float)Math.pow(10, i);
245         }
246     }
247 }
248
Popular Tags