KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > configuration > Configuration


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

51 package org.apache.fop.configuration;
52
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.io.File JavaDoc;
57 import java.net.URL JavaDoc;
58 import org.apache.fop.messaging.MessageHandler;
59
60 /**
61  * a configuration class for all general configuration aspects except those
62  * related to specific renderers. All configuration is stored
63  * in key / value pairs. The value can be a String, a list of Strings
64  * or a map, containing a list of key / value pairs.
65  *
66  */

67 public class Configuration {
68
69     /**
70      * defines role types
71      */

72     public static final int STANDARD = 0;
73     public static final int PDF = 1;
74     public static final int AWT = 2;
75
76     /**
77      * stores the configuration information
78      */

79     private static Map JavaDoc standardConfiguration = new java.util.HashMap JavaDoc(30);
80     private static Map JavaDoc pdfConfiguration = new java.util.HashMap JavaDoc(20);
81     private static Map JavaDoc awtConfiguration = new java.util.HashMap JavaDoc(20);
82
83     /**
84      * contains a Map of existing Maps
85      */

86     private static Map JavaDoc configuration = new java.util.HashMap JavaDoc(3);
87
88     //URL cache
89
private static URL JavaDoc cachedBaseURL = null;
90     private static URL JavaDoc cachedFontBaseURL = null;
91
92     /**
93      * loads the configuration types into the configuration Map
94      */

95     static {
96         configuration.put("standard", standardConfiguration);
97         configuration.put("pdf", pdfConfiguration);
98         configuration.put("awt", awtConfiguration);
99     }
100
101     public static Map JavaDoc getConfiguration() {
102         return configuration;
103     }
104
105     /**
106      * general access method
107      *
108      * @param key a string containing the key value for the configuration value
109      * @param role detemines the configuration target
110      * @return Object containing the value; normally you would use one of the
111      * convenience methods, which return the correct form,
112      * null if the key is not defined.
113      */

114     public static Object JavaDoc getValue(String JavaDoc key, int role) {
115         switch (role) {
116         case Configuration.STANDARD:
117             return standardConfiguration.get(key);
118         case Configuration.PDF:
119             return pdfConfiguration.get(key);
120         case Configuration.AWT:
121             return awtConfiguration.get(key);
122         default:
123             return standardConfiguration.get(key);
124         }
125     }
126
127
128     /**
129      * convenience methods to access strings values in the configuration
130      * @param key a string containing the key value for the configuration value
131      * @param role detemines the configuration target
132      * @return String a string containing the value,
133      * null if the key is not defined.
134      */

135     public static String JavaDoc getStringValue(String JavaDoc key, int role) {
136         Object JavaDoc obj = Configuration.getValue(key, role);
137         if (obj instanceof String JavaDoc) {
138             return (String JavaDoc)obj;
139         } else {
140             return null;
141         }
142     }
143
144
145     /**
146      * convenience methods to access int values in the configuration
147      * @param key a string containing the key value for the configuration value
148      * @param role detemines the configuration target
149      * @return int a int containing the value,
150      * -1 if the key is not defined.
151      */

152     public static int getIntValue(String JavaDoc key, int role) {
153         Object JavaDoc obj = Configuration.getValue(key, role);
154         if (obj instanceof String JavaDoc) {
155             return Integer.parseInt((String JavaDoc)obj);
156         } else {
157             return -1;
158         }
159     }
160
161
162     /**
163      * convenience methods to access boolean values in the configuration
164      * @param key a string containing the key value for the configuration value
165      * @param role detemines the configuration target
166      * @return Boolean true or false as value,
167      * null if the key is not defined.
168      */

169     public static Boolean JavaDoc getBooleanValue(String JavaDoc key, int role) {
170         Object JavaDoc obj = Configuration.getValue(key, role);
171         if (obj instanceof String JavaDoc) {
172             return new Boolean JavaDoc((String JavaDoc)obj);
173         } else if (obj instanceof Boolean JavaDoc) {
174             return (Boolean JavaDoc)obj;
175         } else {
176             return null;
177         }
178     }
179
180
181     /**
182      * convenience methods to access list values in the configuration
183      * @param key a string containing the key value for the configuration value
184      * @param role detemines the configuration target
185      * @return List a List containing the values,
186      * null if the key is not defined.
187      */

188     public static List JavaDoc getListValue(String JavaDoc key, int role) {
189         Object JavaDoc obj = Configuration.getValue(key, role);
190         if (obj instanceof List JavaDoc) {
191             return (List JavaDoc)obj;
192         } else {
193             return null;
194         }
195     }
196
197
198     /**
199      * convenience methods to access Map values in the configuration
200      * @param key a string containing the key value for the configuration value
201      * @param role detemines the configuration target
202      * @return Map a Map containing the values
203      * null if the key is not defined.
204      */

205     public static Map JavaDoc getMapValue(String JavaDoc key, int role) {
206         Object JavaDoc obj = Configuration.getValue(key, role);
207         if (obj instanceof Map JavaDoc) {
208             return (Map JavaDoc)obj;
209         } else {
210             return null;
211         }
212     }
213
214
215     /**
216      * convenience method which retrieves some configuration information
217      * from the standard configuration
218      *
219      * @param key a string containing the key value for the configuration value
220      * @return Object containing the value; normally you would use one of the
221      * convenience methods, which return the correct form.
222      * null if the key is not defined.
223      */

224     public static Object JavaDoc getValue(String JavaDoc key) {
225         return Configuration.getValue(key, Configuration.STANDARD);
226     }
227
228     /**
229      * convenience methods to access strings values in the standard configuration
230      *
231      * @param key a string containing the key value for the configuration value
232      * @return String a string containing the value
233      * null if the key is not defined.
234      */

235     public static String JavaDoc getStringValue(String JavaDoc key) {
236         return Configuration.getStringValue(key, Configuration.STANDARD);
237     }
238
239     /**
240      * convenience methods to access int values in the standard configuration
241      *
242      * @param key a string containing the key value for the configuration value
243      * @return int a int containing the value,
244      * -1 if the key is not defined.
245      */

246     public static int getIntValue(String JavaDoc key) {
247         return Configuration.getIntValue(key, Configuration.STANDARD);
248     }
249
250     /**
251      * convenience methods to access boolean values in the configuration
252      *
253      * @param key a string containing the key value for the configuration value
254      * @return boolean true or false as value,
255      * null if the key is not defined.
256      */

257     public static Boolean JavaDoc getBooleanValue(String JavaDoc key) {
258         return Configuration.getBooleanValue(key, Configuration.STANDARD);
259     }
260
261     /**
262      * convenience methods to access list values in the standard configuration
263      *
264      * @param key a string containing the key value for the configuration value
265      * @return List a List containing the values,
266      * null if the key is not defined.
267      */

268     public static List JavaDoc getListValue(String JavaDoc key) {
269         return Configuration.getListValue(key, Configuration.STANDARD);
270     }
271
272     /**
273      * convenience methods to access Map values in the standard configuration
274      *
275      * @param key a string containing the key value for the configuration value
276      * @return Map a Map containing the values,
277      * null if the key is not defined.
278      */

279     public static Map JavaDoc getMapValue(String JavaDoc key) {
280         return Configuration.getMapValue(key, Configuration.STANDARD);
281     }
282
283
284     /**
285      * Method to access fonts values in the standard configuration
286      *
287      * @return List a List containing the values,
288      * null if the key is not defined.
289      */

290     public static List JavaDoc getFonts() {
291         return (List JavaDoc)Configuration.getValue("fonts",
292                                               Configuration.STANDARD);
293     }
294
295
296     private static URL JavaDoc buildBaseURL(String JavaDoc directory) throws java.net.MalformedURLException JavaDoc {
297         if (directory == null) return null;
298         File JavaDoc dir = new File JavaDoc(directory);
299         if (dir.isDirectory()) {
300             return dir.toURL();
301         } else {
302             URL JavaDoc baseURL = new URL JavaDoc(directory);
303             return baseURL;
304         }
305     }
306
307     public static URL JavaDoc getBaseURL() {
308         if (cachedBaseURL != null) {
309             return cachedBaseURL;
310         } else {
311             String JavaDoc baseDir = getStringValue("baseDir");
312             try {
313                 URL JavaDoc url = buildBaseURL(baseDir);;
314                 cachedBaseURL = url;
315                 return url;
316             } catch (java.net.MalformedURLException JavaDoc mfue) {
317                 throw new RuntimeException JavaDoc("Invalid baseDir specified: "+baseDir+" ("+mfue.getMessage()+")");
318             }
319         }
320     }
321
322
323     public static URL JavaDoc getFontBaseURL() {
324         if (cachedFontBaseURL != null) {
325             return cachedFontBaseURL;
326         } else {
327             URL JavaDoc url = null;
328             String JavaDoc baseDir = getStringValue("fontBaseDir");
329             if (baseDir == null) {
330                 url = getBaseURL();
331             } else {
332                 try {
333                     url = buildBaseURL(baseDir);
334                 } catch (java.net.MalformedURLException JavaDoc mfue) {
335                     throw new RuntimeException JavaDoc("Invalid fontBaseDir specified: "+baseDir+" ("+mfue.getMessage()+")");
336                 }
337             }
338             cachedFontBaseURL = url;
339             return url;
340         }
341     }
342
343     /**
344      * Initializes this configuration
345      * @param role detemines the configuration target
346      * @param config contains the configuration information
347      */

348     public static void setup(int role, Map JavaDoc config) {
349         switch (role) {
350         case Configuration.STANDARD:
351             standardConfiguration = config;
352             break;
353         case Configuration.PDF:
354             pdfConfiguration = config;
355             break;
356         case Configuration.AWT:
357             awtConfiguration = config;
358             break;
359         default:
360             MessageHandler.errorln("Can't setup configuration. Unknown configuration role/target");
361         }
362         invalidateURLCache();
363     }
364
365     /**
366      * adds information to the configuration Map in key,value form
367      * @param key a string containing the key value for the configuration value
368      * @param value the configuration information; can be a String, a List or a Map
369      * @param role detemines the configuration target
370      */

371     public static void put(String JavaDoc key, Object JavaDoc value, int role) {
372         switch (role) {
373         case Configuration.STANDARD:
374             standardConfiguration.put(key, value);
375             break;
376         case Configuration.PDF:
377             pdfConfiguration.put(key, value);
378             break;
379         case Configuration.AWT:
380             awtConfiguration.put(key, value);
381             break;
382         default:
383             standardConfiguration.put(key, value);
384             MessageHandler.errorln("Unknown role for new configuration entry. "
385                                    + "Putting key:" + key + " - value:"
386                                    + value + " into standard configuration.");
387         }
388         invalidateURLCache();
389     }
390
391     /**
392      * adds information to the standard configuration Map in key,value form
393      * @param key a string containing the key value for the configuration value
394      * value the configuration information
395      * role detemines the configuration target
396      * @param value an Object containing the value; can be a String, a List or a Map
397      */

398
399     public static void put(String JavaDoc key, Object JavaDoc value) {
400         Configuration.put(key, value, Configuration.STANDARD);
401     }
402
403     private static void invalidateURLCache() {
404         cachedBaseURL = null;
405         cachedFontBaseURL = null;
406     }
407
408     /**
409      * debug methods, which writes out all information in this configuration
410      */

411     public static void dumpConfiguration() {
412         String JavaDoc key;
413         Object JavaDoc value;
414         List JavaDoc list;
415         Map JavaDoc map, configuration;
416         String JavaDoc tmp;
417         System.out.println("Dumping configuration: ");
418         Map JavaDoc[] configs = {
419             standardConfiguration, pdfConfiguration, awtConfiguration
420         };
421         for (int i = 0; i < configs.length; i++) {
422             MessageHandler.logln("----------------------");
423             configuration = configs[i];
424             Iterator JavaDoc iterator = configuration.keySet().iterator();
425             while (iterator.hasNext()) {
426                 key = (String JavaDoc)iterator.next();
427                 MessageHandler.logln("key: " + key);
428                 value = configuration.get(key);
429                 if (value instanceof String JavaDoc) {
430                     MessageHandler.logln(" value: " + value);
431                 } else if (value instanceof List JavaDoc) {
432                     list = (List JavaDoc)value;
433                     MessageHandler.log(" values: ");
434                     for (int j = 0; j < list.size(); j++) {
435                         MessageHandler.log(list.get(j) + " - ");
436                     }
437                     MessageHandler.logln("");
438                 } else if (value instanceof Map JavaDoc) {
439                     map = (Map JavaDoc)value;
440                     MessageHandler.log(" values: ");
441                     Iterator JavaDoc it2 = map.keySet().iterator();
442                     while (it2.hasNext()) {
443                         tmp = (String JavaDoc)it2.next();
444                         MessageHandler.log(" " + tmp + ":" + map.get(tmp));
445                     }
446                     MessageHandler.logln("");
447                 }
448             }
449         }
450     }
451
452
453
454 }
455
456
Popular Tags