View Javadoc

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