KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2002-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.net.URL JavaDoc;
58 import java.util.Iterator JavaDoc;
59
60 import org.dom4j.Attribute;
61 import org.dom4j.Document;
62 import org.dom4j.Element;
63 import org.dom4j.io.SAXReader;
64
65 /**
66  * <p>A specialized hierarchical configuration class that is able to parse
67  * XML documents using DOM4J.</p>
68  * <p>The parsed document will be stored keeping its structure. The
69  * contained properties can be accessed using all methods supported by
70  * the base class <code>HierarchicalProperties</code>.
71  *
72  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
73  * @version $Id: HierarchicalDOM4JConfiguration.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
74  */

75 public class HierarchicalDOM4JConfiguration
76     extends HierarchicalConfiguration
77     implements BasePathLoader
78 {
79     /** Stores the file name of the document to be parsed.*/
80     private String JavaDoc file;
81
82     /** Stores the base path of this configuration.*/
83     private String JavaDoc basePath;
84
85     /**
86      * Creates a new instance of <code>HierarchicalDOM4JConfiguration</code>.
87      */

88     public HierarchicalDOM4JConfiguration()
89     {
90         super();
91     }
92
93     /**
94      * Creates a new instance of <code>HierarchicalDOM4JConfiguration</code>
95      * and sets the default properties.
96      * @param defaults the default properties
97      */

98     public HierarchicalDOM4JConfiguration(Configuration defaults)
99     {
100         super(defaults);
101     }
102
103     /**
104      * Returns the name of the file to be parsed by this object.
105      * @return the file to be parsed
106      */

107     public String JavaDoc getFileName()
108     {
109         return file;
110     }
111
112     /**
113      * Sets the name of the file to be parsed by this object.
114      * @param file the file to be parsed
115      */

116     public void setFileName(String JavaDoc file)
117     {
118         this.file = file;
119     }
120
121     /**
122      * Returns the base path.
123      * @return the base path
124      */

125     public String JavaDoc getBasePath()
126     {
127         return basePath;
128     }
129
130     /**
131      * Allows to set a base path. Relative file names are resolved based on
132      * this path.
133      * @param path the base path; this can be a URL or a file path
134      */

135     public void setBasePath(String JavaDoc path)
136     {
137         basePath = path;
138     }
139
140     /**
141      * Loads and parses an XML document. The file to be loaded must have
142      * been specified before.
143      * @throws Exception if an error occurs
144      */

145     public void load() throws Exception JavaDoc
146     {
147         load(ConfigurationUtils.getURL(getBasePath(), getFileName()));
148     }
149
150     /**
151      * Loads and parses the specified XML document.
152      * @param url the URL to the XML document
153      * @throws Exception if an error occurs
154      */

155     public void load(URL JavaDoc url) throws Exception JavaDoc
156     {
157         initProperties(new SAXReader().read(url));
158     }
159
160     /**
161      * Initializes this configuration from an XML document.
162      * @param document the document to be parsed
163      */

164     public void initProperties(Document document)
165     {
166         constructHierarchy(getRoot(), document.getRootElement());
167     }
168
169     /**
170      * Helper method for building the internal storage hierarchy. The XML
171      * elements are transformed into node objects.
172      * @param node the actual node
173      * @param element the actual XML element
174      */

175     private void constructHierarchy(Node node, Element element)
176     {
177         if (element.getTextTrim().length() > 0)
178         {
179             node.setValue(element.getTextTrim());
180         } /* if */
181         processAttributes(node, element);
182
183         for (Iterator JavaDoc it = element.elementIterator(); it.hasNext();)
184         {
185             Element child = (Element) it.next();
186             Node childNode = new Node(child.getName());
187             constructHierarchy(childNode, child);
188             node.addChild(childNode);
189         } /* for */
190     }
191
192     /**
193      * Helper method for constructing node objects for the attributes of the
194      * given XML element.
195      * @param node the actual node
196      * @param element the actual XML element
197      */

198     private void processAttributes(Node node, Element element)
199     {
200         for (Iterator JavaDoc it = element.attributeIterator(); it.hasNext();)
201         {
202             Attribute attr = (Attribute) it.next();
203             Node child =
204                 new Node(
205                     ConfigurationKey.constructAttributeKey(attr.getName()));
206             child.setValue(attr.getValue());
207             node.addChild(child);
208         } /* for */
209     }
210 }
211
Popular Tags