KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > XMLConfigManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 /*
7  * XMLConfigManager.java
8  *
9  * Created on April 23, 2002, 5:29 PM
10  */

11
12 package com.raptus.owxv3;
13
14
15 import java.io.File JavaDoc;
16 import java.util.*;
17
18 import org.jdom.*;
19 import org.jdom.input.SAXBuilder;
20
21
22 /**
23  *
24  * @author root
25  */

26 public class XMLConfigManager extends Object JavaDoc implements ConfigManagerIFace
27 {
28
29     /******************************** Constants */
30     /**
31      * This constant specifies the prefix which identifies a key in the
32      * servlet-params-hash belonging to this class.
33      *
34      * <b>Change this only if you know exactly why!</b>
35      */

36     public static final String JavaDoc PARAM_PREFIX = "XMLConfigManager.";
37
38     /**
39      * This key is used when retrieving the filename of the file to open.
40      *
41      * <b>Change this only if you know exactly why!</b>
42      */

43     protected static final String JavaDoc FILENAME_KEY = "FileName";
44
45
46     /******************************** Members */
47     /**
48      * This holds the filename of the properties-file
49      */

50     protected String JavaDoc myFileName;
51
52     /**
53      * This is an instance of the Configuration-object to which the properties-file
54      * is mapped.
55      */

56     protected XMLConfiguration myConfiguration;
57     
58     protected static XMLConfigManager _instance = null;
59     
60     public static XMLConfigManager getInstance()
61     {
62         if(_instance == null)
63         {
64           return null;
65         }
66         
67         return _instance;
68     }
69     
70     /** Creates a new instance of XMLConfigManager */
71     public XMLConfigManager()
72     {
73         log("constructor().");
74     }
75     
76     protected void log(String JavaDoc msg)
77     {
78         //LoggingManager.log(this.getClass().getName()+" "+msg);
79
}
80     
81     /**
82      * This method is used to get a storage-independent object which allows to
83      * access the configuration,
84      *
85      * @return An initialised Configuration-object
86      */

87     public Configuration getConfiguration()
88     {
89         log("getConfiguration()");
90         
91         return (Configuration)myConfiguration;
92     }
93     
94     /**
95      * This method saves the Configuration-object specified.
96      *
97      * @param cfg Config to save
98      * @return true if successful, false if not.
99      */

100     public boolean saveConfiguration(Configuration cfg)
101     {
102         log("saveConfiguration()");
103         return true;
104     }
105     
106     /**
107      * When calling this method, the configmanager is forced to reread/requery its
108      * configuration-data
109      */

110     public void refresh()
111     {
112         log("refresh");
113     }
114     
115     /**
116      * This Method is used to initialize.
117      *
118      * @param hash a hashtable with additional configuration-info.
119      * @return true if initialising was successful, false if not.
120      */

121     public boolean initialize(Hashtable hash)
122     {
123         _instance = this;
124         log("initialise()");
125         myFileName = (String JavaDoc) hash.get(ConfigManager.PARAM_PREFIX + PARAM_PREFIX + FILENAME_KEY);
126         log("Configuration file is "+myFileName);
127         myConfiguration = new XMLConfiguration(hash);
128         return load();
129     }
130     
131
132     public static Element root = null;
133     
134     public boolean load()
135     {
136         log("load()");
137         
138         try
139         {
140             // Request document building without validation
141
SAXBuilder builder = new SAXBuilder(false);
142             Document doc = builder.build(new File JavaDoc(myFileName));
143             root = doc.getRootElement();
144             log("XML Config file loaded!");
145             
146 // // start initialisation process.
147
// // first get virtual hosts and initialise them
148
// List categories = root.getChildren("virtualhost");
149
// Iterator i = categories.iterator();
150
//
151
// while (i.hasNext())
152
// {
153
// Element vhost = (Element) i.next();
154
// if(vhost != null)
155
// {
156
// if(!initialiseVirtualHost(vhost))
157
// {
158
// return false;
159
// }
160
// }
161
// }
162
}
163         catch(org.jdom.JDOMException ex)
164         {
165             ex.printStackTrace();
166         }
167         // Get the root element (categorylist
168
return true;
169     }
170     
171     // return the 'child' element of the e, which has the attrib attribute
172
// with value value
173
public Element getElement(Element e, String JavaDoc child, String JavaDoc attrib, String JavaDoc value)
174     {
175         if(e == null)
176         {
177             return null;
178         }
179         
180         List childs = e.getChildren(child);
181         Iterator i = childs.iterator();
182
183         while (i.hasNext())
184         {
185             // get child
186
Element ch = (Element) i.next();
187             
188             // if no attrib requested, return first child
189
if(attrib == null && ch != null)
190             {
191                 return ch;
192             }
193             
194             // if attrib requested
195
if(attrib != null && ch!=null)
196             {
197                 // check if element has attribute requested
198
if(ch.getAttribute(attrib)!=null)
199                 {
200                     // yes.if no value requested, return element if attribute found
201
// we don't care about attribute's value
202
if(value == null)
203                     {
204                         return ch;
205                     }
206                     else
207                     {
208                         // value specified. return element if attribute value
209
// equals specified value for the attirbute
210
if(ch.getAttribute(attrib).getValue().equals(value))
211                         {
212                             return ch;
213                         }
214                     }
215                 }
216             }
217         }
218         
219         return null;
220     }
221     
222     public Element getElement(Element e, String JavaDoc child )
223     {
224         return getElement(e, child, null, null);
225     }
226     
227     public Element getElement(Element e, String JavaDoc child, String JavaDoc attribute )
228     {
229         return getElement(e, child, attribute, null);
230     }
231     
232     /*
233      * get a tree specified by the path to the tree
234      * element1/element2/element3?attrib=value/element4
235      */

236     public Element getElementByTree(Element start, String JavaDoc tree)
237     {
238         if(start==null)
239         {
240             start=root;
241         }
242         
243         if(tree==null)
244         {
245             return null;
246         }
247         
248         if(tree.length()<=0)
249         {
250             return null;
251         }
252         
253         StringTokenizer st = new StringTokenizer(tree, "/");
254         
255         // if we don't have any / then we are at the last level, return it's child
256
if(!st.hasMoreTokens())
257         {
258             return getElement(start, tree);
259         }
260         
261         // get next level
262
String JavaDoc current=st.nextToken();
263         String JavaDoc rest="";
264         
265         // rebuild rest of path without first level
266
while(st.hasMoreTokens())
267         {
268             rest = rest.length()>0?rest+"/"+st.nextToken():st.nextToken();
269         }
270
271         //LoggingManager.log("Current="+current+",rest="+rest);
272

273         if(current.indexOf("?")==-1)
274         {
275             Element e = getElement(start, current);
276             if(e != null)
277             {
278                 if(rest.length()>0)
279                 {
280                     return getElementByTree(e, rest);
281                 }
282                 else
283                 {
284                     return e;
285                 }
286             }
287             else
288             {
289                 // element not found
290
log("current="+current+" was not found");
291                 return null;
292             }
293         }
294         else
295         {
296             // we have child?attribute=value
297
String JavaDoc child=current.substring(0, current.indexOf("?"));
298             //LoggingManager.log("Child="+child);
299
String JavaDoc attribute=current.substring(current.indexOf("?")+1, current.indexOf("="));
300             //LoggingManager.log("attribute="+attribute);
301

302             String JavaDoc val=current.substring(current.indexOf("=")+1);
303             //LoggingManager.log("attribute val="+val);
304

305             Element e = getElement(start, child, attribute, val);
306             if(e != null)
307             {
308                 if(rest.length()>0)
309                 {
310                     return getElementByTree(e, rest);
311                 }
312                 else
313                 {
314                     return e;
315                 }
316             }
317             else
318             {
319                 // element not found!
320
log("current="+current+" was not found!");
321                 return null;
322             }
323         }
324     }
325     
326     public String JavaDoc getPropertyByTree(String JavaDoc path, String JavaDoc attribute)
327     {
328         Element e = getElementByTree(null, path);
329         if(e == null)
330         {
331             return null;
332         }
333         else
334         {
335             if(e.getAttribute(attribute)!=null)
336             {
337                 return e.getAttribute(attribute).getValue();
338             }
339             else
340             {
341                 if(e.getText().trim().length()<1)
342                 {
343                     return null;
344                 }
345                 else
346                     return e.getText();
347             }
348         }
349     }
350
351     public boolean existProperty(String JavaDoc path, String JavaDoc attribute)
352     {
353         String JavaDoc s = getPropertyByTree(path, attribute);
354         if(s == null)
355         {
356             return false;
357         }
358         else
359         {
360             return true;
361         }
362     }
363     public boolean getBooleanByTree(String JavaDoc path, String JavaDoc attribute)
364     {
365         String JavaDoc s = getPropertyByTree(path, attribute);
366         if(s == null)
367         {
368             return false;
369         }
370         if(s.equals("true")||s.equals("1"))
371         {
372             return true;
373         }
374         return false;
375     }
376
377     public int getIntegerByTree(String JavaDoc path, String JavaDoc attribute)
378     {
379         String JavaDoc s = getPropertyByTree(path, attribute);
380         if(s == null)
381         {
382             return 0;
383         }
384         return Integer.parseInt(s);
385     }
386
387     public String JavaDoc[] getStringArrayByTree(String JavaDoc path, String JavaDoc attribute)
388     {
389         Element e = getElementByTree(null, path);
390         String JavaDoc res;
391         if(e == null)
392         {
393             return null;
394         }
395         else
396         {
397             if(e.getAttribute(attribute)!=null)
398             {
399                 res = e.getAttribute(attribute).getValue();
400             }
401             else
402             {
403                 res = e.getText();
404             }
405         }
406         
407         if(res.indexOf(",") == -1)
408         {
409             res = res+",";
410         }
411         StringTokenizer st = new StringTokenizer(res,",");
412         Vector v = new Vector();
413         while(st.hasMoreTokens())
414         {
415             String JavaDoc s = st.nextToken();
416             log(s);
417             v.add(s.trim());
418         }
419         log("Returning...");
420         String JavaDoc vs[] = new String JavaDoc[v.size()];
421         v.toArray(vs);
422         return vs;
423     }
424
425 }
426
Popular Tags