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