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