KickJava   Java API By Example, From Geeks To Geeks.

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


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 preserveAspectRatio
24  * attribute values.
25  *
26  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
27  * @version $Id: PreserveAspectRatioParser.java,v 1.10 2004/08/18 07:14:47 vhardy Exp $
28  */

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

34     protected PreserveAspectRatioHandler preserveAspectRatioHandler;
35
36     /**
37      * Creates a new PreserveAspectRatioParser.
38      */

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

55     public void
56         setPreserveAspectRatioHandler(PreserveAspectRatioHandler handler) {
57     preserveAspectRatioHandler = handler;
58     }
59
60     /**
61      * Returns the length handler in use.
62      */

63     public PreserveAspectRatioHandler getPreserveAspectRatioHandler() {
64         return preserveAspectRatioHandler;
65     }
66
67     /**
68      * Parses the current stream.
69      */

70     protected void doParse() throws ParseException, IOException JavaDoc {
71     current = reader.read();
72     skipSpaces();
73
74         parsePreserveAspectRatio();
75     }
76
77     /**
78      * Parses a PreserveAspectRatio attribute.
79      */

80     protected void parsePreserveAspectRatio()
81         throws ParseException, IOException JavaDoc {
82     preserveAspectRatioHandler.startPreserveAspectRatio();
83
84         align: switch (current) {
85         case 'n':
86         current = reader.read();
87         if (current != 'o') {
88         reportError("character.expected",
89                 new Object JavaDoc[] { new Character JavaDoc('o'),
90                                            new Integer JavaDoc(current) });
91         skipIdentifier();
92         break align;
93         }
94         current = reader.read();
95         if (current != 'n') {
96         reportError("character.expected",
97                 new Object JavaDoc[] { new Character JavaDoc('n'),
98                        new Integer JavaDoc(current) });
99         skipIdentifier();
100         break align;
101         }
102         current = reader.read();
103         if (current != 'e') {
104         reportError("character.expected",
105                 new Object JavaDoc[] { new Character JavaDoc('e'),
106                        new Integer JavaDoc(current) });
107         skipIdentifier();
108         break align;
109         }
110         current = reader.read();
111         skipSpaces();
112         preserveAspectRatioHandler.none();
113             break;
114                 
115         case 'x':
116             current = reader.read();
117             if (current != 'M') {
118                 reportError("character.expected",
119                             new Object JavaDoc[] { new Character JavaDoc('M'),
120                        new Integer JavaDoc(current) });
121                 skipIdentifier();
122                 break;
123             }
124             current = reader.read();
125             switch (current) {
126             case 'a':
127                 current = reader.read();
128                 if (current != 'x') {
129                     reportError("character.expected",
130                                 new Object JavaDoc[] { new Character JavaDoc('x'),
131                                new Integer JavaDoc(current) });
132                     skipIdentifier();
133                     break align;
134                 }
135                 current = reader.read();
136                 if (current != 'Y') {
137                     reportError("character.expected",
138                                 new Object JavaDoc[] { new Character JavaDoc('Y'),
139                            new Integer JavaDoc(current) });
140                     skipIdentifier();
141                     break align;
142                 }
143                 current = reader.read();
144                 if (current != 'M') {
145                     reportError("character.expected",
146                                 new Object JavaDoc[] { new Character JavaDoc('M'),
147                            new Integer JavaDoc(current) });
148                     skipIdentifier();
149                     break align;
150                 }
151                 current = reader.read();
152                 switch (current) {
153                 case 'a':
154                     current = reader.read();
155                     if (current != 'x') {
156                         reportError("character.expected",
157                                     new Object JavaDoc[] { new Character JavaDoc('x'),
158                            new Integer JavaDoc(current) });
159                         skipIdentifier();
160                         break align;
161                     }
162                     preserveAspectRatioHandler.xMaxYMax();
163                     current = reader.read();
164                     break;
165                 case 'i':
166                     current = reader.read();
167                     switch (current) {
168                     case 'd':
169                         preserveAspectRatioHandler.xMaxYMid();
170                         current = reader.read();
171                         break;
172                     case 'n':
173                         preserveAspectRatioHandler.xMaxYMin();
174                         current = reader.read();
175                         break;
176                     default:
177                         reportError("character.unexpected",
178                                     new Object JavaDoc[] { new Integer JavaDoc(current) });
179                         skipIdentifier();
180                         break align;
181                     }
182                 }
183                 break;
184             case 'i':
185                 current = reader.read();
186                 switch (current) {
187                 case 'd':
188                     current = reader.read();
189                     if (current != 'Y') {
190                         reportError("character.expected",
191                                     new Object JavaDoc[] { new Character JavaDoc('Y'),
192                            new Integer JavaDoc(current) });
193                         skipIdentifier();
194                         break align;
195                     }
196                     current = reader.read();
197                     if (current != 'M') {
198                         reportError("character.expected",
199                                     new Object JavaDoc[] { new Character JavaDoc('M'),
200                            new Integer JavaDoc(current) });
201                         skipIdentifier();
202                         break align;
203                     }
204                     current = reader.read();
205                     switch (current) {
206                     case 'a':
207                         current = reader.read();
208                         if (current != 'x') {
209                             reportError
210                                 ("character.expected",
211                                  new Object JavaDoc[] { new Character JavaDoc('x'),
212                                                     new Integer JavaDoc(current) });
213                 skipIdentifier();
214                 break align;
215                         }
216                         preserveAspectRatioHandler.xMidYMax();
217                         current = reader.read();
218                         break;
219                     case 'i':
220                         current = reader.read();
221                         switch (current) {
222                         case 'd':
223                 preserveAspectRatioHandler.xMidYMid();
224                 current = reader.read();
225                 break;
226                         case 'n':
227                             preserveAspectRatioHandler.xMidYMin();
228                             current = reader.read();
229                             break;
230             default:
231                 reportError("character.unexpected",
232                     new Object JavaDoc[] { new Integer JavaDoc(current) });
233                 skipIdentifier();
234                 break align;
235                         }
236                     }
237                     break;
238                 case 'n':
239                     current = reader.read();
240                     if (current != 'Y') {
241                         reportError("character.expected",
242                                     new Object JavaDoc[] { new Character JavaDoc('Y'),
243                                new Integer JavaDoc(current) });
244                         skipIdentifier();
245                         break align;
246                     }
247                     current = reader.read();
248                     if (current != 'M') {
249                         reportError("character.expected",
250                                     new Object JavaDoc[] { new Character JavaDoc('M'),
251                            new Integer JavaDoc(current) });
252                         skipIdentifier();
253                         break align;
254                     }
255                     current = reader.read();
256                     switch (current) {
257                     case 'a':
258                         current = reader.read();
259                         if (current != 'x') {
260                             reportError
261                                 ("character.expected",
262                                  new Object JavaDoc[] { new Character JavaDoc('x'),
263                                                 new Integer JavaDoc(current) });
264                             skipIdentifier();
265                             break align;
266                         }
267                         preserveAspectRatioHandler.xMinYMax();
268                         current = reader.read();
269                         break;
270                     case 'i':
271                         current = reader.read();
272                         switch (current) {
273                         case 'd':
274                             preserveAspectRatioHandler.xMinYMid();
275                             current = reader.read();
276                             break;
277                         case 'n':
278                             preserveAspectRatioHandler.xMinYMin();
279                             current = reader.read();
280                             break;
281                         default:
282                             reportError
283                                 ("character.unexpected",
284                                  new Object JavaDoc[] { new Integer JavaDoc(current) });
285                             skipIdentifier();
286                             break align;
287                         }
288                     }
289                     break;
290                 default:
291                     reportError("character.unexpected",
292                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
293                     skipIdentifier();
294                     break align;
295                 }
296                 break;
297             default:
298                 reportError("character.unexpected",
299                             new Object JavaDoc[] { new Integer JavaDoc(current) });
300                 skipIdentifier();
301             }
302             break;
303         default:
304             if (current != -1) {
305                 reportError("character.unexpected",
306                             new Object JavaDoc[] { new Integer JavaDoc(current) });
307                 skipIdentifier();
308             }
309         }
310
311         skipCommaSpaces();
312
313         switch (current) {
314         case 'm':
315             current = reader.read();
316             if (current != 'e') {
317                 reportError("character.expected",
318                             new Object JavaDoc[] { new Character JavaDoc('e'),
319                            new Integer JavaDoc(current) });
320                 skipIdentifier();
321                 break;
322             }
323             current = reader.read();
324             if (current != 'e') {
325                 reportError("character.expected",
326                             new Object JavaDoc[] { new Character JavaDoc('e'),
327                            new Integer JavaDoc(current) });
328                 skipIdentifier();
329                 break;
330             }
331             current = reader.read();
332             if (current != 't') {
333                 reportError("character.expected",
334                             new Object JavaDoc[] { new Character JavaDoc('t'),
335                            new Integer JavaDoc(current) });
336                 skipIdentifier();
337                 break;
338             }
339             preserveAspectRatioHandler.meet();
340             current = reader.read();
341             break;
342         case 's':
343             current = reader.read();
344             if (current != 'l') {
345                 reportError("character.expected",
346                             new Object JavaDoc[] { new Character JavaDoc('l'),
347                            new Integer JavaDoc(current) });
348                 skipIdentifier();
349                 break;
350             }
351             current = reader.read();
352             if (current != 'i') {
353                 reportError("character.expected",
354                             new Object JavaDoc[] { new Character JavaDoc('i'),
355                        new Integer JavaDoc(current) });
356                 skipIdentifier();
357                 break;
358             }
359             current = reader.read();
360             if (current != 'c') {
361                 reportError("character.expected",
362                             new Object JavaDoc[] { new Character JavaDoc('c'),
363                            new Integer JavaDoc(current) });
364                 skipIdentifier();
365                 break;
366             }
367             current = reader.read();
368             if (current != 'e') {
369                 reportError("character.expected",
370                             new Object JavaDoc[] { new Character JavaDoc('e'),
371                        new Integer JavaDoc(current) });
372                 skipIdentifier();
373                 break;
374             }
375             preserveAspectRatioHandler.slice();
376             current = reader.read();
377             break;
378         default:
379             if (current != -1) {
380                 reportError("character.unexpected",
381                             new Object JavaDoc[] { new Integer JavaDoc(current) });
382                 skipIdentifier();
383             }
384         }
385
386     skipSpaces();
387     if (current != -1) {
388         reportError("end.of.stream.expected",
389             new Object JavaDoc[] { new Integer JavaDoc(current) });
390     }
391
392     preserveAspectRatioHandler.endPreserveAspectRatio();
393     }
394
395     /**
396      * Skips characters in the given reader until a white space is encountered.
397      * @return the first character after the space.
398      */

399     protected void skipIdentifier() throws IOException JavaDoc {
400     loop: for (;;) {
401         current = reader.read();
402         switch(current) {
403         case 0xD: case 0xA: case 0x20: case 0x9:
404         current = reader.read();
405         break loop;
406         default:
407         if (current == -1) {
408             break loop;
409         }
410         }
411     }
412     }
413 }
414
Popular Tags