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
018 package org.apache.commons.configuration;
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 * @author <a
029 * href="http://commons.apache.org/configuration/team-list.html">Commons
030 * Configuration team</a>
031 * @version $Id: BaseConfigurationXMLReader.java 1206765 2011-11-27 16:46:11Z oheger $
032 */
033 public class BaseConfigurationXMLReader extends ConfigurationXMLReader
034 {
035 /** Stores the actual configuration.*/
036 private Configuration config;
037
038 /**
039 * Creates a new instance of {@code BaseConfigurationXMLReader}.
040 */
041 public BaseConfigurationXMLReader()
042 {
043 super();
044 }
045
046 /**
047 * Creates a new instance of {@code BaseConfigurationXMLReader} and
048 * sets the configuration object to be parsed.
049 *
050 * @param conf the configuration to be parsed
051 */
052 public BaseConfigurationXMLReader(Configuration conf)
053 {
054 this();
055 setConfiguration(conf);
056 }
057
058 /**
059 * Returns the actual configuration to be processed.
060 *
061 * @return the actual configuration
062 */
063 public Configuration getConfiguration()
064 {
065 return config;
066 }
067
068 /**
069 * Sets the configuration to be processed.
070 *
071 * @param conf the configuration
072 */
073 public void setConfiguration(Configuration conf)
074 {
075 config = conf;
076 }
077
078 /**
079 * Returns the configuration to be processed.
080 *
081 * @return the actual configuration
082 */
083 @Override
084 public Configuration getParsedConfiguration()
085 {
086 return getConfiguration();
087 }
088
089 /**
090 * The main SAX event generation method. This element uses an internal
091 * {@code HierarchicalConfigurationConverter} object to iterate over
092 * all keys in the actual configuration and to generate corresponding SAX
093 * events.
094 */
095 @Override
096 protected void processKeys()
097 {
098 fireElementStart(getRootName(), null);
099 new SAXConverter().process(getConfiguration());
100 fireElementEnd(getRootName());
101 }
102
103 /**
104 * An internally used helper class to iterate over all configuration keys
105 * ant to generate corresponding SAX events.
106 *
107 */
108 class SAXConverter extends HierarchicalConfigurationConverter
109 {
110 /**
111 * Callback for the start of an element.
112 *
113 * @param name the element name
114 * @param value the element value
115 */
116 @Override
117 protected void elementStart(String name, Object value)
118 {
119 fireElementStart(name, null);
120 if (value != null)
121 {
122 fireCharacters(value.toString());
123 }
124 }
125
126 /**
127 * Callback for the end of an element.
128 *
129 * @param name the element name
130 */
131 @Override
132 protected void elementEnd(String name)
133 {
134 fireElementEnd(name);
135 }
136 }
137 }