    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>A specialized SAX2 XML parser that processes configuration objects.</p>
    022 *
    023 * <p>This class mimics to be a SAX compliant XML parser. It is able to iterate
    024 * over the keys in a configuration object and to generate corresponding SAX
    025 * events. By registering a {@code ContentHandler} at an instance
    026 * it is possible to perform XML processing on a configuration object.</p>
    027 *
    028 */
    029public class BaseConfigurationXMLReader extends ConfigurationXMLReader
    030{
    031    /** Stores the actual configuration.*/
    032    private Configuration config;
    033
    034    /**
    035     * Creates a new instance of {@code BaseConfigurationXMLReader}.
    036     */
    037    public BaseConfigurationXMLReader()
    038    {
    039        super();
    040    }
    041
    042    /**
    043     * Creates a new instance of {@code BaseConfigurationXMLReader} and
    044     * sets the configuration object to be parsed.
    045     *
    046     * @param conf the configuration to be parsed
    047     */
    048    public BaseConfigurationXMLReader(final Configuration conf)
    049    {
    050        this();
    051        setConfiguration(conf);
    052    }
    053
    054    /**
    055     * Returns the actual configuration to be processed.
    056     *
    057     * @return the actual configuration
    058     */
    059    public Configuration getConfiguration()
    060    {
    061        return config;
    062    }
    063
    064    /**
    065     * Sets the configuration to be processed.
    066     *
    067     * @param conf the configuration
    068     */
    069    public void setConfiguration(final Configuration conf)
    070    {
    071        config = conf;
    072    }
    073
    074    /**
    075     * Returns the configuration to be processed.
    076     *
    077     * @return the actual configuration
    078     */
    079    @Override
    080    public Configuration getParsedConfiguration()
    081    {
    082        return getConfiguration();
    083    }
    084
    085    /**
    086     * The main SAX event generation method. This element uses an internal
    087     * {@code HierarchicalConfigurationConverter} object to iterate over
    088     * all keys in the actual configuration and to generate corresponding SAX
    089     * events.
    090     */
    091    @Override
    092    protected void processKeys()
    093    {
    094        fireElementStart(getRootName(), null);
    095        new SAXConverter().process(getConfiguration());
    096        fireElementEnd(getRootName());
    097    }
    098
    099    /**
    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        @Override
    113        protected void elementStart(final String name, final Object value)
    114        {
    115            fireElementStart(name, null);
    116            if (value != null)
    117            {
    118                fireCharacters(value.toString());
    119            }
    120        }
    121
    122        /**
    123         * Callback for the end of an element.
    124         *
    125         * @param name the element name
    126         */
    127        @Override
    128        protected void elementEnd(final String name)
    129        {
    130            fireElementEnd(name);
    131        }
    132    }
    133}