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</code> at an instance
026: * it is possible to perform XML processing on a configuration object.</p>
027: *
028: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
029: * @version $Id: BaseConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
030: */
031: public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
032: /** Stores the actual configuration.*/
033: private Configuration config;
034:
035: /**
036: * Creates a new instance of <code>BaseConfigurationXMLReader</code>.
037: */
038: public BaseConfigurationXMLReader() {
039: super ();
040: }
041:
042: /**
043: * Creates a new instance of <code>BaseConfigurationXMLReader</code> and
044: * sets the configuration object to be parsed.
045: *
046: * @param conf the configuration to be parsed
047: */
048: public BaseConfigurationXMLReader(Configuration conf) {
049: this ();
050: setConfiguration(conf);
051: }
052:
053: /**
054: * Returns the actual configuration to be processed.
055: *
056: * @return the actual configuration
057: */
058: public Configuration getConfiguration() {
059: return config;
060: }
061:
062: /**
063: * Sets the configuration to be processed.
064: *
065: * @param conf the configuration
066: */
067: public void setConfiguration(Configuration conf) {
068: config = conf;
069: }
070:
071: /**
072: * Returns the configuration to be processed.
073: *
074: * @return the actual configuration
075: */
076: public Configuration getParsedConfiguration() {
077: return getConfiguration();
078: }
079:
080: /**
081: * The main SAX event generation method. This element uses an internal
082: * <code>HierarchicalConfigurationConverter</code> object to iterate over
083: * all keys in the actual configuration and to generate corresponding SAX
084: * events.
085: */
086: protected void processKeys() {
087: fireElementStart(getRootName(), null);
088: new SAXConverter().process(getConfiguration());
089: fireElementEnd(getRootName());
090: }
091:
092: /**
093: * An internally used helper class to iterate over all configuration keys
094: * ant to generate corresponding SAX events.
095: *
096: */
097: class SAXConverter extends HierarchicalConfigurationConverter {
098: /**
099: * Callback for the start of an element.
100: *
101: * @param name the element name
102: * @param value the element value
103: */
104: protected void elementStart(String name, Object value) {
105: fireElementStart(name, null);
106: if (value != null) {
107: fireCharacters(value.toString());
108: }
109: }
110:
111: /**
112: * Callback for the end of an element.
113: *
114: * @param name the element name
115: */
116: protected void elementEnd(String name) {
117: fireElementEnd(name);
118: }
119: }
120: }
|