KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

35     protected AngleHandler angleHandler = DefaultAngleHandler.INSTANCE;
36
37     /**
38      * Allows an application to register an angle handler.
39      *
40      * <p>If the application does not register a handler, all
41      * events reported by the parser will be silently ignored.
42      *
43      * <p>Applications may register a new or different handler in the
44      * middle of a parse, and the parser must begin using the new
45      * handler immediately.</p>
46      * @param handler The transform list handler.
47      */

48     public void setAngleHandler(AngleHandler handler) {
49     angleHandler = handler;
50     }
51
52     /**
53      * Returns the angle handler in use.
54      */

55     public AngleHandler getAngleHandler() {
56     return angleHandler;
57     }
58
59     /**
60      * Parses the current reader representing an angle.
61      */

62     protected void doParse() throws ParseException, IOException JavaDoc {
63     angleHandler.startAngle();
64
65     current = reader.read();
66     skipSpaces();
67     
68     try {
69         float f = parseFloat();
70
71         angleHandler.angleValue(f);
72
73         s: if (current != -1) {
74         switch (current) {
75         case 0xD: case 0xA: case 0x20: case 0x9:
76             break s;
77         }
78         
79         switch (current) {
80         case 'd':
81             current = reader.read();
82             if (current != 'e') {
83             reportError("character.expected",
84                     new Object JavaDoc[] { new Character JavaDoc('e'),
85                            new Integer JavaDoc(current) });
86             break;
87             }
88             current = reader.read();
89             if (current != 'g') {
90             reportError("character.expected",
91                     new Object JavaDoc[] { new Character JavaDoc('g'),
92                            new Integer JavaDoc(current) });
93             break;
94             }
95             angleHandler.deg();
96             current = reader.read();
97             break;
98         case 'g':
99             current = reader.read();
100             if (current != 'r') {
101             reportError("character.expected",
102                     new Object JavaDoc[] { new Character JavaDoc('r'),
103                            new Integer JavaDoc(current) });
104             break;
105             }
106             current = reader.read();
107             if (current != 'a') {
108             reportError("character.expected",
109                     new Object JavaDoc[] { new Character JavaDoc('a'),
110                            new Integer JavaDoc(current) });
111             break;
112             }
113             current = reader.read();
114             if (current != 'd') {
115             reportError("character.expected",
116                     new Object JavaDoc[] { new Character JavaDoc('d'),
117                            new Integer JavaDoc(current) });
118             break;
119             }
120             angleHandler.grad();
121             current = reader.read();
122             break;
123         case 'r':
124             current = reader.read();
125             if (current != 'a') {
126             reportError("character.expected",
127                     new Object JavaDoc[] { new Character JavaDoc('a'),
128                            new Integer JavaDoc(current) });
129             break;
130             }
131             current = reader.read();
132             if (current != 'd') {
133             reportError("character.expected",
134                     new Object JavaDoc[] { new Character JavaDoc('d'),
135                            new Integer JavaDoc(current) });
136             break;
137             }
138             angleHandler.rad();
139             current = reader.read();
140             break;
141         default:
142             reportError("character.unexpected",
143                 new Object JavaDoc[] { new Integer JavaDoc(current) });
144         }
145         }
146
147         skipSpaces();
148         if (current != -1) {
149         reportError("end.of.stream.expected",
150                 new Object JavaDoc[] { new Integer JavaDoc(current) });
151         }
152     } catch (NumberFormatException JavaDoc e) {
153             reportError("character.unexpected",
154                         new Object JavaDoc[] { new Integer JavaDoc(current) });
155     }
156     angleHandler.endAngle();
157     }
158 }
159
Popular Tags