View Javadoc

1   /*
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
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
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.
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 }