aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/configuration/Configuration.java
blob: 298a10e2aad835650bc67484c1b9a26b6055b98c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.fop.messaging.MessageHandler;

/**
 * a configuration class for all general configuration aspects except those
 * related to specific renderers. All configuration is stored
 * in key / value pairs. The value can be a String, a list of Strings
 * or a map, containing a list of key / value pairs.
 *
 */
public class Configuration {

    /**
     * defines role types
     */
    public final static int STANDARD = 0;
    public final static int PDF = 1;
    public final static int AWT = 2;

    /**
     * stores the configuration information
     */
    private static HashMap standardConfiguration = new HashMap(30);
    private static HashMap pdfConfiguration = new HashMap(20);
    private static HashMap awtConfiguration = new HashMap(20);

    /**
     * contains a HashMap of existing HashMaps
     */
    private static HashMap configuration = new HashMap(3);

    /**
     * loads the configuration types into the configuration HashMap
     */
    static {
        configuration.put("standard", standardConfiguration);
        configuration.put("pdf", pdfConfiguration);
        configuration.put("awt", awtConfiguration);
    }

    public static HashMap getConfiguration() {
        return configuration;
    }

    /**
     * general access method
     *
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return Object containing the value; normally you would use one of the
     * convenience methods, which return the correct form.
     * null   if the key is not defined.
     */
    public static Object getValue(String key, int role) {
        switch (role) {
        case Configuration.STANDARD:
            return standardConfiguration.get(key);
        case Configuration.PDF:
            return pdfConfiguration.get(key);
        case Configuration.AWT:
            return awtConfiguration.get(key);
        default:
            return standardConfiguration.get(key);
        }
    }

    /**
     * convenience methods to access strings values in the configuration
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return String a string containing the value
     * null   if the key is not defined.
     */
    public static String getStringValue(String key, int role) {
        Object obj = Configuration.getValue(key, role);
        if (obj instanceof String) {
            return (String)obj;
        } else {
            return null;
        }
    }

    /**
     * convenience methods to access int values in the configuration
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return int a int containing the value
     * -1   if the key is not defined.
     */
    public static int getIntValue(String key, int role) {
        Object obj = Configuration.getValue(key, role);
        if (obj instanceof String) {
            return Integer.parseInt((String)obj);
        } else {
            return -1;
        }
    }

