001: package net.myvietnam.mvncore.configuration;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056:
057: import java.io.IOException;
058:
059: import org.xml.sax.SAXException;
060:
061: /**
062: * <p>A specialized SAX2 XML parser that processes configuration objects.</p>
063: * <p>This class mimics to be a SAX compliant XML parser. It is able to iterate
064: * over the keys in a configuration object and to generate corresponding SAX
065: * events. By registering a <code>ContentHandler</code> at an instance
066: * it is possible to perform XML processing on a configuration object.</p>
067: *
068: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
069: * @version $Id: BaseConfigurationXMLReader.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
070: */
071: public class BaseConfigurationXMLReader extends ConfigurationXMLReader {
072: /** Stores the actual configuration.*/
073: private Configuration config;
074:
075: /**
076: * Creates a new instance of <code>BaseConfigurationXMLReader</code>.
077: */
078: public BaseConfigurationXMLReader() {
079: super ();
080: }
081:
082: /**
083: * Creates a new instance of <code>BaseConfigurationXMLReader</code> and
084: * sets the configuration object to be parsed.
085: * @param conf the configuration to be parsed
086: */
087: public BaseConfigurationXMLReader(Configuration conf) {
088: this ();
089: setConfiguration(conf);
090: }
091:
092: /**
093: * Returns the actual configuration to be processed.
094: * @return the actual configuration
095: */
096: public Configuration getConfiguration() {
097: return config;
098: }
099:
100: /**
101: * Sets the configuration to be processed.
102: * @param conf the configuration
103: */
104: public void setConfiguration(Configuration conf) {
105: config = conf;
106: }
107:
108: /**
109: * Returns the configuration to be processed.
110: * @return the actual configuration
111: */
112: public Configuration getParsedConfiguration() {
113: return getConfiguration();
114: }
115:
116: /**
117: * The main SAX event generation method. This element uses an internal
118: * <code>HierarchicalConfigurationConverter</code> object to iterate over
119: * all keys in the actual configuration and to generate corresponding SAX
120: * events.
121: * @throws IOException if no configuration object is specified
122: * @throws SAXException if a SAXException occurs during parsing
123: */
124: protected void processKeys() throws IOException, SAXException {
125: fireElementStart(getRootName(), null);
126: new SAXConverter().process(getConfiguration());
127: fireElementEnd(getRootName());
128: }
129:
130: /**
131: * An internally used helper class to iterate over all configuration keys
132: * ant to generate corresponding SAX events.
133: *
134: * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
135: */
136: class SAXConverter extends HierarchicalConfigurationConverter {
137: /**
138: * Callback for the start of an element.
139: * @param name the element name
140: * @param value the element value
141: */
142: protected void elementStart(String name, Object value) {
143: fireElementStart(name, null);
144: if (value != null) {
145: fireCharacters(value.toString());
146: } /* if */
147: }
148:
149: /**
150: * Callback for the end of an element.
151: * @param name the element name
152: */
153: protected void elementEnd(String name) {
154: fireElementEnd(name);
155: }
156: }
157: }
|