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> 022 * A specialized SAX2 XML parser that processes configuration objects. 023 * </p> 024 * 025 * <p> 026 * This class mimics to be a SAX compliant XML parser. It is able to iterate over the keys in a configuration object and 027 * to generate corresponding SAX events. By registering a {@code ContentHandler} at an instance it is possible to 028 * perform XML processing on a configuration object. 029 * </p> 030 */ 031public class BaseConfigurationXMLReader extends ConfigurationXMLReader { 032 /** Stores the actual configuration. */ 033 private Configuration config; 034 035 /** 036 * Creates a new instance of {@code BaseConfigurationXMLReader}. 037 */ 038 public BaseConfigurationXMLReader() { 039 } 040 041 /** 042 * Creates a new instance of {@code BaseConfigurationXMLReader} and sets the configuration object to be parsed. 043 * 044 * @param conf the configuration to be parsed 045 */ 046 public BaseConfigurationXMLReader(final Configuration conf) { 047 this(); 048 setConfiguration(conf); 049 } 050 051 /** 052 * Gets the actual configuration to be processed. 053 * 054 * @return the actual configuration 055 */ 056 public Configuration getConfiguration() { 057 return config; 058 } 059 060 /** 061 * Sets the configuration to be processed. 062 * 063 * @param conf the configuration 064 */ 065 public void setConfiguration(final Configuration conf) { 066 config = conf; 067 } 068 069 /** 070 * Gets the configuration to be processed. 071 * 072 * @return the actual configuration 073 */ 074 @Override 075 public Configuration getParsedConfiguration() { 076 return getConfiguration(); 077 } 078 079 /** 080 * The main SAX event generation method. This element uses an internal {@code HierarchicalConfigurationConverter} object 081 * to iterate over all keys in the actual configuration and to generate corresponding SAX events. 082 */ 083 @Override 084 protected void processKeys() { 085 fireElementStart(getRootName(), null); 086 new SAXConverter().process(getConfiguration()); 087 fireElementEnd(getRootName()); 088 } 089 090 /** 091 * An internally used helper class to iterate over all configuration keys ant to generate corresponding SAX events. 092 */ 093 final class SAXConverter extends HierarchicalConfigurationConverter { 094 /** 095 * Callback for the start of an element. 096 * 097 * @param name the element name 098 * @param value the element value 099 */ 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}