KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

11
12 package com.raptus.owxv3;
13
14 import java.util.*;
15
16 import org.jdom.Element;
17 /**
18  *
19  * @author root
20  */

21 public class XMLConfiguration extends Configuration
22 {
23     protected void log(String JavaDoc msg)
24     {
25         try
26         {
27             LoggingManager.log("++++++++++++++++++++++++++++++"+this.getClass().getName()+" "+msg);
28             throw new RuntimeException JavaDoc();
29         }
30         catch(RuntimeException JavaDoc ex)
31         {
32             ex.printStackTrace();
33         }
34     }
35     
36     /** Creates a new instance of XMLConfiguration */
37     public XMLConfiguration(Hashtable hash)
38     {
39         super(new Hashtable());
40     }
41
42     /******************************** Methods */
43     /**
44      * @return the cloned instance of itself, or null if failed
45      */

46     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
47     {
48         log("clone");
49         return null;
50         //return null;
51
}
52
53     /**
54      * We provide an access to the Hashtable, so configuration-info can also
55      * be retrieved in a custom manner.
56      *
57      * @return The config-hashtable.
58      */

59     public Hashtable getConfigHash()
60     {
61         log("getConfigHash()");
62         //return this.myConfigHash;
63
return null;
64     }
65
66     /**
67      * This method checks if the specified
68      * key has a value or not.
69      *
70      * @return false if the key has a value, true if not.
71      */

72     public boolean isEmptyKey(String JavaDoc key)
73     {
74         log("isEmptyKey("+key+")");
75         String JavaDoc str = (String JavaDoc) myConfigHash.get(key);
76
77         return (str == null || str.length() == 0);
78     }
79
80     /**
81      * Deletes the specified key.
82      * <b>IMPORTANT:</b>
83      * The key is not really deleted from the config, but it's value is set
84      * to "". So it seems that the key is deleted and isEmptyKey() works properly.
85      *
86      * @param key The key to delete.
87      *
88      * @return true if removed, false if the key didn't exist.
89      */

90      public boolean deleteByKey(String JavaDoc key)
91      {
92          log("deleteByKey("+key+")");
93         myConfigHash.put(key,"");
94
95         return true;
96      }
97
98
99 /******************************** set-methods */
100     /**
101      * This method sets the config-property identified with the key to the
102      * value of the String value.
103      *
104      * @param key The key to store the value under.
105      * @param value The value to store
106      *
107      * @throws NullPointerException if value is null.
108      */

109      public void setStringByKey(String JavaDoc key, String JavaDoc value) throws NullPointerException JavaDoc
110      {
111          log("setStringByKey("+key+","+value+")");
112         myConfigHash.put(key, value);
113      }
114
115     /**
116      * This saves back the String-array value in the property identified with key.
117      * The array is saved as a single String which is separated with ARRAY_SEPARATOR.
118      *
119      * @param key The key to store the value under.
120      * @param value The array to store.
121      *
122      * @throws NullPointerException if value is null.
123      */

124      public void setStringArrayByKey(String JavaDoc key, String JavaDoc[] value) throws NullPointerException JavaDoc
125      {
126          log("setStringArray("+key+")");
127          if(value == null || value.length == 0)
128              throw new NullPointerException JavaDoc("Empty array");
129
130          String JavaDoc tmp = "";
131          for(int i=0; i < value.length; i++)
132          {
133              tmp += value[i];
134              if(i != value.length-1)
135                  tmp += ARRAY_SEPARATOR;
136          }
137          myConfigHash.put(key,tmp);
138      }
139
140     /**
141      * This saves back a boolean value in the property identified with key.
142      * The boolean is converted in a String.
143      *
144      * @param key The key to store the value under.
145      * @param value The boolean to store.
146      *
147      * @throws NullPointerException if value is null.
148      */

149      public void setBooleanByKey(String JavaDoc key, boolean value) throws NullPointerException JavaDoc
150      {
151          log("setBooleanByKey("+key+","+value+")");
152         if(value)
153             myConfigHash.put(key, "true");
154         else
155             myConfigHash.put(key, "false");
156      }
157
158     /**
159      * This saves back an int value in the property identified with key.
160      * The int is converted in a String.
161      *
162      * @param key The key to store the value under.
163      * @param value The int to store.
164      *
165      * @throws NullPointerException if value is null.
166      */

167      public void setIntByKey(String JavaDoc key, int value) throws NullPointerException JavaDoc
168      {
169          log("setIntByKey("+key+","+value);
170         myConfigHash.put(key, Integer.toString(value));
171      }
172
173
174 /******************************** get-methods */
175     /**
176      * Used for getting strings.
177      *
178      * @return A string with the value of the specified key, or null if not set.
179      */

180     public String JavaDoc getStringByKey(String JavaDoc key)
181     {
182         log("getStringByKey("+key+")");
183         //return (String) myConfigHash.get(key);
184
return getStringValue(XMLConfigManager.root,key);
185     }
186
187     /**
188      * Used for getting a property-array.
189      * A property-array is a property which has more than one value separated by
190      * commas
191      *
192      * @param key the name of the property-array
193      * @return an array of the properties values, or null if none have been found.
194      */

195     public String JavaDoc[] getStringArrayByKey(String JavaDoc key)
196     {
197         log("getStringArrayByKey("+key+")");
198         String JavaDoc value = getStringByKey(key);
199         if(value == null)
200             return null;
201
202         StringTokenizer st = new StringTokenizer(value, ARRAY_SEPARATOR);
203         int count = st.countTokens();
204         if(count == 0)
205             return null;
206
207         int i = 0;
208         String JavaDoc[] array = new String JavaDoc[count];
209         while(st.hasMoreTokens())
210         {
211             array[i++] = st.nextToken();
212         }
213         return array;
214     }
215
216     /**
217      * Used for getting boolean states.
218      * A key is true if it has a value of "true", "on". "1". The case needn't match.
219      * the key is false if it's value is "false", "off", "0".
220      *
221      * @throws ParseException if the key has another value than specified above.
222      * @return true or false as defined above.
223      */

224     public boolean getBooleanByKey(String JavaDoc key)// throws ParseException
225
{
226         log("getBooleanByKey("+key+")");
227         if(!isEmptyKey(key))
228         {
229             String JavaDoc str = (String JavaDoc) myConfigHash.get(key);
230
231             if(str.compareToIgnoreCase("true") == 0 ||
232                str.compareToIgnoreCase("on") == 0 ||
233                str.compareToIgnoreCase("1") == 0)
234                return true;
235             else if(str.compareToIgnoreCase("false") == 0 ||
236                     str.compareToIgnoreCase("off") == 0 ||
237                     str.compareToIgnoreCase("0") == 0)
238                     return false;
239         }
240         return false;
241         //throw new ParseException("Key has an unknown value",0);
242
}
243
244     /**
245      * Used for getting int's.
246      *
247      * @throws ParseException if the String couldn't have been converted.
248      * @return int-value representing the key's converted value.
249      */

250     public int getIntByKey(String JavaDoc key) throws /*ParseException, */NumberFormatException JavaDoc
251     {
252         log("getIntByKey("+key+")");
253 // if(isEmptyKey(key))
254
// throw new ParseException("Key is empty",0);
255

256         return new Integer JavaDoc(getStringByKey(key)).intValue();
257     }
258     
259     public String JavaDoc getStringValue(Element e, String JavaDoc key)
260     {
261         log("checking for key "+key);
262         StringTokenizer st = new StringTokenizer(key, ".");
263         String JavaDoc k = st.nextToken();
264         String JavaDoc rest="";
265         while(st.hasMoreTokens())
266         {
267             rest=rest.length()>0?rest+"."+st.nextToken():st.nextToken();
268         }
269         
270         
271         if(rest.length()==0)
272         {
273             // key is last, check if attribute exist
274
if(e.getAttribute(k)!=null)
275             {
276                 // yes return value
277
return e.getAttribute(k).getValue();
278             }
279             
280             // no attribute matches the key. try subitem
281
List l = e.getChildren(k);
282             Iterator i = l.iterator();
283
284             while (i.hasNext())
285             {
286                 Element ee = (Element) i.next();
287                 
288                 if(ee!=null)
289                 {
290                     return ee.getText();
291                 }
292                 else
293                 {
294                     return null;
295                 }
296             }
297         }
298         
299         // we have subkeys to check
300
List l = e.getChildren(k);
301         Iterator i = l.iterator();
302
303         while (i.hasNext())
304         {
305             Element ee = (Element) i.next();
306
307             if(ee!=null)
308             {
309                 
310                 return getStringValue(ee, rest);
311             }
312             else
313             {
314                 return null;
315             }
316         }
317         
318         return null;
319         
320     }
321 }
322
Popular Tags