import java.util.Collections;
import java.util.EventObject;
+import java.util.Locale;
import java.util.Map;
import org.apache.fop.events.model.EventSeverity;
/**
* This is the default event class used by this package. Each event has a unique event identifier
- * (a String), a severity indicator and a map of name/value pairs.
+ * (a String), a severity indicator, a locale (for formatting event messages), and a map of
+ * name/value pairs.
*/
public class Event extends EventObject {
private static final long serialVersionUID = -1310594422868258083L;
private String eventGroupID;
-
private String eventKey;
-
private EventSeverity severity;
+ private Locale locale;
private Map<String, Object> params;
/**
- * Creates a new Event.
+ * Creates a new Event using default locale.
* @param source the object that creates the event
* @param eventID the unique identifier of the event
* @param severity the severity level
* @param params the event parameters (a map of name/value pairs)
*/
public Event(Object source, String eventID, EventSeverity severity, Map<String, Object> params)
+ {
+ this ( source, eventID, severity, Locale.getDefault(), params );
+ }
+
+ /**
+ * Creates a new Event.
+ * @param source the object that creates the event
+ * @param eventID the unique identifier of the event
+ * @param severity the severity level
+ * @param locale to use when formatting event (or null, which means use default locale)
+ * @param params the event parameters (a map of name/value pairs)
+ */
+ public Event(Object source, String eventID, EventSeverity severity, Locale locale, Map<String, Object> params)
{
super(source);
int pos = eventID.lastIndexOf('.');
eventKey = eventID.substring(pos + 1);
}
setSeverity(severity);
+ this.locale = locale;
this.params = params;
}
this.severity = severity;
}
+ /**
+ * Returns the locale.
+ * @return the locale
+ */
+ public Locale getLocale() {
+ return this.locale;
+ }
+
/**
* Returns a parameter.
* @param key the key to the parameter
//utility class
}
+ private static ResourceBundle getBundle ( String groupID, Locale locale ) {
+ ResourceBundle bundle;
+ String baseName = ( groupID != null ) ? groupID : EventFormatter.class.getName();
+ try {
+ ClassLoader classLoader = EventFormatter.class.getClassLoader();
+ bundle = XMLResourceBundle.getXMLBundle ( baseName, locale, classLoader );
+ } catch ( MissingResourceException e ) {
+ if ( log.isTraceEnabled() ) {
+ log.trace ( "No XMLResourceBundle for " + baseName + " available." );
+ }
+ bundle = null;
+ }
+ return bundle;
+ }
+
/**
* Formats an event using the default locale.
* @param event the event
* @return the formatted message
*/
- public static String format(Event event) {
- ResourceBundle bundle = null;
- String groupID = event.getEventGroupID();
- if (groupID != null) {
- try {
- bundle = XMLResourceBundle.getXMLBundle(
- groupID,
- EventFormatter.class.getClassLoader());
- } catch (MissingResourceException mre) {
- throw new IllegalStateException("No XMLResourceBundle for " + groupID
- + " available.");
- }
- }
- return format(event, bundle);
+ public static String format ( Event event ) {
+ return format ( event, event.getLocale() );
}
/**
* @return the formatted message
*/
public static String format(Event event, Locale locale) {
- ResourceBundle bundle = null;
- String groupID = event.getEventGroupID();
- if (groupID != null) {
- try {
- bundle = XMLResourceBundle.getXMLBundle(
- groupID, locale,
- EventFormatter.class.getClassLoader());
- } catch (MissingResourceException mre) {
- if (log.isTraceEnabled()) {
- log.trace("No XMLResourceBundle for " + groupID + " available.");
- }
- }
- }
- if (bundle == null) {
- bundle = XMLResourceBundle.getXMLBundle(
- EventFormatter.class.getName(),
- locale,
- EventFormatter.class.getClassLoader());
- }
- return format(event, bundle);
+ return format ( event, getBundle ( event.getEventGroupID(), locale ) );
}
- private static String format(Event event, ResourceBundle bundle) {
- String template = bundle.getString(event.getEventKey());
- return format(event, processIncludes(template, bundle));
+ private static String format ( Event event, ResourceBundle bundle ) {
+ assert event != null;
+ String key = event.getEventKey();
+ String template;
+ if ( bundle != null ) {
+ template = bundle.getString ( key );
+ } else {
+ template = "Missing bundle. Can't lookup event key: '" + key + "'.";
+ }
+ return format ( event, processIncludes ( template, bundle ) );
}
private static String processIncludes(String template, ResourceBundle bundle) {
private static int processIncludesInner(CharSequence template, StringBuffer sb,
ResourceBundle bundle) {
int replacements = 0;
- Matcher m = INCLUDES_PATTERN.matcher(template);
- while (m.find()) {
- String include = m.group();
- include = include.substring(2, include.length() - 2);
- m.appendReplacement(sb, bundle.getString(include));
- replacements++;
+ if ( bundle != null ) {
+ Matcher m = INCLUDES_PATTERN.matcher(template);
+ while (m.find()) {
+ String include = m.group();
+ include = include.substring(2, include.length() - 2);
+ m.appendReplacement(sb, bundle.getString(include));
+ replacements++;
+ }
+ m.appendTail(sb);
}
- m.appendTail(sb);
return replacements;
}
Map params = new java.util.HashMap(event.getParams());
params.put("source", event.getSource());
params.put("severity", event.getSeverity());
+ params.put("groupID", event.getEventGroupID());
+ params.put("locale", event.getLocale());
return format.format(params);
}
}
public void write(StringBuffer sb, Map params) {
- // TODO there's no defaultBundle anymore
-// sb.append(defaultBundle.getString(getKey(params)));
+ String groupID = (String) params.get("groupID");
+ Locale locale = (Locale) params.get("locale");
+ ResourceBundle bundle = getBundle ( groupID, locale );
+ if ( bundle != null ) {
+ sb.append(bundle.getString(getKey(params)));
+ }
}
private String getKey(Map params) {