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    *     https://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.configuration2;
19  
20  /**
21   * <p>
22   * A specialized SAX2 XML parser that processes configuration objects.
23   * </p>
24   *
25   * <p>
26   * This class mimics to be a SAX compliant XML parser. It is able to iterate over the keys in a configuration object and
27   * to generate corresponding SAX events. By registering a {@code ContentHandler} at an instance it is possible to
28   * perform XML processing on a configuration object.
29   * </p>
30   */
31  public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
32  
33      /**
34       * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events.
35       */
36      final class SAXConverter extends HierarchicalConfigurationConverter {
37  
38          /**
39           * Callback for the end of an element.
40           *
41           * @param name the element name
42           */
43          @Override
44          protected void elementEnd(final String name) {
45              fireElementEnd(name);
46          }
47  
48          /**
49           * Callback for the start of an element.
50           *
51           * @param name the element name
52           * @param value the element value
53           */
54          @Override
55          protected void elementStart(final String name, final Object value) {
56              fireElementStart(name, null);
57              if (value != null) {
58                  fireCharacters(value.toString());
59              }
60          }
61      }
62  
63      /** Stores the actual configuration. */
64      private Configuration config;
65  
66      /**
67       * Creates a new instance of {@code BaseConfigurationXMLReader}.
68       */
69      public BaseConfigurationXMLReader() {
70      }
71  
72      /**
73       * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed.
74       *
75       * @param conf the configuration to be parsed
76       */
77      public BaseConfigurationXMLReader(final Configuration conf) {
78          this();
79          setConfiguration(conf);
80      }
81  
82      /**
83       * Gets the actual configuration to be processed.
84       *
85       * @return the actual configuration
86       */
87      public Configuration getConfiguration() {
88          return config;
89      }
90  
91      /**
92       * Gets the configuration to be processed.
93       *
94       * @return the actual configuration
95       */
96      @Override
97      public Configuration getParsedConfiguration() {
98          return getConfiguration();
99      }
100 
101     /**
102      * The main SAX event generation method. This element uses an internal {@code HierarchicalConfigurationConverter} object
103      * to iterate over all keys in the actual configuration and to generate corresponding SAX events.
104      */
105     @Override
106     protected void processKeys() {
107         fireElementStart(getRootName(), null);
108         new SAXConverter().process(getConfiguration());
109         fireElementEnd(getRootName());
110     }
111 
112     /**
113      * Sets the configuration to be processed.
114      *
115      * @param conf the configuration
116      */
117     public void setConfiguration(final Configuration conf) {
118         config = conf;
119     }
120 }