KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 public class PathParser extends NumberParser {
30
31     /**
32      * The path handler used to report parse events.
33      */

34     protected PathHandler pathHandler;
35
36     /**
37      * Creates a new PathParser.
38      */

39     public PathParser() {
40     pathHandler = DefaultPathHandler.INSTANCE;
41     }
42
43     /**
44      * Allows an application to register a path 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 setPathHandler(PathHandler handler) {
55     pathHandler = handler;
56     }
57
58     /**
59      * Returns the path handler in use.
60      */

61     public PathHandler getPathHandler() {
62     return pathHandler;
63     }
64
65     protected void doParse() throws ParseException, IOException JavaDoc {
66     pathHandler.startPath();
67
68     current = reader.read();
69     loop: for (;;) {
70             try {
71                 switch (current) {
72                 case 0xD:
73                 case 0xA:
74                 case 0x20:
75                 case 0x9:
76                     current = reader.read();
77                     break;
78                 case 'z':
79                 case 'Z':
80                     current = reader.read();
81                     pathHandler.closePath();
82                     break;
83                 case 'm':
84                     parsem();
85                 case 'l':
86                     parsel();
87                     break;
88                 case 'M':
89                     parseM();
90                 case 'L':
91                     parseL();
92                     break;
93                 case 'h':
94                     parseh();
95                     break;
96                 case 'H':
97                     parseH();
98                     break;
99                 case 'v':
100                     parsev();
101                     break;
102                 case 'V':
103                     parseV();
104                     break;
105                 case 'c':
106                     parsec();
107                     break;
108                 case 'C':
109                     parseC();
110                     break;
111                 case 'q':
112                     parseq();
113                     break;
114                 case 'Q':
115                     parseQ();
116                     break;
117                 case 's':
118                     parses();
119                     break;
120                 case 'S':
121                     parseS();
122                     break;
123                 case 't':
124                     parset();
125                     break;
126                 case 'T':
127                     parseT();
128                     break;
129                 case 'a':
130                     parsea();
131                     break;
132                 case 'A':
133                     parseA();
134                     break;
135                 case -1:
136                     break loop;
137                 default:
138                     reportError("character.unexpected",
139                                 new Object JavaDoc[] { new Integer JavaDoc(current) });
140                     skipSubPath();
141                 }
142         } catch (ParseException e) {
143                 errorHandler.error(e);
144                 skipSubPath();
145             }
146     }
147
148     skipSpaces();
149     if (current != -1) {
150         reportError("end.of.stream.expected",
151             new Object JavaDoc[] { new Integer JavaDoc(current) });
152     }
153
154     pathHandler.endPath();
155     }
156
157     /**
158      * Parses a 'm' command.
159      */

160     protected void parsem() throws ParseException, IOException JavaDoc {
161     current = reader.read();
162     skipSpaces();
163
164         float x = parseFloat();
165         skipCommaSpaces();
166         float y = parseFloat();
167
168         pathHandler.movetoRel(x, y);
169         skipCommaSpaces();
170     }
171
172     /**
173      * Parses a 'l' command.
174      */

175     protected void parsel() throws ParseException, IOException JavaDoc {
176     if (current == 'l') {
177         current = reader.read();
178     }
179     skipSpaces();
180         for (;;) {
181         switch (current) {
182         case '+': case '-': case '.':
183         case '0': case '1': case '2': case '3': case '4':
184         case '5': case '6': case '7': case '8': case '9':
185                 float x = parseFloat();
186                 skipCommaSpaces();
187                 float y = parseFloat();
188
189                 pathHandler.linetoRel(x, y);
190         break;
191         default:
192         return;
193         }
194         skipCommaSpaces();
195     }
196     }
197
198     /**
199      * Parses a 'M' command.
200      */

201     protected void parseM() throws ParseException, IOException JavaDoc {
202     current = reader.read();
203     skipSpaces();
204
205         float x = parseFloat();
206         skipCommaSpaces();
207         float y = parseFloat();
208         
209         pathHandler.movetoAbs(x, y);
210         skipCommaSpaces();
211     }
212
213     /**
214      * Parses a 'L' command.
215      */

216     protected void parseL() throws ParseException, IOException JavaDoc {
217     if (current == 'L') {
218         current = reader.read();
219     }
220     skipSpaces();
221         for (;;) {
222         switch (current) {
223         case '+': case '-': case '.':
224         case '0': case '1': case '2': case '3': case '4':
225         case '5': case '6': case '7': case '8': case '9':
226                 float x = parseFloat();
227                 skipCommaSpaces();
228                 float y = parseFloat();
229
230                 pathHandler.linetoAbs(x, y);
231                 break;
232         default:
233                 return;
234         }
235         skipCommaSpaces();
236     }
237     }
238
239     /**
240      * Parses a 'h' command.
241      */

242     protected void parseh() throws ParseException, IOException JavaDoc {
243     current = reader.read();
244     skipSpaces();
245
246     for (;;) {
247         switch (current) {
248         case '+': case '-': case '.':
249         case '0': case '1': case '2': case '3': case '4':
250         case '5': case '6': case '7': case '8': case '9':
251                 float x = parseFloat();
252                 pathHandler.linetoHorizontalRel(x);
253         break;
254         default:
255         return;
256         }
257         skipCommaSpaces();
258     }
259     }
260
261     /**
262      * Parses a 'H' command.
263      */

264     protected void parseH() throws ParseException, IOException JavaDoc {
265     current = reader.read();
266     skipSpaces();
267
268     for (;;) {
269         switch (current) {
270         case '+': case '-': case '.':
271         case '0': case '1': case '2': case '3': case '4':
272         case '5': case '6': case '7': case '8': case '9':
273                 float x = parseFloat();
274                 pathHandler.linetoHorizontalAbs(x);
275         break;
276         default:
277         return;
278         }
279         skipCommaSpaces();
280     }
281     }
282
283     /**
284      * Parses a 'v' command.
285      */

286     protected void parsev() throws ParseException, IOException JavaDoc {
287     current = reader.read();
288     skipSpaces();
289
290     for (;;) {
291         switch (current) {
292         case '+': case '-': case '.':
293         case '0': case '1': case '2': case '3': case '4':
294         case '5': case '6': case '7': case '8': case '9':
295                 float x = parseFloat();
296                 pathHandler.linetoVerticalRel(x);
297         break;
298         default:
299         return;
300         }
301         skipCommaSpaces();
302     }
303     }
304
305     /**
306      * Parses a 'V' command.
307      */

308     protected void parseV() throws ParseException, IOException JavaDoc {
309     current = reader.read();
310     skipSpaces();
311
312     for (;;) {
313         switch (current) {
314         case '+': case '-': case '.':
315         case '0': case '1': case '2': case '3': case '4':
316         case '5': case '6': case '7': case '8': case '9':
317                 float x = parseFloat();
318                 pathHandler.linetoVerticalAbs(x);
319         break;
320         default:
321         return;
322         }
323         skipCommaSpaces();
324     }
325     }
326
327     /**
328      * Parses a 'c' command.
329      */

330     protected void parsec() throws ParseException, IOException JavaDoc {
331     current = reader.read();
332     skipSpaces();
333     
334     for (;;) {
335         switch (current) {
336         default:
337         return;
338         case '+': case '-': case '.':
339         case '0': case '1': case '2': case '3': case '4':
340         case '5': case '6': case '7': case '8': case '9':
341         }
342
343             float x1 = parseFloat();
344             skipCommaSpaces();
345             float y1 = parseFloat();
346             skipCommaSpaces();
347             float x2 = parseFloat();
348             skipCommaSpaces();
349             float y2 = parseFloat();
350             skipCommaSpaces();
351             float x = parseFloat();
352             skipCommaSpaces();
353             float y = parseFloat();
354
355             pathHandler.curvetoCubicRel(x1, y1, x2, y2, x, y);
356         skipCommaSpaces();
357     }
358     }
359
360     /**
361      * Parses a 'C' command.
362      */

363     protected void parseC() throws ParseException, IOException JavaDoc {
364     current = reader.read();
365     skipSpaces();
366     
367     for (;;) {
368         switch (current) {
369         default:
370         return;
371         case '+': case '-': case '.':
372         case '0': case '1': case '2': case '3': case '4':
373         case '5': case '6': case '7': case '8': case '9':
374         }
375
376             float x1 = parseFloat();
377             skipCommaSpaces();
378             float y1 = parseFloat();
379             skipCommaSpaces();
380             float x2 = parseFloat();
381             skipCommaSpaces();
382             float y2 = parseFloat();
383             skipCommaSpaces();
384             float x = parseFloat();
385             skipCommaSpaces();
386             float y = parseFloat();
387
388             pathHandler.curvetoCubicAbs(x1, y1, x2, y2, x, y);
389         skipCommaSpaces();
390     }
391     }
392
393     /**
394      * Parses a 'q' command.
395      */

396     protected void parseq() throws ParseException, IOException JavaDoc {
397     current = reader.read();
398     skipSpaces();
399
400     for (;;) {
401         switch (current) {
402         default:
403         return;
404         case '+': case '-': case '.':
405         case '0': case '1': case '2': case '3': case '4':
406         case '5': case '6': case '7': case '8': case '9':
407         }
408
409             float x1 = parseFloat();
410             skipCommaSpaces();
411             float y1 = parseFloat();
412             skipCommaSpaces();
413             float x = parseFloat();
414             skipCommaSpaces();
415             float y = parseFloat();
416
417             pathHandler.curvetoQuadraticRel(x1, y1, x, y);
418         skipCommaSpaces();
419     }
420     }
421
422     /**
423      * Parses a 'Q' command.
424      */

425     protected void parseQ() throws ParseException, IOException JavaDoc {
426     current = reader.read();
427     skipSpaces();
428
429     for (;;) {
430         switch (current) {
431         default:
432         return;
433         case '+': case '-': case '.':
434         case '0': case '1': case '2': case '3': case '4':
435         case '5': case '6': case '7': case '8': case '9':
436         }
437
438             float x1 = parseFloat();
439             skipCommaSpaces();
440             float y1 = parseFloat();
441             skipCommaSpaces();
442             float x = parseFloat();
443             skipCommaSpaces();
444             float y = parseFloat();
445
446             pathHandler.curvetoQuadraticAbs(x1, y1, x, y);
447         skipCommaSpaces();
448     }
449     }
450
451     /**
452      * Parses a 's' command.
453      */

454     protected void parses() throws ParseException, IOException JavaDoc {
455     current = reader.read();
456     skipSpaces();
457
458     for (;;) {
459         switch (current) {
460         default:
461         return;
462         case '+': case '-': case '.':
463         case '0': case '1': case '2': case '3': case '4':
464         case '5': case '6': case '7': case '8': case '9':
465         }
466         
467             float x2 = parseFloat();
468             skipCommaSpaces();
469             float y2 = parseFloat();
470             skipCommaSpaces();
471             float x = parseFloat();
472             skipCommaSpaces();
473             float y = parseFloat();
474
475             pathHandler.curvetoCubicSmoothRel(x2, y2, x, y);
476         skipCommaSpaces();
477     }
478     }
479
480     /**
481      * Parses a 'S' command.
482      */

483     protected void parseS() throws ParseException, IOException JavaDoc {
484     current = reader.read();
485     skipSpaces();
486
487     for (;;) {
488         switch (current) {
489         default:
490         return;
491         case '+': case '-': case '.':
492         case '0': case '1': case '2': case '3': case '4':
493         case '5': case '6': case '7': case '8': case '9':
494         }
495         
496             float x2 = parseFloat();
497             skipCommaSpaces();
498             float y2 = parseFloat();
499             skipCommaSpaces();
500             float x = parseFloat();
501             skipCommaSpaces();
502             float y = parseFloat();
503
504             pathHandler.curvetoCubicSmoothAbs(x2, y2, x, y);
505         skipCommaSpaces();
506     }
507     }
508
509     /**
510      * Parses a 't' command.
511      */

512     protected void parset() throws ParseException, IOException JavaDoc {
513     current = reader.read();
514     skipSpaces();
515
516     for (;;) {
517         switch (current) {
518         default:
519         return;
520         case '+': case '-': case '.':
521         case '0': case '1': case '2': case '3': case '4':
522         case '5': case '6': case '7': case '8': case '9':
523         }
524
525             float x = parseFloat();
526             skipCommaSpaces();
527             float y = parseFloat();
528             
529             pathHandler.curvetoQuadraticSmoothRel(x, y);
530         skipCommaSpaces();
531     }
532     }
533
534     /**
535      * Parses a 'T' command.
536      */

537     protected void parseT() throws ParseException, IOException JavaDoc {
538     current = reader.read();
539     skipSpaces();
540
541     for (;;) {
542         switch (current) {
543         default:
544         return;
545         case '+': case '-': case '.':
546         case '0': case '1': case '2': case '3': case '4':
547         case '5': case '6': case '7': case '8': case '9':
548         }
549
550             float x = parseFloat();
551             skipCommaSpaces();
552             float y = parseFloat();
553
554             pathHandler.curvetoQuadraticSmoothAbs(x, y);
555         skipCommaSpaces();
556     }
557     }
558
559     /**
560      * Parses a 'a' command.
561      */

562     protected void parsea() throws ParseException, IOException JavaDoc {
563     current = reader.read();
564     skipSpaces();
565
566     for (;;) {
567         switch (current) {
568         default:
569         return;
570         case '+': case '-': case '.':
571         case '0': case '1': case '2': case '3': case '4':
572         case '5': case '6': case '7': case '8': case '9':
573         }
574
575             float rx = parseFloat();
576             skipCommaSpaces();
577             float ry = parseFloat();
578             skipCommaSpaces();
579             float ax = parseFloat();
580             skipCommaSpaces();
581         
582             boolean laf;
583             switch (current) {
584             case '0':
585                 laf = false;
586                 break;
587             case '1':
588                 laf = true;
589                 break;
590             default:
591                 reportError("character.unexpected",
592                             new Object JavaDoc[] { new Integer JavaDoc(current) });
593                 skipSubPath();
594                 return;
595             }
596
597             current = reader.read();
598             skipCommaSpaces();
599
600             boolean sf;
601             switch (current) {
602             case '0':
603                 sf = false;
604                 break;
605             case '1':
606                 sf = true;
607                 break;
608             default:
609                 reportError("character.unexpected",
610                             new Object JavaDoc[] { new Integer JavaDoc(current) });
611                 skipSubPath();
612                 return;
613             }
614
615             current = reader.read();
616             skipCommaSpaces();
617
618             float x = parseFloat();
619             skipCommaSpaces();
620             float y = parseFloat();
621
622             pathHandler.arcRel(rx, ry, ax, laf, sf, x, y);
623         skipCommaSpaces();
624     }
625     }
626
627     /**
628      * Parses a 'A' command.
629      */

630     protected void parseA() throws ParseException, IOException JavaDoc {
631     current = reader.read();
632     skipSpaces();
633
634     for (;;) {
635         switch (current) {
636         default:
637         return;
638         case '+': case '-': case '.':
639         case '0': case '1': case '2': case '3': case '4':
640         case '5': case '6': case '7': case '8': case '9':
641         }
642
643             float rx = parseFloat();
644             skipCommaSpaces();
645             float ry = parseFloat();
646             skipCommaSpaces();
647             float ax = parseFloat();
648             skipCommaSpaces();
649             
650             boolean laf;
651             switch (current) {
652             case '0':
653                 laf = false;
654                 break;
655             case '1':
656                 laf = true;
657                 break;
658             default:
659                 reportError("character.unexpected",
660                             new Object JavaDoc[] { new Integer JavaDoc(current) });
661                 skipSubPath();
662                 return;
663             }
664             
665             current = reader.read();
666             skipCommaSpaces();
667
668             boolean sf;
669             switch (current) {
670             case '0':
671                 sf = false;
672                 break;
673             case '1':
674                 sf = true;
675                 break;
676             default:
677                 reportError("character.unexpected",
678                             new Object JavaDoc[] { new Integer JavaDoc(current) });
679                 skipSubPath();
680                 return;
681             }
682
683             current = reader.read();
684             skipCommaSpaces();
685             float x = parseFloat();
686             skipCommaSpaces();
687             float y = parseFloat();
688
689             pathHandler.arcAbs(rx, ry, ax, laf, sf, x, y);
690         skipCommaSpaces();
691     }
692     }
693
694     /**
695      * Skips a sub-path.
696      */

697     protected void skipSubPath() throws ParseException, IOException JavaDoc {
698     for (;;) {
699         switch (current) {
700         case 'm':
701         case 'M':
702         return;
703         default:
704         if (current == -1) {
705             return;
706         }
707         current = reader.read();
708         }
709     }
710     }
711 }
712
Popular Tags