1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.configuration; 19 20 /** 21 * <p>A specialized SAX2 XML parser that processes configuration objects.</p> 22 * 23 * <p>This class mimics to be a SAX compliant XML parser. It is able to iterate 24 * over the keys in a configuration object and to generate corresponding SAX 25 * events. By registering a {@code ContentHandler} at an instance 26 * it is possible to perform XML processing on a configuration object.</p> 27 * 28 * @author <a 29 * href="http://commons.apache.org/configuration/team-list.html">Commons 30 * Configuration team</a> 31 * @version $Id: BaseConfigurationXMLReader.java 1206765 2011-11-27 16:46:11Z oheger $ 32 */ 33 public class BaseConfigurationXMLReader extends ConfigurationXMLReader 34 { 35 /** Stores the actual configuration.*/ 36 private Configuration config; 37 38 /** 39 * Creates a new instance of {@code BaseConfigurationXMLReader}. 40 */ 41 public BaseConfigurationXMLReader() 42 { 43 super(); 44 } 45 46 /** 47 * Creates a new instance of {@code BaseConfigurationXMLReader} and 48 * sets the configuration object to be parsed. 49 * 50 * @param conf the configuration to be parsed 51 */ 52 public BaseConfigurationXMLReader(Configuration conf) 53 { 54 this(); 55 setConfiguration(conf); 56 } 57 58 /** 59 * Returns the actual configuration to be processed. 60 * 61 * @return the actual configuration 62 */ 63 public Configuration getConfiguration() 64 { 65 return config; 66 } 67 68 /** 69 * Sets the configuration to be processed. 70 * 71 * @param conf the configuration 72 */ 73 public void setConfiguration(Configuration conf) 74 { 75 config = conf; 76 } 77 78 /** 79 * Returns the configuration to be processed. 80 * 81 * @return the actual configuration 82 */ 83 @Override 84 public Configuration getParsedConfiguration() 85 { 86 return getConfiguration(); 87 } 88 89 /** 90 * The main SAX event generation method. This element uses an internal 91 * {@code HierarchicalConfigurationConverter} object to iterate over 92 * all keys in the actual configuration and to generate corresponding SAX 93 * events. 94 */ 95 @Override 96 protected void processKeys() 97 { 98 fireElementStart(getRootName(), null); 99 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 }