    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.configuration2;
    019
    020/**
    021 * <p>
    022 * A specialized SAX2 XML parser that processes configuration objects.
    023 * </p>
    024 *
    025 * <p>
    026 * This class mimics to be a SAX compliant XML parser. It is able to iterate over the keys in a configuration object and
    027 * to generate corresponding SAX events. By registering a {@code ContentHandler} at an instance it is possible to
    028 * perform XML processing on a configuration object.
    029 * </p>
    030 *
    031 */
    032public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
    033    /** Stores the actual configuration. */
    034    private Configuration config;
    035
    036    /**
    037     * Creates a new instance of {@code BaseConfigurationXMLReader}.
    038     */
    039    public BaseConfigurationXMLReader() {
    040    }
    041
    042    /**
    043     * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed.
    044     *
    045     * @param conf the configuration to be parsed
    046     */
    047    public BaseConfigurationXMLReader(final Configuration conf) {
    048        this();
    049        setConfiguration(conf);
    050    }
    051
    052    /**
    053     * Returns the actual configuration to be processed.
    054     *
    055     * @return the actual configuration
    056     */
    057    public Configuration getConfiguration() {
    058        return config;
    059    }
    060
    061    /**
    062     * Sets the configuration to be processed.
    063     *
    064     * @param conf the configuration
    065     */
    066    public void setConfiguration(final Configuration conf) {
    067        config = conf;
    068    }
    069
    070    /**
    071     * Returns the configuration to be processed.
    072     *
    073     * @return the actual configuration
    074     */
    075    @Override
    076    public Configuration getParsedConfiguration() {
    077        return getConfiguration();
    078    }
    079
    080    /**
    081     * The main SAX event generation method. This element uses an internal {@code HierarchicalConfigurationConverter} object
    082     * to iterate over all keys in the actual configuration and to generate corresponding SAX events.
    083     */
    084    @Override
    085    protected void processKeys() {
    086        fireElementStart(getRootName(), null);
    087        new SAXConverter().process(getConfiguration());
    088        fireElementEnd(getRootName());
    089    }
    090
    091    /**
    092     * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events.
    093     *
    094     */
    095    class SAXConverter extends HierarchicalConfigurationConverter {
    096        /**
    097         * Callback for the start of an element.
    098         *
    099         * @param name the element name
    100         * @param value the element value
    101         */
    102        @Override
    103        protected void elementStart(final String name, final Object value) {
    104            fireElementStart(name, null);
    105            if (value != null) {
    106                fireCharacters(value.toString());
    107            }
    108        }
    109
    110        /**
    111         * Callback for the end of an element.
    112         *
    113         * @param name the element name
    114         */
    115        @Override
    116        protected void elementEnd(final String name) {
    117            fireElementEnd(name);
    118        }
    119    }
    120}