BaseConfigurationXMLReader.java

  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. package org.apache.commons.configuration2;

  18. /**
  19.  * <p>
  20.  * A specialized SAX2 XML parser that processes configuration objects.
  21.  * </p>
  22.  *
  23.  * <p>
  24.  * This class mimics to be a SAX compliant XML parser. It is able to iterate over the keys in a configuration object and
  25.  * to generate corresponding SAX events. By registering a {@code ContentHandler} at an instance it is possible to
  26.  * perform XML processing on a configuration object.
  27.  * </p>
  28.  */
  29. public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
  30.     /**
  31.      * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events.
  32.      */
  33.     final class SAXConverter extends HierarchicalConfigurationConverter {
  34.         /**
  35.          * Callback for the end of an element.
  36.          *
  37.          * @param name the element name
  38.          */
  39.         @Override
  40.         protected void elementEnd(final String name) {
  41.             fireElementEnd(name);
  42.         }

  43.         /**
  44.          * Callback for the start of an element.
  45.          *
  46.          * @param name the element name
  47.          * @param value the element value
  48.          */
  49.         @Override
  50.         protected void elementStart(final String name, final Object value) {
  51.             fireElementStart(name, null);
  52.             if (value != null) {
  53.                 fireCharacters(value.toString());
  54.             }
  55.         }
  56.     }

  57.     /** Stores the actual configuration. */
  58.     private Configuration config;

  59.     /**
  60.      * Creates a new instance of {@code BaseConfigurationXMLReader}.
  61.      */
  62.     public BaseConfigurationXMLReader() {
  63.     }

  64.     /**
  65.      * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed.
  66.      *
  67.      * @param conf the configuration to be parsed
  68.      */
  69.     public BaseConfigurationXMLReader(final Configuration conf) {
  70.         this();
  71.         setConfiguration(conf);
  72.     }

  73.     /**
  74.      * Gets the actual configuration to be processed.
  75.      *
  76.      * @return the actual configuration
  77.      */
  78.     public Configuration getConfiguration() {
  79.         return config;
  80.     }

  81.     /**
  82.      * Gets the configuration to be processed.
  83.      *
  84.      * @return the actual configuration
  85.      */
  86.     @Override
  87.     public Configuration getParsedConfiguration() {
  88.         return getConfiguration();
  89.     }

  90.     /**
  91.      * The main SAX event generation method. This element uses an internal {@code HierarchicalConfigurationConverter} object
  92.      * to iterate over all keys in the actual configuration and to generate corresponding SAX events.
  93.      */
  94.     @Override
  95.     protected void processKeys() {
  96.         fireElementStart(getRootName(), null);
  97.         new SAXConverter().process(getConfiguration());
  98.         fireElementEnd(getRootName());
  99.     }

  100.     /**
  101.      * Sets the configuration to be processed.
  102.      *
  103.      * @param conf the configuration
  104.      */
  105.     public void setConfiguration(final Configuration conf) {
  106.         config = conf;
  107.     }
  108. }