KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > configuration > HierarchicalConfigurationXMLReader


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58
59 import net.myvietnam.mvncore.configuration.HierarchicalConfiguration.*;
60 import org.xml.sax.Attributes JavaDoc;
61 import org.xml.sax.SAXException JavaDoc;
62 import org.xml.sax.helpers.AttributesImpl JavaDoc;
63
64 /**
65  * <p>A specialized SAX2 XML parser that "parses" hierarchical
66  * configuration objects.</p>
67  * <p>This class mimics to be a SAX conform XML parser. Instead of parsing
68  * XML documents it processes a <code>Configuration</code> object and
69  * generates SAX events for the single properties defined there. This enables
70  * the whole world of XML processing for configuration objects.</p>
71  * <p>The <code>HierarchicalConfiguration</code> object to be parsed can be
72  * specified using a constructor or the <code>setConfiguration()</code> method.
73  * This object will be processed by the <code>parse()</code> methods. Note
74  * that these methods ignore their argument.</p>
75  *
76  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
77  * @version $Id: HierarchicalConfigurationXMLReader.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
78  */

79 public class HierarchicalConfigurationXMLReader
80 extends ConfigurationXMLReader
81 {
82     /** Stores the configuration object to be parsed.*/
83     private HierarchicalConfiguration configuration;
84
85     /**
86      * Creates a new instance of
87      * <code>HierarchicalConfigurationXMLReader</code>.
88      */

89     public HierarchicalConfigurationXMLReader()
90     {
91         super();
92     }
93
94     /**
95      * Creates a new instance of
96      * <code>HierarchicalConfigurationXMLReader</code> and sets the
97      * configuration to be parsed.
98      * @param config the configuration object
99      */

100     public HierarchicalConfigurationXMLReader(
101     HierarchicalConfiguration config)
102     {
103         this();
104         setConfiguration(config);
105     }
106
107     /**
108      * Returns the configuration object to be parsed.
109      * @return the configuration object to be parsed
110      */

111     public HierarchicalConfiguration getConfiguration()
112     {
113         return configuration;
114     }
115
116     /**
117      * Sets the configuration object to be parsed.
118      * @param config the configuration object to be parsed
119      */

120     public void setConfiguration(HierarchicalConfiguration config)
121     {
122         configuration = config;
123     }
124
125     /**
126      * Returns the configuration object to be processed.
127      * @return the actual configuration object
128      */

129     public Configuration getParsedConfiguration()
130     {
131         return getConfiguration();
132     }
133
134     /**
135      * Processes the actual configuration object to generate SAX parsing
136      * events.
137      * @throws IOException if no configuration has been specified
138      * @throws SAXException if an error occurs during parsing
139      */

140     protected void processKeys() throws IOException JavaDoc, SAXException JavaDoc
141     {
142         getConfiguration().getRoot().visit(new SAXVisitor(), null);
143     }
144
145     /**
146      * A specialized visitor class for generating SAX events for a
147      * hierarchical node structure.
148      *
149      * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
150      */

151     class SAXVisitor extends HierarchicalConfiguration.NodeVisitor
152     {
153         /** Constant for the attribute type.*/
154         private static final String JavaDoc ATTR_TYPE = "CDATA";
155
156         /**
157          * Visits the specified node after its children have been processed.
158          * @param node the actual node
159          * @param key the key of this node
160          */

161         public void visitAfterChildren(Node node, ConfigurationKey key)
162         {
163             if(!isAttributeNode(node))
164             {
165                 fireElementEnd(nodeName(node));
166             } /* if */
167         }
168
169         /**
170          * Visits the specified node.
171          * @param node the actual node
172          * @param key the key of this node
173          */

174         public void visitBeforeChildren(Node node, ConfigurationKey key)
175         {
176             if(!isAttributeNode(node))
177             {
178                 fireElementStart(nodeName(node), fetchAttributes(node));
179
180                 if(node.getValue() != null)
181                 {
182                     fireCharacters(node.getValue().toString());
183                 } /* if */
184             } /* if */
185         }
186
187         /**
188          * Checks if iteration should be terminated. This implementation stops
189          * iteration after an exception has occurred.
190          * @return a flag if iteration should be stopped
191          */

192         public boolean terminate()
193         {
194             return getException() != null;
195         }
196
197         /**
198          * Returns an object with all attributes for the specified node.
199          * @param node the actual node
200          * @return an object with all attributes of this node
201          */

202         protected Attributes JavaDoc fetchAttributes(Node node)
203         {
204             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
205             AbstractConfiguration.Container children = node.getChildren();
206
207             for(int i = 0; i < children.size(); i++)
208             {
209                 Node child = (Node) children.get(i);
210                 if(isAttributeNode(child) && child.getValue() != null)
211                 {
212                     String JavaDoc attr = ConfigurationKey.attributeName(
213                     child.getName());
214                     attrs.addAttribute(NS_URI, attr, attr, ATTR_TYPE,
215                     child.getValue().toString());
216                 } /* if */
217             } /* for */
218
219             return attrs;
220         }
221
222         /**
223          * Helper method for determining the name of a node. If a node has no
224          * name (which is true for the root node), the specified default name
225          * will be used.
226          * @param node the node to be checked
227          * @return the name for this node
228          */

229         private String JavaDoc nodeName(Node node)
230         {
231             return (node.getName() == null) ? getRootName() : node.getName();
232         }
233
234         /**
235          * Checks if the specified node is an attribute node. In the node
236          * hierarchy attributes are stored as normal child nodes, but with
237          * special names.
238          * @param node the node to be checked
239          * @return a flag if this is an attribute node
240          */

241         private boolean isAttributeNode(Node node)
242         {
243             return ConfigurationKey.isAttributeKey(node.getName());
244         }
245     }
246 }
247
Popular Tags