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.configuration2; 19 20 /** 21 * <p> 22 * A specialized SAX2 XML parser that processes configuration objects. 23 * </p> 24 * 25 * <p> 26 * This class mimics to be a SAX compliant XML parser. It is able to iterate over the keys in a configuration object and 27 * to generate corresponding SAX events. By registering a {@code ContentHandler} at an instance it is possible to 28 * perform XML processing on a configuration object. 29 * </p> 30 */ 31 public class BaseConfigurationXMLReader extends ConfigurationXMLReader { 32 /** Stores the actual configuration. */ 33 private Configuration config; 34 35 /** 36 * Creates a new instance of {@code BaseConfigurationXMLReader}. 37 */ 38 public BaseConfigurationXMLReader() { 39 } 40 41 /** 42 * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed. 43 * 44 * @param conf the configuration to be parsed 45 */ 46 public BaseConfigurationXMLReader(final Configuration conf) { 47 this(); 48 setConfiguration(conf); 49 } 50 51 /** 52 * Gets the actual configuration to be processed. 53 * 54 * @return the actual configuration 55 */ 56 public Configuration getConfiguration() { 57 return config; 58 } 59 60 /** 61 * Sets the configuration to be processed. 62 * 63 * @param conf the configuration 64 */ 65 public void setConfiguration(final Configuration conf) { 66 config = conf; 67 } 68 69 /** 70 * Gets the configuration to be processed. 71 * 72 * @return the actual configuration 73 */ 74 @Override 75 public Configuration getParsedConfiguration() { 76 return getConfiguration(); 77 } 78 79 /** 80 * The main SAX event generation method. This element uses an internal {@code HierarchicalConfigurationConverter} object 81 * to iterate over all keys in the actual configuration and to generate corresponding SAX events. 82 */ 83 @Override 84 protected void processKeys() { 85 fireElementStart(getRootName(), null); 86 new SAXConverter().process(getConfiguration()); 87 fireElementEnd(getRootName()); 88 } 89 90 /** 91 * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events. 92 */ 93 final class SAXConverter extends HierarchicalConfigurationConverter { 94 /** 95 * Callback for the start of an element. 96 * 97 * @param name the element name 98 * @param value the element value 99 */ 100 @Override 101 protected void elementStart(final String name, final Object value) { 102 fireElementStart(name, null); 103 if (value != null) { 104 fireCharacters(value.toString()); 105 } 106 } 107 108 /** 109 * Callback for the end of an element. 110 * 111 * @param name the element name 112 */ 113 @Override 114 protected void elementEnd(final String name) { 115 fireElementEnd(name); 116 } 117 } 118 }