KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2000-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 implements an event-based parser for the SVG length
24  * values.
25  *
26  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: LengthParser.java,v 1.12 2004/08/18 07:14:47 vhardy Exp $
28  */

29 public class LengthParser extends AbstractParser {
30
31     /**
32      * The length handler used to report parse events.
33      */

34     protected LengthHandler lengthHandler;
35
36     /**
37      * Creates a new LengthParser.
38      */

39     public LengthParser() {
40     lengthHandler = DefaultLengthHandler.INSTANCE;
41     }
42
43     /**
44      * Allows an application to register a length handler.
45      *
46      * <p>If the application does not register a handler, all
47      * events reported by the parser will be silently ignored.
48      *
49      * <p>Applications may register a new or different handler in the
50      * middle of a parse, and the parser must begin using the new
51      * handler immediately.</p>
52      * @param handler The transform list handler.
53      */

54     public void setLengthHandler(LengthHandler handler) {
55     lengthHandler = handler;
56     }
57
58     /**
59      * Returns the length handler in use.
60      */

61     public LengthHandler getLengthHandler() {
62     return lengthHandler;
63     }
64
65     protected void doParse() throws ParseException, IOException JavaDoc {
66         lengthHandler.startLength();
67
68         current = reader.read();
69         skipSpaces();
70     
71         parseLength();
72
73         skipSpaces();
74         if (current != -1) {
75             reportError("end.of.stream.expected",
76                         new Object JavaDoc[] { new Integer JavaDoc(current) });
77         }
78         lengthHandler.endLength();
79     }
80
81     /**
82      * Parses a length value.
83      */

84     protected void parseLength() throws ParseException, IOException JavaDoc {
85         int mant = 0;
86         int mantDig = 0;
87         boolean mantPos = true;
88         boolean mantRead = false;
89
90         int exp = 0;
91         int expDig = 0;
92         int expAdj = 0;
93         boolean expPos = true;
94
95         int unitState = 0;
96
97         switch (current) {
98         case '-':
99             mantPos = false;
100         case '+':
101             current = reader.read();
102         }
103
104         m1: switch (current) {
105         default:
106             reportError("character.unexpected",
107                         new Object JavaDoc[] { new Integer JavaDoc(current) });
108             return;
109
110         case '.':
111             break;
112
113         case '0':
114             mantRead = true;
115             l: for (;;) {
116                 current = reader.read();
117                 switch (current) {
118                 case '1': case '2': case '3': case '4':
119                 case '5': case '6': case '7': case '8': case '9':
120                     break l;
121                 default:
122                     break m1;
123                 case '0':
124                 }
125             }
126
127         case '1': case '2': case '3': case '4':
128         case '5': case '6': case '7': case '8': case '9':
129             mantRead = true;
130             l: for (;;) {
131                 if (mantDig < 9) {
132                     mantDig++;
133                     mant = mant * 10 + (current - '0');
134                 } else {
135                     expAdj++;
136                 }
137                 current = reader.read();
138                 switch (current) {
139                 default:
140                     break l;
141                 case '0': case '1': case '2': case '3': case '4':
142                 case '5': case '6': case '7': case '8': case '9':
143                 }
144             }
145         }
146         
147         if (current == '.') {
148             current = reader.read();
149             m2: switch (current) {
150             default:
151             case 'e': case 'E':
152                 if (!mantRead) {
153                     reportError("character.unexpected",
154                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
155                     return;
156                 }
157                 break;
158
159             case '0':
160                 if (mantDig == 0) {
161                     l: for (;;) {
162                         current = reader.read();
163                         expAdj--;
164                         switch (current) {
165                         case '1': case '2': case '3': case '4':
166                         case '5': case '6': case '7': case '8': case '9':
167                             break l;
168                         default:
169                             break m2;
170                         case '0':
171                         }
172                     }
173                 }
174             case '1': case '2': case '3': case '4':
175             case '5': case '6': case '7': case '8': case '9':
176                 l: for (;;) {
177                     if (mantDig < 9) {
178                         mantDig++;
179                         mant = mant * 10 + (current - '0');
180                         expAdj--;
181                     }
182                     current = reader.read();
183                     switch (current) {
184                     default:
185                         break l;
186                     case '0': case '1': case '2': case '3': case '4':
187                     case '5': case '6': case '7': case '8': case '9':
188                     }
189                 }
190             }
191         }
192
193         boolean le = false;
194         es: switch (current) {
195         case 'e':
196             le = true;
197         case 'E':
198             current = reader.read();
199             switch (current) {
200             default:
201                 reportError("character.unexpected",
202                             new Object JavaDoc[] { new Integer JavaDoc(current) });
203                 return;
204             case 'm':
205                 if (!le) {
206                     reportError("character.unexpected",
207                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
208                     return;
209                 }
210                 unitState = 1;
211                 break es;
212             case 'x':
213                 if (!le) {
214                     reportError("character.unexpected",
215                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
216                     return;
217                 }
218                 unitState = 2;
219                 break es;
220             case '-':
221                 expPos = false;
222             case '+':
223                 current = reader.read();
224                 switch (current) {
225                 default:
226                     reportError("character.unexpected",
227                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
228                     return;
229                 case '0': case '1': case '2': case '3': case '4':
230                 case '5': case '6': case '7': case '8': case '9':
231                 }
232             case '0': case '1': case '2': case '3': case '4':
233             case '5': case '6': case '7': case '8': case '9':
234             }
235             
236             en: switch (current) {
237             case '0':
238                 l: for (;;) {
239                     current = reader.read();
240                     switch (current) {
241                     case '1': case '2': case '3': case '4':
242                     case '5': case '6': case '7': case '8': case '9':
243                         break l;
244                     default:
245                         break en;
246                     case '0':
247                     }
248                 }
249
250             case '1': case '2': case '3': case '4':
251             case '5': case '6': case '7': case '8': case '9':
252                 l: for (;;) {
253                     if (expDig < 3) {
254                         expDig++;
255                         exp = exp * 10 + (current - '0');
256                     }
257                     current = reader.read();
258                     switch (current) {
259                     default:
260                         break l;
261                     case '0': case '1': case '2': case '3': case '4':
262                     case '5': case '6': case '7': case '8': case '9':
263                     }
264                 }
265             }
266         default:
267         }
268
269         if (!expPos) {
270             exp = -exp;
271         }
272         exp += expAdj;
273         if (!mantPos) {
274             mant = -mant;
275         }
276
277         lengthHandler.lengthValue(NumberParser.buildFloat(mant, exp));
278
279         switch (unitState) {
280         case 1:
281             lengthHandler.em();
282             current = reader.read();
283             return;
284         case 2:
285             lengthHandler.ex();
286             current = reader.read();
287             return;
288         }
289
290         switch (current) {
291         case 'e':
292             current = reader.read();
293             switch (current) {
294             case 'm':
295                 lengthHandler.em();
296                 current = reader.read();
297                 break;
298             case 'x':
299                 lengthHandler.ex();
300                 current = reader.read();
301                 break;
302             default:
303                 reportError("character.unexpected",
304                             new Object JavaDoc[] { new Integer JavaDoc(current) });
305             }
306             break;
307
308         case 'p':
309             current = reader.read();
310             switch (current) {
311             case 'c':
312                 lengthHandler.pc();
313                 current = reader.read();
314                 break;
315             case 't':
316                 lengthHandler.pt();
317                 current = reader.read();
318                 break;
319             case 'x':
320                 lengthHandler.px();
321                 current = reader.read();
322                 break;
323             default:
324                 reportError("character.unexpected",
325                             new Object JavaDoc[] { new Integer JavaDoc(current) });
326             }
327             break;
328
329         case 'i':
330             current = reader.read();
331             if (current != 'n') {
332                 reportError("character.expected",
333                             new Object JavaDoc[] { new Character JavaDoc('n'),
334                                            new Integer JavaDoc(current) });
335                 break;
336             }
337             lengthHandler.in();
338             current = reader.read();
339             break;
340         case 'c':
341             current = reader.read();
342             if (current != 'm') {
343                 reportError("character.expected",
344                             new Object JavaDoc[] { new Character JavaDoc('m'),
345                                            new Integer JavaDoc(current) });
346                 break;
347             }
348             lengthHandler.cm();
349             current = reader.read();
350             break;
351         case 'm':
352             current = reader.read();
353             if (current != 'm') {
354                 reportError("character.expected",
355                             new Object JavaDoc[] { new Character JavaDoc('m'),
356                                            new Integer JavaDoc(current) });
357                 break;
358             }
359             lengthHandler.mm();
360             current = reader.read();
361             break;
362         case '%':
363             lengthHandler.percentage();
364             current = reader.read();
365             break;
366         }
367     }
368 }
369
Popular Tags