*/
public class Configuration {
- protected static final Logger logger = Logger.getLogger(Fop.fopPackage);
+ protected final Logger logger = Logger.getLogger(Fop.fopPackage);
/**
* defines role types
*/
/**
* 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;
}
* 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);
* @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 {
* @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) {
/**
- * 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) {
}
}
+ /**
+ * 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
* @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 {
* @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 {
* if the map is not defined, the <i>key</i>entry is not defined,
* or the <i>key</i>entry is itself <tt>null</tt>.
*/
- 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);
* 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);
}
/**
* @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);
}
/**
* @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();
}
/**
* @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);
}
/**
* @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);
}
* if the map is not defined, the <i>key</i>entry is not defined,
* or the <i>key</i>entry is itself <tt>null</tt>.
*/
- 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);
}
* @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);
}
* 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) {
}
/**
* @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);
* 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;
/**
- * 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 {
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
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";
// 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();
}
/**
// 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");
}
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")) {
} // 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.");
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: <br>
- *
- * <code>ConfigurationReader reader = new ConfigurationReader ("config.xml","standard");
- * try {
- * reader.start();
- * } catch (org.apache.fop.apps.FOPException error) {
- * reader.dumpError(error);
- * }
- * </code>
- * 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
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 {
*
* @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();
}
}
- /**
- * 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;
- }
-
}