1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.IOException ; 58 59 import org.xml.sax.Attributes ; 60 import org.xml.sax.ContentHandler ; 61 import org.xml.sax.DTDHandler ; 62 import org.xml.sax.EntityResolver ; 63 import org.xml.sax.ErrorHandler ; 64 import org.xml.sax.InputSource ; 65 import org.xml.sax.SAXException ; 66 import org.xml.sax.XMLReader ; 67 import org.xml.sax.helpers.AttributesImpl ; 68 69 80 public abstract class ConfigurationXMLReader implements XMLReader 81 { 82 83 protected static final String NS_URI = ""; 84 85 86 private static final String DEFAULT_ROOT_NAME = "config"; 87 88 89 private static final Attributes EMPTY_ATTRS = new AttributesImpl (); 90 91 92 private ContentHandler contentHandler; 93 94 95 private SAXException exception; 96 97 98 private String rootName; 99 100 103 protected ConfigurationXMLReader() 104 { 105 super(); 106 setRootName(DEFAULT_ROOT_NAME); 107 } 108 109 116 public void parse(String systemId) throws IOException , SAXException 117 { 118 parseConfiguration(); 119 } 120 121 128 public void parse(InputSource input) throws IOException , SAXException 129 { 130 parseConfiguration(); 131 } 132 133 138 public boolean getFeature(String name) 139 { 140 return false; 141 } 142 143 148 public void setFeature(String name, boolean value) 149 { 150 } 151 152 156 public ContentHandler getContentHandler() 157 { 158 return contentHandler; 159 } 160 161 166 public void setContentHandler(ContentHandler handler) 167 { 168 contentHandler = handler; 169 } 170 171 176 public DTDHandler getDTDHandler() 177 { 178 return null; 179 } 180 181 185 public void setDTDHandler(DTDHandler handler) 186 { 187 } 188 189 194 public EntityResolver getEntityResolver() 195 { 196 return null; 197 } 198 199 203 public void setEntityResolver(EntityResolver resolver) 204 { 205 } 206 207 212 public ErrorHandler getErrorHandler() 213 { 214 return null; 215 } 216 217 221 public void setErrorHandler(ErrorHandler handler) 222 { 223 } 224 225 231 public Object getProperty(String name) 232 { 233 return null; 234 } 235 236 242 public void setProperty(String name, Object value) 243 { 244 } 245 246 250 public String getRootName() 251 { 252 return rootName; 253 } 254 255 259 public void setRootName(String string) 260 { 261 rootName = string; 262 } 263 264 269 protected void fireElementStart(String name, Attributes attribs) 270 { 271 if (getException() == null) 272 { 273 try 274 { 275 Attributes at = (attribs == null) ? EMPTY_ATTRS : attribs; 276 getContentHandler().startElement(NS_URI, name, name, at); 277 } 278 catch (SAXException ex) 279 { 280 exception = ex; 281 } 282 } 283 } 284 285 289 protected void fireElementEnd(String name) 290 { 291 if (getException() == null) 292 { 293 try 294 { 295 getContentHandler().endElement(NS_URI, name, name); 296 } 297 catch (SAXException ex) 298 { 299 exception = ex; 300 } 301 } 302 } 303 304 308 protected void fireCharacters(String text) 309 { 310 if (getException() == null) 311 { 312 try 313 { 314 char[] ch = text.toCharArray(); 315 getContentHandler().characters(ch, 0, ch.length); 316 } 317 catch (SAXException ex) 318 { 319 exception = ex; 320 } 321 } 322 } 323 324 328 public SAXException getException() 329 { 330 return exception; 331 } 332 333 339 protected void parseConfiguration() throws IOException , SAXException 340 { 341 if (getParsedConfiguration() == null) 342 { 343 throw new IOException ("No configuration specified!"); 344 } 345 346 if (getContentHandler() != null) 347 { 348 exception = null; 349 getContentHandler().startDocument(); 350 processKeys(); 351 if (getException() != null) 352 { 353 throw getException(); 354 } 355 getContentHandler().endDocument(); 356 } 357 } 358 359 363 public abstract Configuration getParsedConfiguration(); 364 365 375 protected abstract void processKeys() throws IOException , SAXException ; 376 } 377 | Popular Tags |