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