KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.InputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.MissingResourceException JavaDoc;
25
26 import org.apache.batik.i18n.LocalizableSupport;
27 import org.apache.batik.util.io.NormalizingReader;
28 import org.apache.batik.util.io.StreamNormalizingReader;
29 import org.apache.batik.util.io.StringNormalizingReader;
30
31 /**
32  * This class is the superclass of all parsers. It provides localization
33  * and error handling methods.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: AbstractParser.java,v 1.10 2004/08/18 07:14:45 vhardy Exp $
37  */

38 public abstract class AbstractParser implements Parser {
39
40     /**
41      * The default resource bundle base name.
42      */

43     public final static String JavaDoc BUNDLE_CLASSNAME =
44     "org.apache.batik.parser.resources.Messages";
45
46     /**
47      * The error handler.
48      */

49     protected ErrorHandler errorHandler = new DefaultErrorHandler();
50
51     /**
52      * The localizable support.
53      */

54     protected LocalizableSupport localizableSupport =
55         new LocalizableSupport(BUNDLE_CLASSNAME,
56                                AbstractParser.class.getClassLoader());
57
58     /**
59      * The normalizing reader.
60      */

61     protected NormalizingReader reader;
62
63     /**
64      * The current character.
65      */

66     protected int current;
67
68     /**
69      * Returns the current character value.
70      */

71     public int getCurrent() {
72     return current;
73     }
74
75     /**
76      * Implements {@link org.apache.batik.i18n.Localizable#setLocale(Locale)}.
77      */

78     public void setLocale(Locale JavaDoc l) {
79     localizableSupport.setLocale(l);
80     }
81
82     /**
83      * Implements {@link org.apache.batik.i18n.Localizable#getLocale()}.
84      */

85     public Locale JavaDoc getLocale() {
86         return localizableSupport.getLocale();
87     }
88
89     /**
90      * Implements {@link
91      * org.apache.batik.i18n.Localizable#formatMessage(String,Object[])}.
92      */

93     public String JavaDoc formatMessage(String JavaDoc key, Object JavaDoc[] args)
94         throws MissingResourceException JavaDoc {
95         return localizableSupport.formatMessage(key, args);
96     }
97
98     /**
99      * Allow an application to register an error event handler.
100      *
101      * <p>If the application does not register an error event handler,
102      * all error events reported by the parser will cause an exception
103      * to be thrown.
104 ` *
105      * <p>Applications may register a new or different handler in the
106      * middle of a parse, and the parser must begin using the new
107      * handler immediately.</p>
108      * @param handler The error handler.
109      */

110     public void setErrorHandler(ErrorHandler handler) {
111     errorHandler = handler;
112     }
113
114     /**
115      * Parses the given reader
116      */

117     public void parse(Reader JavaDoc r) throws ParseException {
118         try {
119             reader = new StreamNormalizingReader(r);
120             doParse();
121         } catch (IOException JavaDoc e) {
122             errorHandler.error
123                 (new ParseException
124                  (createErrorMessage("io.exception", null), e));
125         }
126     }
127
128     /**
129      * Parses the given input stream. If the encoding is null,
130      * ISO-8859-1 is used.
131      */

132     public void parse(InputStream JavaDoc is, String JavaDoc enc) throws ParseException {
133         try {
134             reader = new StreamNormalizingReader(is, enc);
135             doParse();
136         } catch (IOException JavaDoc e) {
137             errorHandler.error
138                 (new ParseException
139                  (createErrorMessage("io.exception", null), e));
140         }
141     }
142
143     /**
144      * Parses the given string.
145      */

146     public void parse(String JavaDoc s) throws ParseException {
147         try {
148             reader = new StringNormalizingReader(s);
149             doParse();
150         } catch (IOException JavaDoc e) {
151             errorHandler.error
152                 (new ParseException
153                  (createErrorMessage("io.exception", null), e));
154         }
155     }
156
157     /**
158      * Method responsible for actually parsing data after AbstractParser
159      * has initialized itself.
160      */

161     protected abstract void doParse()
162         throws ParseException, IOException JavaDoc;
163
164     /**
165      * Signals an error to the error handler.
166      * @param key The message key in the resource bundle.
167      * @param args The message arguments.
168      */

169     protected void reportError(String JavaDoc key, Object JavaDoc[] args)
170         throws ParseException {
171         errorHandler.error(new ParseException(createErrorMessage(key, args),
172                                               reader.getLine(),
173                                               reader.getColumn()));
174     }
175
176     /**
177      * Returns a localized error message.
178      * @param key The message key in the resource bundle.
179      * @param args The message arguments.
180      */

181     protected String JavaDoc createErrorMessage(String JavaDoc key, Object JavaDoc[] args) {
182         try {
183             return formatMessage(key, args);
184         } catch (MissingResourceException JavaDoc e) {
185             return key;
186         }
187     }
188
189     /**
190      * Returns the resource bundle base name.
191      * @return BUNDLE_CLASSNAME.
192      */

193     protected String JavaDoc getBundleClassName() {
194         return BUNDLE_CLASSNAME;
195     }
196
197     /**
198      * Skips the whitespaces in the current reader.
199      */

200     protected void skipSpaces() throws IOException JavaDoc {
201         for (;;) {
202             switch (current) {
203             default:
204                 return;
205             case 0x20:
206             case 0x09:
207             case 0x0D:
208             case 0x0A:
209             }
210             current = reader.read();
211         }
212     }
213
214     /**
215      * Skips the whitespaces and an optional comma.
216      */

217     protected void skipCommaSpaces() throws IOException JavaDoc {
218         wsp1: for (;;) {
219         switch (current) {
220         default:
221         break wsp1;
222         case 0x20:
223         case 0x9:
224         case 0xD:
225         case 0xA:
226         }
227         current = reader.read();
228     }
229     if (current == ',') {
230             wsp2: for (;;) {
231         switch (current = reader.read()) {
232         default:
233             break wsp2;
234         case 0x20:
235         case 0x9:
236         case 0xD:
237         case 0xA:
238         }
239         }
240     }
241     }
242 }
243
Popular Tags