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 { 033 /** Stores the actual configuration.*/ 034 private Configuration config; 035 036 /** 037 * Creates a new instance of <code>BaseConfigurationXMLReader</code>. 038 */ 039 public BaseConfigurationXMLReader() 040 { 041 super(); 042 } 043 044 /** 045 * Creates a new instance of <code>BaseConfigurationXMLReader</code> and 046 * sets the configuration object to be parsed. 047 * 048 * @param conf the configuration to be parsed 049 */ 050 public BaseConfigurationXMLReader(Configuration conf) 051 { 052 this(); 053 setConfiguration(conf); 054 } 055 056 /** 057 * Returns the actual configuration to be processed. 058 * 059 * @return the actual configuration 060 */ 061 public Configuration getConfiguration() 062 { 063 return config; 064 } 065 066 /** 067 * Sets the configuration to be processed. 068 * 069 * @param conf the configuration 070 */ 071 public void setConfiguration(Configuration conf) 072 { 073 config = conf; 074 } 075 076 /** 077 * Returns the configuration to be processed. 078 * 079 * @return the actual configuration 080 */ 081 public Configuration getParsedConfiguration() 082 { 083 return getConfiguration(); 084 } 085 086 /** 087 * The main SAX event generation method. This element uses an internal 088 * <code>HierarchicalConfigurationConverter</code> object to iterate over 089 * all keys in the actual configuration and to generate corresponding SAX 090 * events. 091 */ 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 protected void elementStart(String name, Object value) 113 { 114 fireElementStart(name, null); 115 if (value != null) 116 { 117 fireCharacters(value.toString()); 118 } 119 } 120 121 /** 122 * Callback for the end of an element. 123 * 124 * @param name the element name 125 */ 126 protected void elementEnd(String name) 127 { 128 fireElementEnd(name); 129 } 130 } 131 }