1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 /***
21 * <p>A specialized SAX2 XML parser that processes configuration objects.</p>
22 *
23 * <p>This class mimics to be a SAX compliant XML parser. It is able to iterate
24 * over the keys in a configuration object and to generate corresponding SAX
25 * events. By registering a <code>ContentHandler</code> at an instance
26 * it is possible to perform XML processing on a configuration object.</p>
27 *
28 * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
29 * @version $Id: BaseConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
30 */
31 public class BaseConfigurationXMLReader extends ConfigurationXMLReader
32 {
33 /*** Stores the actual configuration.*/
34 private Configuration config;
35
36 /***
37 * Creates a new instance of <code>BaseConfigurationXMLReader</code>.
38 */
39 public BaseConfigurationXMLReader()
40 {
41 super();
42 }
43
44 /***
45 * Creates a new instance of <code>BaseConfigurationXMLReader</code> and
46 * sets the configuration object to be parsed.
47 *
48 * @param conf the configuration to be parsed
49 */
50 public BaseConfigurationXMLReader(Configuration conf)
51 {
52 this();
53 setConfiguration(conf);
54 }
55
56 /***
57 * Returns the actual configuration to be processed.
58 *
59 * @return the actual configuration
60 */
61 public Configuration getConfiguration()
62 {
63 return config;
64 }
65
66 /***
67 * Sets the configuration to be processed.
68 *
69 * @param conf the configuration
70 */
71 public void setConfiguration(Configuration conf)
72 {
73 config = conf;
74 }
75
76 /***
77 * Returns the configuration to be processed.
78 *
79 * @return the actual configuration
80 */
81 public Configuration getParsedConfiguration()
82 {
83 return getConfiguration();
84 }
85
86 /***
87 * The main SAX event generation method. This element uses an internal
88 * <code>HierarchicalConfigurationConverter</code> object to iterate over
89 * all keys in the actual configuration and to generate corresponding SAX
90 * events.
91 */
92 protected void processKeys()
93 {
94 fireElementStart(getRootName(), null);
95 new SAXConverter().process(getConfiguration());
96 fireElementEnd(getRootName());
97 }
98
99 /***
100 * An internally used helper class to iterate over all configuration keys
101 * ant to generate corresponding SAX events.
102 *
103 */
104 class SAXConverter extends HierarchicalConfigurationConverter
105 {
106 /***
107 * Callback for the start of an element.
108 *
109 * @param name the element name
110 * @param value the element value
111 */
112 protected void elementStart(String name, Object value)
113 {
114 fireElementStart(name, null);
115 if (value != null)
116 {
117 fireCharacters(value.toString());
118 }
119 }
120
121 /***
122 * Callback for the end of an element.
123 *
124 * @param name the element name
125 */
126 protected void elementEnd(String name)
127 {
128 fireElementEnd(name);
129 }
130 }
131 }