aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/events
diff options
context:
space:
mode:
authorGlenn Adams <gadams@apache.org>2012-04-11 18:43:46 +0000
committerGlenn Adams <gadams@apache.org>2012-04-11 18:43:46 +0000
commit9ed2391ac82b8fc118a20f18a1d365c50c187669 (patch)
treedd6de60aafc84b02d5da8c43ad415521d7feb08b /src/java/org/apache/fop/events
parent59cd251daa9b13c05844c80938c765ca136a9ee5 (diff)
downloadxmlgraphics-fop-9ed2391ac82b8fc118a20f18a1d365c50c187669.tar.gz
xmlgraphics-fop-9ed2391ac82b8fc118a20f18a1d365c50c187669.zip
Bugzilla #53065: Restore lookup on Event message lookup field part. Add locale field to Event to enable locale dependent deferred bundle load during lookup field part substitution.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1324913 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/events')
-rw-r--r--src/java/org/apache/fop/events/Event.java31
-rw-r--r--src/java/org/apache/fop/events/EventFormatter.java91
2 files changed, 72 insertions, 50 deletions
diff --git a/src/java/org/apache/fop/events/Event.java b/src/java/org/apache/fop/events/Event.java
index ffec9473e..6c4553228 100644
--- a/src/java/org/apache/fop/events/Event.java
+++ b/src/java/org/apache/fop/events/Event.java
@@ -21,27 +21,28 @@ package org.apache.fop.events;
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
@@ -49,6 +50,19 @@ public class Event extends EventObject {
*/
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('.');
if (pos < 0 || pos == eventID.length() - 1) {
@@ -58,6 +72,7 @@ public class Event extends EventObject {
eventKey = eventID.substring(pos + 1);
}
setSeverity(severity);
+ this.locale = locale;
this.params = params;
}
@@ -107,6 +122,14 @@ public class Event extends EventObject {
}
/**
+ * Returns the locale.
+ * @return the locale
+ */
+ public Locale getLocale() {
+ return this.locale;
+ }
+
+ /**
* Returns a parameter.
* @param key the key to the parameter
* @return the parameter value or null if no value with this key is found
diff --git a/src/java/org/apache/fop/events/EventFormatter.java b/src/java/org/apache/fop/events/EventFormatter.java
index 072cddd24..32fd7f110 100644
--- a/src/java/org/apache/fop/events/EventFormatter.java
+++ b/src/java/org/apache/fop/events/EventFormatter.java
@@ -47,25 +47,28 @@ public final class EventFormatter {
//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() );
}
/**
@@ -75,31 +78,19 @@ public final class EventFormatter {
* @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) {
@@ -118,14 +109,16 @@ public final class EventFormatter {
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;
}
@@ -141,6 +134,8 @@ public final class EventFormatter {
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);
}
@@ -157,8 +152,12 @@ public final class EventFormatter {
}
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) {