2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.configuration;
20
import java.io.IOException;
22
import org.xml.sax.Attributes;
23
import org.xml.sax.ContentHandler;
24
import org.xml.sax.DTDHandler;
25
import org.xml.sax.EntityResolver;
26
import org.xml.sax.ErrorHandler;
27
import org.xml.sax.InputSource;
28
import org.xml.sax.SAXException;
29
import org.xml.sax.XMLReader;
30
import org.xml.sax.helpers.AttributesImpl;
33
* <p>A base class for "faked" <code>XMLReader</code> classes
34
* that transform a configuration object in a set of SAX parsing events.</p>
35
* <p>This class provides dummy implementations for most of the methods
36
* defined in the <code>XMLReader</code> interface that are not used for this
37
* special purpose. There will be concrete sub classes that process specific
38
* configuration classes.</p>
40
* @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
41
* @version $Id: ConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
43
public abstract class ConfigurationXMLReader implements XMLReader
45
/** Constant for the namespace URI.*/
46
protected static final String NS_URI = "";
48
/** Constant for the default name of the root element.*/
49
private static final String DEFAULT_ROOT_NAME = "config";
51
/** An empty attributes object.*/
52
private static final Attributes EMPTY_ATTRS = new AttributesImpl();
54
/** Stores the content handler.*/
55
private ContentHandler contentHandler;
57
/** Stores an exception that occurred during parsing.*/
58
private SAXException exception;
60
/** Stores the name for the root element.*/
61
private String rootName;
64
* Creates a new instance of <code>ConfigurationXMLReader</code>.
66
protected ConfigurationXMLReader()
69
setRootName(DEFAULT_ROOT_NAME);
73
* Parses the acutal configuration object. The passed system ID will be
76
* @param systemId the system ID (ignored)
77
* @throws IOException if no configuration was specified
78
* @throws SAXException if an error occurs during parsing
80
public void parse(String systemId) throws IOException, SAXException
86
* Parses the acutal configuration object. The passed input source will be
89
* @param input the input source (ignored)
90
* @throws IOException if no configuration was specified
91
* @throws SAXException if an error occurs during parsing
93
public void parse(InputSource input) throws IOException, SAXException
99
* Dummy implementation of the interface method.
101
* @param name the name of the feature
102
* @return always <b>false</b> (no features are supported)
104
public boolean getFeature(String name)
110
* Dummy implementation of the interface method.
112
* @param name the name of the feature to be set
113
* @param value the value of the feature
115
public void setFeature(String name, boolean value)
120
* Returns the actually set content handler.
122
* @return the content handler
124
public ContentHandler getContentHandler()
126
return contentHandler;
130
* Sets the content handler. The object specified here will receive SAX
131
* events during parsing.
133
* @param handler the content handler
135
public void setContentHandler(ContentHandler handler)
137
contentHandler = handler;
141
* Returns the DTD handler. This class does not support DTD handlers,
142
* so this method always returns <b>null</b>.
144
* @return the DTD handler
146
public DTDHandler getDTDHandler()
152
* Sets the DTD handler. The passed value is ignored.
154
* @param handler the handler to be set
156
public void setDTDHandler(DTDHandler handler)
161
* Returns the entity resolver. This class does not support an entity
162
* resolver, so this method always returns <b>null</b>.
164
* @return the entity resolver
166
public EntityResolver getEntityResolver()
172
* Sets the entity resolver. The passed value is ignored.
174
* @param resolver the entity resolver
176
public void setEntityResolver(EntityResolver resolver)
181
* Returns the error handler. This class does not support an error handler,
182
* so this method always returns <b>null</b>.
184
* @return the error handler
186
public ErrorHandler getErrorHandler()
192
* Sets the error handler. The passed value is ignored.
194
* @param handler the error handler
196
public void setErrorHandler(ErrorHandler handler)
201
* Dummy implementation of the interface method. No properties are
202
* supported, so this method always returns <b>null</b>.
204
* @param name the name of the requested property
205
* @return the property value
207
public Object getProperty(String name)
213
* Dummy implementation of the interface method. No properties are
214
* supported, so a call of this method just has no effect.
216
* @param name the property name
217
* @param value the property value
219
public void setProperty(String name, Object value)
224
* Returns the name to be used for the root element.
226
* @return the name for the root element
228
public String getRootName()
234
* Sets the name for the root element.
236
* @param string the name for the root element.
238
public void setRootName(String string)
244
* Fires a SAX element start event.
246
* @param name the name of the actual element
247
* @param attribs the attributes of this element (can be <b>null</b>)
249
protected void fireElementStart(String name, Attributes attribs)
251
if (getException() == null)
255
Attributes at = (attribs == null) ? EMPTY_ATTRS : attribs;
256
getContentHandler().startElement(NS_URI, name, name, at);
258
catch (SAXException ex)
266
* Fires a SAX element end event.
268
* @param name the name of the affected element
270
protected void fireElementEnd(String name)
272
if (getException() == null)
276
getContentHandler().endElement(NS_URI, name, name);
278
catch (SAXException ex)
286
* Fires a SAX characters event.
288
* @param text the text
290
protected void fireCharacters(String text)
292
if (getException() == null)
296
char[] ch = text.toCharArray();
297
getContentHandler().characters(ch, 0, ch.length);
299
catch (SAXException ex)
307
* Returns a reference to an exception that occurred during parsing.
309
* @return a SAXExcpetion or <b>null</b> if none occurred
311
public SAXException getException()
317
* Parses the configuration object and generates SAX events. This is the
318
* main processing method.
320
* @throws IOException if no configuration has been specified
321
* @throws SAXException if an error occurs during parsing
323
protected void parseConfiguration() throws IOException, SAXException
325
if (getParsedConfiguration() == null)
327
throw new IOException("No configuration specified!");
330
if (getContentHandler() != null)
333
getContentHandler().startDocument();
335
if (getException() != null)
337
throw getException();
339
getContentHandler().endDocument();
344
* Returns a reference to the configuration that is parsed by this object.
346
* @return the parsed configuration
348
public abstract Configuration getParsedConfiguration();
351
* Processes all keys stored in the actual configuration. This method is
352
* called by <code>parseConfiguration()</code> to start the main parsing
353
* process. <code>parseConfiguration()</code> calls the content handler's
354
* <code>startDocument()</code> and <code>endElement()</code> methods
355
* and cares for exception handling. The remaining actions are left to this
356
* method that must be implemented in a concrete sub class.
358
* @throws IOException if an IO error occurs
359
* @throws SAXException if a SAX error occurs
361
protected abstract void processKeys() throws IOException, SAXException;