1 16 17 package org.apache.commons.configuration; 18 19 import java.io.IOException ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.DTDHandler ; 24 import org.xml.sax.EntityResolver ; 25 import org.xml.sax.ErrorHandler ; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.XMLReader ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 42 public abstract class ConfigurationXMLReader implements XMLReader 43 { 44 45 protected static final String NS_URI = ""; 46 47 48 private static final String DEFAULT_ROOT_NAME = "config"; 49 50 51 private static final Attributes EMPTY_ATTRS = new AttributesImpl (); 52 53 54 private ContentHandler contentHandler; 55 56 57 private SAXException exception; 58 59 60 private String rootName; 61 62 65 protected ConfigurationXMLReader() 66 { 67 super(); 68 setRootName(DEFAULT_ROOT_NAME); 69 } 70 71 79 public void parse(String systemId) throws IOException , SAXException 80 { 81 parseConfiguration(); 82 } 83 84 92 public void parse(InputSource input) throws IOException , SAXException 93 { 94 parseConfiguration(); 95 } 96 97 103 public boolean getFeature(String name) 104 { 105 return false; 106 } 107 108 114 public void setFeature(String name, boolean value) 115 { 116 } 117 118 123 public ContentHandler getContentHandler() 124 { 125 return contentHandler; 126 } 127 128 134 public void setContentHandler(ContentHandler handler) 135 { 136 contentHandler = handler; 137 } 138 139 145 public DTDHandler getDTDHandler() 146 { 147 return null; 148 } 149 150 155 public void setDTDHandler(DTDHandler handler) 156 { 157 } 158 159 165 public EntityResolver getEntityResolver() 166 { 167 return null; 168 } 169 170 175 public void setEntityResolver(EntityResolver resolver) 176 { 177 } 178 179 185 public ErrorHandler getErrorHandler() 186 { 187 return null; 188 } 189 190 195 public void setErrorHandler(ErrorHandler handler) 196 { 197 } 198 199 206 public Object getProperty(String name) 207 { 208 return null; 209 } 210 211 218 public void setProperty(String name, Object value) 219 { 220 } 221 222 227 public String getRootName() 228 { 229 return rootName; 230 } 231 232 237 public void setRootName(String string) 238 { 239 rootName = string; 240 } 241 242 248 protected void fireElementStart(String name, Attributes attribs) 249 { 250 if (getException() == null) 251 { 252 try 253 { 254 Attributes at = (attribs == null) ? EMPTY_ATTRS : attribs; 255 getContentHandler().startElement(NS_URI, name, name, at); 256 } 257 catch (SAXException ex) 258 { 259 exception = ex; 260 } 261 } 262 } 263 264 269 protected void fireElementEnd(String name) 270 { 271 if (getException() == null) 272 { 273 try 274 { 275 getContentHandler().endElement(NS_URI, name, name); 276 } 277 catch (SAXException ex) 278 { 279 exception = ex; 280 } 281 } 282 } 283 284 289 protected void fireCharacters(String text) 290 { 291 if (getException() == null) 292 { 293 try 294 { 295 char[] ch = text.toCharArray(); 296 getContentHandler().characters(ch, 0, ch.length); 297 } 298 catch (SAXException ex) 299 { 300 exception = ex; 301 } 302 } 303 } 304 305 310 public SAXException getException() 311 { 312 return exception; 313 } 314 315 322 protected void parseConfiguration() throws IOException , SAXException 323 { 324 if (getParsedConfiguration() == null) 325 { 326 throw new IOException ("No configuration specified!"); 327 } 328 329 if (getContentHandler() != null) 330 { 331 exception = null; 332 getContentHandler().startDocument(); 333 processKeys(); 334 if (getException() != null) 335 { 336 throw getException(); 337 } 338 getContentHandler().endDocument(); 339 } 340 } 341 342 347 public abstract Configuration getParsedConfiguration(); 348 349 360 protected abstract void processKeys() throws IOException , SAXException ; 361 } 362 | Popular Tags |