KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > configuration > ConfigurationElement


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.configuration;
47
48 import java.io.File JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.HashMap JavaDoc;
51
52 import javax.xml.parsers.DocumentBuilder JavaDoc;
53 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
54
55 import org.mr.core.log.StartupLogger;
56 import org.w3c.dom.Document JavaDoc;
57 import org.w3c.dom.Element JavaDoc;
58 import org.w3c.dom.Node JavaDoc;
59 import org.w3c.dom.NodeList JavaDoc;
60
61 /**
62  * Represents a XML tag in the configuration xml
63  * @author Amir Shevat
64  *
65  */

66 public class ConfigurationElement {
67     private String JavaDoc name;
68     // if this is not a leaf configuration element then it has sub configuration elements
69
private ArrayList JavaDoc subConfigurationElements = null;
70     private HashMap JavaDoc subConfigurationElementsMap = null;
71     // if this ConfigurationElement is a leaf then
72
private String JavaDoc value = null;
73
74     private boolean isLeaf;
75
76     private Element JavaDoc myRoot;
77
78
79     public ConfigurationElement(Element JavaDoc element) {
80         name =element.getNodeName();
81         myRoot = element;
82         NodeList JavaDoc sub = element.getChildNodes();
83         if(sub.getLength()==1 && sub.item(0).getNodeType() == Node.TEXT_NODE){
84             // this is a leaf
85
value = sub.item(0).getNodeValue();
86             value = value.trim();
87             isLeaf = true;
88         }else{
89             isLeaf = false;
90             subConfigurationElements = new ArrayList JavaDoc();
91             subConfigurationElementsMap = new HashMap JavaDoc();
92             for (int i = 0; i < sub.getLength(); i++) {
93                 if(sub.item(i).getNodeType() == Node.ELEMENT_NODE){
94                     Element JavaDoc subE = (Element JavaDoc)sub.item(i);
95                     ConfigurationElement subElement =getRefOrRealConfigurationElement(subE);// new ConfigurationElement(subE);
96
subConfigurationElements.add(subElement);
97                     String JavaDoc subName = subElement.getName();
98                     ArrayList JavaDoc toBeAdded = (ArrayList JavaDoc) subConfigurationElementsMap.get(subName);
99
100                     if(toBeAdded == null){
101                         toBeAdded = new ArrayList JavaDoc();
102                         subConfigurationElementsMap.put(subName,toBeAdded);
103                     }
104                     toBeAdded.add(subElement);
105
106                 }
107             }//for
108

109         }//else
110
}//ConfigurationElement
111

112     public Element JavaDoc getMyElement() {
113         return myRoot;
114     }
115
116     public ArrayList JavaDoc getSubConfigurationElements(String JavaDoc key) {
117         ConfigurationElement element = this;
118         ArrayList JavaDoc result = null;
119         String JavaDoc[] subKeys = key.split("\\.");
120         for (int i = 0; i < subKeys.length; i++) {
121             String JavaDoc subKey = subKeys[i];
122             if(element != null)
123                 result = element.getSubConfigurationElementsByName(subKey);
124             if(result == null){
125                 return null;
126             }else{
127                 element = (ConfigurationElement) result.get(0);
128             }
129         }
130         return result;
131     }
132
133     public ConfigurationElement getSubConfigurationElement(String JavaDoc key) {
134         ConfigurationElement element = this;
135         ArrayList JavaDoc result = null;
136         String JavaDoc[] subKeys = key.split("\\.");
137         for (int i = 0; i < subKeys.length; i++) {
138             String JavaDoc subKey = subKeys[i];
139             result = element.getSubConfigurationElementsByName(subKey);
140             if(result == null){
141                 return null;
142             }else{
143                 element = (ConfigurationElement) result.get(0);
144             }
145         }
146         return (ConfigurationElement) result.get(0);
147     }
148
149     private ConfigurationElement getRefOrRealConfigurationElement(Element JavaDoc subE) {
150         ConfigurationElement result = null;
151         if(subE.getNodeName().equals("file_ref")){
152             String JavaDoc fileRef = subE.getChildNodes().item(0).getNodeValue();
153             File JavaDoc f = new File JavaDoc(fileRef);
154             if(!f.exists()){
155                 //System.out.println("FATAL: Did not find configuration file reference -"+fileRef);
156
StartupLogger.log.fatal("Did not find configuration file reference -"+fileRef, "ConfigurationElement");
157                 throw new IllegalArgumentException JavaDoc("Did not find configuration file reference -"+fileRef);
158             }
159             Element JavaDoc rootElement;
160             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
161             factory.setIgnoringElementContentWhitespace(true);
162
163             try{
164                 DocumentBuilder JavaDoc xmlBuilder= factory.newDocumentBuilder();
165                 Document JavaDoc xmlDoc = xmlBuilder.parse(f);
166
167                 rootElement = xmlDoc.getDocumentElement();
168                 result = new ConfigurationElement(rootElement);
169             }catch(Exception JavaDoc e){
170                 e.printStackTrace();
171                 throw new IllegalArgumentException JavaDoc(e.getLocalizedMessage());
172             }
173         }else{
174             result = new ConfigurationElement(subE);
175         }
176         return result;
177     }
178
179     public boolean isLeaf() {
180         return isLeaf;
181     }
182
183     public ArrayList JavaDoc getSubConfigurationElements() {
184         return subConfigurationElements;
185     }
186
187     public ArrayList JavaDoc getSubConfigurationElementsByName(String JavaDoc subName) {
188         ArrayList JavaDoc result = null;
189         if(subConfigurationElementsMap!=null)
190             result =(ArrayList JavaDoc) subConfigurationElementsMap.get(subName);
191         return result;
192     }
193     public ConfigurationElement getSubConfigurationElementByName(String JavaDoc subName) {
194         ConfigurationElement result = null;
195         ArrayList JavaDoc sub = (ArrayList JavaDoc) subConfigurationElementsMap.get(subName);
196         if(sub!=null){
197             result = (ConfigurationElement) sub.get(0);
198         }
199         return result;
200     }
201
202     public String JavaDoc getValue() {
203         return value;
204     }
205     public String JavaDoc getName() {
206         return name;
207     }
208 }
209
Popular Tags