1 18 package org.apache.batik.parser; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 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 38 public abstract class AbstractParser implements Parser { 39 40 43 public final static String BUNDLE_CLASSNAME = 44 "org.apache.batik.parser.resources.Messages"; 45 46 49 protected ErrorHandler errorHandler = new DefaultErrorHandler(); 50 51 54 protected LocalizableSupport localizableSupport = 55 new LocalizableSupport(BUNDLE_CLASSNAME, 56 AbstractParser.class.getClassLoader()); 57 58 61 protected NormalizingReader reader; 62 63 66 protected int current; 67 68 71 public int getCurrent() { 72 return current; 73 } 74 75 78 public void setLocale(Locale l) { 79 localizableSupport.setLocale(l); 80 } 81 82 85 public Locale getLocale() { 86 return localizableSupport.getLocale(); 87 } 88 89 93 public String formatMessage(String key, Object [] args) 94 throws MissingResourceException { 95 return localizableSupport.formatMessage(key, args); 96 } 97 98 110 public void setErrorHandler(ErrorHandler handler) { 111 errorHandler = handler; 112 } 113 114 117 public void parse(Reader r) throws ParseException { 118 try { 119 reader = new StreamNormalizingReader(r); 120 doParse(); 121 } catch (IOException e) { 122 errorHandler.error 123 (new ParseException 124 (createErrorMessage("io.exception", null), e)); 125 } 126 } 127 128 132 public void parse(InputStream is, String enc) throws ParseException { 133 try { 134 reader = new StreamNormalizingReader(is, enc); 135 doParse(); 136 } catch (IOException e) { 137 errorHandler.error 138 (new ParseException 139 (createErrorMessage("io.exception", null), e)); 140 } 141 } 142 143 146 public void parse(String s) throws ParseException { 147 try { 148 reader = new StringNormalizingReader(s); 149 doParse(); 150 } catch (IOException e) { 151 errorHandler.error 152 (new ParseException 153 (createErrorMessage("io.exception", null), e)); 154 } 155 } 156 157 161 protected abstract void doParse() 162 throws ParseException, IOException ; 163 164 169 protected void reportError(String key, Object [] args) 170 throws ParseException { 171 errorHandler.error(new ParseException(createErrorMessage(key, args), 172 reader.getLine(), 173 reader.getColumn())); 174 } 175 176 181 protected String createErrorMessage(String key, Object [] args) { 182 try { 183 return formatMessage(key, args); 184 } catch (MissingResourceException e) { 185 return key; 186 } 187 } 188 189 193 protected String getBundleClassName() { 194 return BUNDLE_CLASSNAME; 195 } 196 197 200 protected void skipSpaces() throws IOException { 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 217 protected void skipCommaSpaces() throws IOException { 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 |