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 */
031public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
032    /**
033     * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events.
034     */
035    final class SAXConverter extends HierarchicalConfigurationConverter {
036        /**
037         * Callback for the end of an element.
038         *
039         * @param name the element name
040         */
041        @Override
042        protected void elementEnd(final String name) {
043            fireElementEnd(name);
044        }
045
046        /**
047         * Callback for the start of an element.
048         *
049         * @param name the element name
050         * @param value the element value
051         */
052        @Override
053        protected void elementStart(final String name, final Object value) {
054            fireElementStart(name, null);
055            if (value != null) {
056                fireCharacters(value.toString());
057            }
058        }
059    }
060
061    /** Stores the actual configuration. */
062    private Configuration config;
063
064    /**
065     * Creates a new instance of {@code BaseConfigurationXMLReader}.
066     */
067    public BaseConfigurationXMLReader() {
068    }
069
070    /**
071     * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed.
072     *
073     * @param conf the configuration to be parsed
074     */
075    public BaseConfigurationXMLReader(final Configuration conf) {
076        this();
077        setConfiguration(conf);
078    }
079
080    /**
081     * Gets the actual configuration to be processed.
082     *
083     * @return the actual configuration
084     */
085    public Configuration getConfiguration() {
086        return config;
087    }
088
089    /**
090     * Gets the configuration to be processed.
091     *
092     * @return the actual configuration
093     */
094    @Override
095    public Configuration getParsedConfiguration() {
096        return getConfiguration();
097    }
098
099    /**
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}