    /**
     * convenience methods to access boolean values in the configuration
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return Boolean true or false as value
     * null   if the key is not defined.
     */
    public static Boolean getBooleanValue(String key, int role) {
        Object obj = Configuration.getValue(key, role);
        if (obj instanceof String) {
            return new Boolean((String)obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj;
        } else {
            return null;
        }
    }

    /**
     * convenience methods to access list values in the configuration
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return ArrayList a ArrayList containing the values
     * null   if the key is not defined.
     */
    public static ArrayList getListValue(String key, int role) {
        Object obj = Configuration.getValue(key, role);
        if (obj instanceof ArrayList) {
            return (ArrayList)obj;
        } else {
            return null;
        }
    }

    /**
     * convenience methods to access hashmap values in the configuration
     * @param key a string containing the key value for the configuration value
     * role detemines the configuration target
     * @return HashMap a HashMap containing the values
     * null   if the key is not defined.
     */
    public static HashMap getHashMapValue(String key, int role) {
        Object obj = Configuration.getValue(key, role);
        if (obj instanceof HashMap) {
            return (HashMap)obj;
        } else {
            return null;
        }
    }

    /**
     * convenience method which retrieves some configuration information
     * from the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return Object containing the value; normally you would use one of the
     * convenience methods, which return the correct form.
     * null   if the key is not defined.
     */
    public static Object getValue(String key) {
        return Configuration.getValue(key, Configuration.STANDARD);
    }

    /**
     * convenience methods to access strings values in the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return String a string containing the value
     * null   if the key is not defined.
     */
    public static String getStringValue(String key) {
        return Configuration.getStringValue(key, Configuration.STANDARD);
    }

    /**
     * convenience methods to access int values in the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return int a int containing the value
     * -1   if the key is not defined.
     */
    public static int getIntValue(String key) {
        return Configuration.getIntValue(key, Configuration.STANDARD);
    }

    /**
     * convenience methods to access boolean values in the configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return boolean true or false as value
     * null   if the key is not defined.
     */
    public static Boolean getBooleanValue(String key) {
        return Configuration.getBooleanValue(key, Configuration.STANDARD);
    }

    /**
     * convenience methods to access list values in the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return ArrayList a ArrayList containing the values
     * null   if the key is not defined.
     */
    public static ArrayList getListValue(String key) {
        return Configuration.getListValue(key, Configuration.STANDARD);
    }

    /**
     * convenience methods to access hashmap values in the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return HashMap a HashMap containing the values
     * null   if the key is not defined.
     */
    public static HashMap getHashMapValue(String key) {
        return Configuration.getHashMapValue(key, Configuration.STANDARD);
    }


    /**
     * method to access fonts values in the standard configuration
     *
     * @param key a string containing the key value for the configuration value
     * @return HashMap a HashMap containing the values
     * null   if the key is not defined.
     */
    public static ArrayList getFonts() {
        return (ArrayList)Configuration.getValue("fonts",
                                              Configuration.STANDARD);
    }

    /**
     * initializes this configuration
     * @param config contains the configuration information
     */
    public static void setup(int role, HashMap config) {
        switch (role) {
        case Configuration.STANDARD:
            standardConfiguration = config;
            break;
        case Configuration.PDF:
            pdfConfiguration = config;
            break;
        case Configuration.AWT:
            awtConfiguration = config;
            break;
        default:
            MessageHandler.errorln("Can't setup configuration. Unknown configuration role/target");
        }
    }

    /**
     * adds information to the configuration hashmap in key,value form
     * @param key a string containing the key value for the configuration value
     * value the configuration information
     * role detemines the configuration target
     * @param value an Object containing the value; can be a String, a ArrayList or a HashMap
     */
    public static void put(String key, Object value, int role) {
        switch (role) {
        case Configuration.STANDARD:
            standardConfiguration.put(key, value);
            break;
        case Configuration.PDF:
            pdfConfiguration.put(key, value);
            break;
        case Configuration.AWT:
            awtConfiguration.put(key, value);
            break;
        default:
            standardConfiguration.put(key, value);
            MessageHandler.errorln("Unknown role for new configuration entry. "
                                   + "Putting key:" + key + " - value:"
                                   + value + " into standard configuration.");
        }
    }

    ;

    /**
     * adds information to the standard configuration hashmap in key,value form
     * @param key a string containing the key value for the configuration value
     * value the configuration information
     * role detemines the configuration target
     * @param value an Object containing the value; can be a String, a ArrayList or a HashMap
     */

    public static void put(String key, Object value) {
        Configuration.put(key, value, Configuration.STANDARD);
    }

    /**
     * debug methods, which writes out all information in this configuration
     */
    public static void dumpConfiguration() {
        String key;
        Object value;
        ArrayList list;
        HashMap map, configuration;
        String tmp;
        System.out.println("Dumping configuration: ");
        HashMap[] configs = {
            standardConfiguration, pdfConfiguration, awtConfiguration
        };
        for (int i = 0; i < configs.length; i++) {
            MessageHandler.logln("----------------------");
            configuration = configs[i];
            Iterator iterator = configuration.keySet().iterator();
            while (iterator.hasNext()) {
                key = (String)iterator.next();
                MessageHandler.logln("key: " + key);
                value = configuration.get(key);
                if (value instanceof String) {
                    MessageHandler.logln("   value: " + value);
                } else if (value instanceof ArrayList) {
                    list = (ArrayList)value;
                    MessageHandler.log("   values: ");
                    for (int count = 0; count < list.size(); count++) {
                        MessageHandler.log(list.get(count) + " - ");
                    }
                    MessageHandler.logln("");
                } else if (value instanceof HashMap) {
                    map = (HashMap)value;
                    Iterator iter = map.keySet().iterator();
                    MessageHandler.log("   values: ");
                    while (iter.hasNext()) {
                        tmp = (String)iter.next();
                        MessageHandler.log(" " + tmp + ":" + map.get(tmp));
                    }
                    MessageHandler.logln("");
                }
            }
        }
    }



}