From 9ed2391ac82b8fc118a20f18a1d365c50c187669 Mon Sep 17 00:00:00 2001 From: Glenn Adams Date: Wed, 11 Apr 2012 18:43:46 +0000 Subject: [PATCH] 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 --- src/java/org/apache/fop/events/Event.java | 31 ++++++- .../org/apache/fop/events/EventFormatter.java | 91 +++++++++---------- .../apache/fop/util/XMLResourceBundle.java | 2 +- status.xml | 4 + 4 files changed, 77 insertions(+), 51 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,33 +21,47 @@ 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 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 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 params) { super(source); int pos = eventID.lastIndexOf('.'); @@ -58,6 +72,7 @@ public class Event extends EventObject { eventKey = eventID.substring(pos + 1); } setSeverity(severity); + this.locale = locale; this.params = params; } @@ -106,6 +121,14 @@ public class Event extends EventObject { 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 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) { diff --git a/src/java/org/apache/fop/util/XMLResourceBundle.java b/src/java/org/apache/fop/util/XMLResourceBundle.java index a7444147c..7c391f16a 100644 --- a/src/java/org/apache/fop/util/XMLResourceBundle.java +++ b/src/java/org/apache/fop/util/XMLResourceBundle.java @@ -117,7 +117,7 @@ public class XMLResourceBundle extends ResourceBundle { if (baseName == null) { throw new NullPointerException("baseName must not be null"); } - + assert locale != null; ResourceBundle bundle; if (!locale.equals(Locale.getDefault())) { bundle = handleGetXMLBundle(baseName, "_" + locale, false, loader); diff --git a/status.xml b/status.xml index 000d611ea..67d915353 100644 --- a/status.xml +++ b/status.xml @@ -62,6 +62,10 @@ documents. Example: the fix of marks layering will be such a case when it's done. --> + + 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. + Bug in AFP font metrics that performed integer arithmetic when it should have been double arithemetic. -- 2.39.5