From a242e9ab885feda0ea0e38923b15ec1af3a5b04c Mon Sep 17 00:00:00 2001 From: Peter Bernard West Date: Sat, 6 Mar 2004 05:59:10 +0000 Subject: [PATCH] Made Configuration instance. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP_0-20-0_Alt-Design@197416 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/configuration/Configuration.java | 149 +++++++++++------- .../configuration/ConfigurationParser.java | 35 ++-- .../configuration/ConfigurationReader.java | 78 ++------- 3 files changed, 125 insertions(+), 137 deletions(-) diff --git a/src/java/org/apache/fop/configuration/Configuration.java b/src/java/org/apache/fop/configuration/Configuration.java index 7a611fdfe..31d9a3af9 100644 --- a/src/java/org/apache/fop/configuration/Configuration.java +++ b/src/java/org/apache/fop/configuration/Configuration.java @@ -36,7 +36,7 @@ import org.apache.fop.apps.Fop; */ public class Configuration { - protected static final Logger logger = Logger.getLogger(Fop.fopPackage); + protected final Logger logger = Logger.getLogger(Fop.fopPackage); /** * defines role types */ @@ -47,25 +47,45 @@ public class Configuration { /** * 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); + private HashMap standardConfiguration = new HashMap(); + private HashMap pdfConfiguration = new HashMap(); + private HashMap awtConfiguration = new HashMap(); /** * contains a HashMap of existing HashMaps */ - private static HashMap configuration = new HashMap(3); - - /** - * loads the configuration types into the configuration HashMap - */ - static { + private HashMap configuration = new HashMap(3); + + public Configuration() { configuration.put("standard", standardConfiguration); configuration.put("pdf", pdfConfiguration); configuration.put("awt", awtConfiguration); } - public static HashMap getConfiguration() { + public Configuration(int role, HashMap config) { + this(); + setRole(role, config); + } + + public void setRole(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: + logger.warning( + "Can't setup configuration. Unknown configuration role/target" + ); + } + } + + public HashMap getConfiguration() { return configuration; } @@ -78,7 +98,7 @@ public class Configuration { * convenience methods, which return the correct form. * null if the key is not defined. */ - public static Object getValue(String key, int role) { + public Object getValue(String key, int role) { switch (role) { case Configuration.STANDARD: return standardConfiguration.get(key); @@ -99,8 +119,8 @@ public class Configuration { * @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); + public String getStringValue(String key, int role) { + Object obj = getValue(key, role); if (obj instanceof String) { return (String)obj; } else { @@ -116,8 +136,8 @@ public class Configuration { * @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); + public int getIntValue(String key, int role) { + Object obj = getValue(key, role); if (obj instanceof String) { return Integer.parseInt((String)obj); } else if (obj instanceof Integer) { @@ -129,14 +149,14 @@ public class Configuration { /** - * convenience methods to access boolean values in the configuration + * convenience methods to access Boolean values in the configuration * @param key a string containing the key for the configuration value * role determines 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); + public Boolean getBooleanObject(String key, int role) { + Object obj = getValue(key, role); if (obj instanceof String) { return new Boolean((String)obj); } else if (obj instanceof Boolean) { @@ -146,6 +166,17 @@ public class Configuration { } } + /** + * Convenience method for accessing boolean values in the configuration + * @param key the key for the configuration entry + * @param role the configuration target + * @return the boolean value of the key value if defined, or false + */ + public boolean isTrue(String key, int role) { + Boolean bval = getBooleanObject(key, role); + if (bval == null) return false; + return bval.booleanValue(); + } /** * convenience methods to access list values in the configuration @@ -154,8 +185,8 @@ public class Configuration { * @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); + public ArrayList getListValue(String key, int role) { + Object obj = getValue(key, role); if (obj instanceof ArrayList) { return (ArrayList)obj; } else { @@ -171,8 +202,8 @@ public class Configuration { * @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); + public HashMap getHashMapValue(String key, int role) { + Object obj = getValue(key, role); if (obj instanceof HashMap) { return (HashMap)obj; } else { @@ -194,7 +225,7 @@ public class Configuration { * if the map is not defined, the keyentry is not defined, * or the keyentry is itself null. */ - public static Object getHashMapEntry(String map, Object key, int role) { + public Object getHashMapEntry(String map, Object key, int role) { Object obj = getValue(map, role); if (obj instanceof HashMap) { return ((HashMap)obj).get(key); @@ -213,8 +244,8 @@ public class Configuration { * 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); + public Object getValue(String key) { + return getValue(key, Configuration.STANDARD); } /** @@ -225,8 +256,8 @@ public class Configuration { * @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); + public String getStringValue(String key) { + return getStringValue(key, Configuration.STANDARD); } /** @@ -236,19 +267,30 @@ public class Configuration { * @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); + public int getIntValue(String key) { + return getIntValue(key, Configuration.STANDARD); } /** - * convenience methods to access boolean values in the configuration + * convenience methods to access Boolean values in the configuration * * @param key a string containing the key 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); + public Boolean getBooleanObject(String key) { + return getBooleanObject(key, Configuration.STANDARD); + } + + /** + * Convenience method for accessing boolean values in the configuration + * @param key the key for the configuration entry + * @return the boolean value of the key value if defined, or false + */ + public boolean isTrue(String key) { + Boolean bval = getBooleanObject(key); + if (bval == null) return false; + return bval.booleanValue(); } /** @@ -258,8 +300,8 @@ public class Configuration { * @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); + public ArrayList getListValue(String key) { + return getListValue(key, Configuration.STANDARD); } /** @@ -270,8 +312,8 @@ public class Configuration { * @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); + public HashMap getHashMapValue(String key) { + return getHashMapValue(key, Configuration.STANDARD); } @@ -287,8 +329,8 @@ public class Configuration { * if the map is not defined, the keyentry is not defined, * or the keyentry is itself null. */ - public static Object getHashMapEntry(String map, Object key) { - return Configuration.getHashMapEntry(map, key, STANDARD); + public Object getHashMapEntry(String map, Object key) { + return getHashMapEntry(map, key, STANDARD); } @@ -298,8 +340,8 @@ public class Configuration { * @return HashMap a HashMap containing the values * null if the key is not defined. */ - public static ArrayList getFonts() { - return (ArrayList)Configuration.getValue("fonts", + public ArrayList getFonts() { + return (ArrayList)getValue("fonts", Configuration.STANDARD); } @@ -307,22 +349,7 @@ public class Configuration { * 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: - logger.warning( - "Can't setup configuration. Unknown configuration role/target" - ); - } + public void setup(int role, HashMap config) { } /** @@ -333,7 +360,7 @@ public class Configuration { * @param value an Object containing the value; * can be a String, a Boolean, and Integer, an ArrayList or a HashMap */ - public static void put(String key, Object value, int role) { + public void put(String key, Object value, int role) { switch (role) { case Configuration.STANDARD: standardConfiguration.put(key, value); @@ -362,14 +389,14 @@ public class Configuration { * can be a String, a Boolean, an Integer, an ArrayList or a HashMap */ - public static void put(String key, Object value) { - Configuration.put(key, value, Configuration.STANDARD); + public void put(String key, Object value) { + put(key, value, Configuration.STANDARD); } /** * debug methods, which writes out all information in this configuration */ - public static void dumpConfiguration() { + public void dumpConfiguration() { String key; Object value; ArrayList list; diff --git a/src/java/org/apache/fop/configuration/ConfigurationParser.java b/src/java/org/apache/fop/configuration/ConfigurationParser.java index d0a1240cd..cf912baa0 100644 --- a/src/java/org/apache/fop/configuration/ConfigurationParser.java +++ b/src/java/org/apache/fop/configuration/ConfigurationParser.java @@ -33,7 +33,7 @@ import java.util.ArrayList; /** - * SAX2 Handler which retrieves the configuration information and stores them in Configuration. + * SAX2 Handler which retrieves the configMaps information and stores them in Configuration. * Normally this class doesn't need to be accessed directly. */ public class ConfigurationParser extends DefaultHandler { @@ -55,18 +55,18 @@ public class ConfigurationParser extends DefaultHandler { private int status = OUT; private int datatype = -1; - // store the result configuration - private static HashMap configuration; + // store the result configMaps + private static HashMap configMaps; private static HashMap activeConfiguration; - // stores key for new config entry + // stores key for new configuration entry private String key = ""; private ArrayList keyStack = new ArrayList(); // stores string value private String value = ""; - // stores key for new config entry + // stores key for new configuration entry private String subkey = ""; // stores list value @@ -81,7 +81,7 @@ public class ConfigurationParser extends DefaultHandler { private Locator locator; /** - * determines role / target of configuration information, default is standard + * determines role / target of configMaps information, default is standard */ private String role = "standard"; @@ -101,9 +101,14 @@ public class ConfigurationParser extends DefaultHandler { // information on a font triplet private String fontTripletName, weight, style; + private Configuration configuration; + + public ConfigurationParser(Configuration config) { + this.configuration = config; + } public void startDocument() { - configuration = Configuration.getConfiguration(); + configMaps = configuration.getConfiguration(); } /** @@ -130,7 +135,7 @@ public class ConfigurationParser extends DefaultHandler { // role=standard as default if (attributes.getLength() == 0) { role = "standard"; - // retrieve attribute value for "role" which determines configuration target + // retrieve attribute value for "role" which determines configMaps target } else { role = attributes.getValue("role"); } @@ -156,13 +161,13 @@ public class ConfigurationParser extends DefaultHandler { fontTriplets.add(fontTriplet); } else { // to make sure that user knows about false tag - Configuration.logger.warning("Unknown tag in configuration file: " + configuration.logger.warning("Unknown tag in configMaps file: " + localName); } } // end startElement /** - * stores subentries or entries into their hashes (map for subentries, configuration for entry) + * stores subentries or entries into their hashes (map for subentries, configMaps for entry) */ public void endElement(String uri, String localName, String qName) { if (localName.equals("entry")) { @@ -252,18 +257,20 @@ public class ConfigurationParser extends DefaultHandler { } // end characters /** - * stores configuration entry into configuration hashtable according to the role + * stores configuration entry into configuration hashtable + * according to the role * - * @param role a string containing the role / target for this configuration information + * @param role a string containing the role / target for this + * configuration information * @param key a string containing the key value for the configuration * @param value a string containing the value for the configuration */ private void store(String role, String key, Object value) { - activeConfiguration = (HashMap)configuration.get(role); + activeConfiguration = (HashMap)configMaps.get(role); if (activeConfiguration != null) { activeConfiguration.put(key, value); } else { - Configuration.logger.warning("Unknown role >" + role + configuration.logger.warning("Unknown role >" + role + "< for new configuration entry. \n" + "Putting configuration with key:" + key + " into standard configuration."); diff --git a/src/java/org/apache/fop/configuration/ConfigurationReader.java b/src/java/org/apache/fop/configuration/ConfigurationReader.java index 2c96c00ed..8ddc5adad 100644 --- a/src/java/org/apache/fop/configuration/ConfigurationReader.java +++ b/src/java/org/apache/fop/configuration/ConfigurationReader.java @@ -26,49 +26,28 @@ import org.xml.sax.SAXException; import java.io.IOException; import org.xml.sax.InputSource; -// fop -import org.apache.fop.apps.Driver; import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.Fop; /** * entry class for reading configuration from file and creating a configuration - * class. Typical use looks like:
- * - * ConfigurationReader reader = new ConfigurationReader ("config.xml","standard"); - * try { - * reader.start(); - * } catch (org.apache.fop.apps.FOPException error) { - * reader.dumpError(error); - * } - * - * Once the configuration has been setup, the information can be accessed with - * the methods of StandardConfiguration. - */ + * class. */ public class ConfigurationReader { - /** - * show a full dump on error - */ - private static boolean errorDump = false; - - /** - * inputsource for configuration file - */ + /** inputsource for configuration file */ private InputSource filename; + private Configuration configuration; /** - * creates a configuration reader + * creates a configuration reader and starts parsing of config file * @param filename the file which contains the configuration information */ - public ConfigurationReader(InputSource filename) { + public ConfigurationReader( + InputSource filename, Configuration configuration) + throws FOPException { this.filename = filename; - } - - /** - * intantiates parser and starts parsing of config file - */ - public void start() throws FOPException { + this.configuration = configuration; XMLReader parser = createParser(); // setting the parser features @@ -79,7 +58,8 @@ public class ConfigurationReader { throw new FOPException("You need a parser which supports SAX version 2", e); } - ConfigurationParser configurationParser = new ConfigurationParser(); + ConfigurationParser configurationParser = + new ConfigurationParser(configuration); parser.setContentHandler(configurationParser); try { @@ -101,13 +81,11 @@ public class ConfigurationReader { * * @return the created SAX parser */ - public static XMLReader createParser() throws FOPException { - String parserClassName = Driver.getParserClassName(); - if (errorDump) { - Configuration.logger.config( - "configuration reader using SAX parser " - + parserClassName); - } + public XMLReader createParser() throws FOPException { + String parserClassName = Fop.getParserClassName(); + configuration.logger.config( + "configuration reader using SAX parser " + + parserClassName); try { return (XMLReader)Class.forName(parserClassName).newInstance(); @@ -124,28 +102,4 @@ public class ConfigurationReader { } } - /** - * Dumps an error - */ - public void dumpError(Exception e) { - if (errorDump) { - if (e instanceof SAXException) { - e.printStackTrace(); - if (((SAXException)e).getException() != null) { - ((SAXException)e).getException().printStackTrace(); - } - } else { - e.printStackTrace(); - } - } - } - - /** - * long or short error messages - * - */ - public void setDumpError(boolean dumpError) { - errorDump = dumpError; - } - } -- 2.39.5