1 51 package org.apache.fop.configuration; 52 53 import java.util.List ; 54 import java.util.Map ; 55 import java.util.Iterator ; 56 import java.io.File ; 57 import java.net.URL ; 58 import org.apache.fop.messaging.MessageHandler; 59 60 67 public class Configuration { 68 69 72 public static final int STANDARD = 0; 73 public static final int PDF = 1; 74 public static final int AWT = 2; 75 76 79 private static Map standardConfiguration = new java.util.HashMap (30); 80 private static Map pdfConfiguration = new java.util.HashMap (20); 81 private static Map awtConfiguration = new java.util.HashMap (20); 82 83 86 private static Map configuration = new java.util.HashMap (3); 87 88 private static URL cachedBaseURL = null; 90 private static URL cachedFontBaseURL = null; 91 92 95 static { 96 configuration.put("standard", standardConfiguration); 97 configuration.put("pdf", pdfConfiguration); 98 configuration.put("awt", awtConfiguration); 99 } 100 101 public static Map getConfiguration() { 102 return configuration; 103 } 104 105 114 public static Object getValue(String 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 135 public static String getStringValue(String key, int role) { 136 Object obj = Configuration.getValue(key, role); 137 if (obj instanceof String ) { 138 return (String )obj; 139 } else { 140 return null; 141 } 142 } 143 144 145 152 public static int getIntValue(String key, int role) { 153 Object obj = Configuration.getValue(key, role); 154 if (obj instanceof String ) { 155 return Integer.parseInt((String )obj); 156 } else { 157 return -1; 158 } 159 } 160 161 162 169 public static Boolean getBooleanValue(String key, int role) { 170 Object obj = Configuration.getValue(key, role); 171 if (obj instanceof String ) { 172 return new Boolean ((String )obj); 173 } else if (obj instanceof Boolean ) { 174 return (Boolean )obj; 175 } else { 176 return null; 177 } 178 } 179 180 181 188 public static List getListValue(String key, int role) { 189 Object obj = Configuration.getValue(key, role); 190 if (obj instanceof List ) { 191 return (List )obj; 192 } else { 193 return null; 194 } 195 } 196 197 198 205 public static Map getMapValue(String key, int role) { 206 Object obj = Configuration.getValue(key, role); 207 if (obj instanceof Map ) { 208 return (Map )obj; 209 } else { 210 return null; 211 } 212 } 213 214 215 224 public static Object getValue(String key) { 225 return Configuration.getValue(key, Configuration.STANDARD); 226 } 227 228 235 public static String getStringValue(String key) { 236 return Configuration.getStringValue(key, Configuration.STANDARD); 237 } 238 239 246 public static int getIntValue(String key) { 247 return Configuration.getIntValue(key, Configuration.STANDARD); 248 } 249 250 257 public static Boolean getBooleanValue(String key) { 258 return Configuration.getBooleanValue(key, Configuration.STANDARD); 259 } 260 261 268 public static List getListValue(String key) { 269 return Configuration.getListValue(key, Configuration.STANDARD); 270 } 271 272 279 public static Map getMapValue(String key) { 280 return Configuration.getMapValue(key, Configuration.STANDARD); 281 } 282 283 284 290 public static List getFonts() { 291 return (List )Configuration.getValue("fonts", 292 Configuration.STANDARD); 293 } 294 295 296 private static URL buildBaseURL(String directory) throws java.net.MalformedURLException { 297 if (directory == null) return null; 298 File dir = new File (directory); 299 if (dir.isDirectory()) { 300 return dir.toURL(); 301 } else { 302 URL baseURL = new URL (directory); 303 return baseURL; 304 } 305 } 306 307 public static URL getBaseURL() { 308 if (cachedBaseURL != null) { 309 return cachedBaseURL; 310 } else { 311 String baseDir = getStringValue("baseDir"); 312 try { 313 URL url = buildBaseURL(baseDir);; 314 cachedBaseURL = url; 315 return url; 316 } catch (java.net.MalformedURLException mfue) { 317 throw new RuntimeException ("Invalid baseDir specified: "+baseDir+" ("+mfue.getMessage()+")"); 318 } 319 } 320 } 321 322 323 public static URL getFontBaseURL() { 324 if (cachedFontBaseURL != null) { 325 return cachedFontBaseURL; 326 } else { 327 URL url = null; 328 String baseDir = getStringValue("fontBaseDir"); 329 if (baseDir == null) { 330 url = getBaseURL(); 331 } else { 332 try { 333 url = buildBaseURL(baseDir); 334 } catch (java.net.MalformedURLException mfue) { 335 throw new RuntimeException ("Invalid fontBaseDir specified: "+baseDir+" ("+mfue.getMessage()+")"); 336 } 337 } 338 cachedFontBaseURL = url; 339 return url; 340 } 341 } 342 343 348 public static void setup(int role, Map 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 371 public static void put(String key, Object 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 398 399 public static void put(String key, Object value) { 400 Configuration.put(key, value, Configuration.STANDARD); 401 } 402 403 private static void invalidateURLCache() { 404 cachedBaseURL = null; 405 cachedFontBaseURL = null; 406 } 407 408 411 public static void dumpConfiguration() { 412 String key; 413 Object value; 414 List list; 415 Map map, configuration; 416 String tmp; 417 System.out.println("Dumping configuration: "); 418 Map [] configs = { 419 standardConfiguration, pdfConfiguration, awtConfiguration 420 }; 421 for (int i = 0; i < configs.length; i++) { 422 MessageHandler.logln("----------------------"); 423 configuration = configs[i]; 424 Iterator iterator = configuration.keySet().iterator(); 425 while (iterator.hasNext()) { 426 key = (String )iterator.next(); 427 MessageHandler.logln("key: " + key); 428 value = configuration.get(key); 429 if (value instanceof String ) { 430 MessageHandler.logln(" value: " + value); 431 } else if (value instanceof List ) { 432 list = (List )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 ) { 439 map = (Map )value; 440 MessageHandler.log(" values: "); 441 Iterator it2 = map.keySet().iterator(); 442 while (it2.hasNext()) { 443 tmp = (String )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 |