You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventFormatter.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.events;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import java.util.MissingResourceException;
  22. import java.util.ResourceBundle;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.fop.util.XMLResourceBundle;
  28. import org.apache.fop.util.text.AdvancedMessageFormat;
  29. import org.apache.fop.util.text.AdvancedMessageFormat.Part;
  30. import org.apache.fop.util.text.AdvancedMessageFormat.PartFactory;
  31. /**
  32. * Converts events into human-readable, localized messages.
  33. */
  34. public final class EventFormatter {
  35. private static final Pattern INCLUDES_PATTERN = Pattern.compile("\\{\\{.+\\}\\}");
  36. private static Log log = LogFactory.getLog(EventFormatter.class);
  37. private EventFormatter() {
  38. //utility class
  39. }
  40. /**
  41. * Formats an event using the default locale.
  42. * @param event the event
  43. * @return the formatted message
  44. */
  45. public static String format(Event event) {
  46. ResourceBundle bundle = null;
  47. String groupID = event.getEventGroupID();
  48. if (groupID != null) {
  49. try {
  50. bundle = XMLResourceBundle.getXMLBundle(
  51. groupID,
  52. EventFormatter.class.getClassLoader());
  53. } catch (MissingResourceException mre) {
  54. throw new IllegalStateException("No XMLResourceBundle for " + groupID
  55. + " available.");
  56. }
  57. }
  58. return format(event, bundle);
  59. }
  60. /**
  61. * Formats an event using a given locale.
  62. * @param event the event
  63. * @param locale the locale
  64. * @return the formatted message
  65. */
  66. public static String format(Event event, Locale locale) {
  67. ResourceBundle bundle = null;
  68. String groupID = event.getEventGroupID();
  69. if (groupID != null) {
  70. try {
  71. bundle = XMLResourceBundle.getXMLBundle(
  72. groupID, locale,
  73. EventFormatter.class.getClassLoader());
  74. } catch (MissingResourceException mre) {
  75. if (log.isTraceEnabled()) {
  76. log.trace("No XMLResourceBundle for " + groupID + " available.");
  77. }
  78. }
  79. }
  80. if (bundle == null) {
  81. bundle = XMLResourceBundle.getXMLBundle(
  82. EventFormatter.class.getName(),
  83. locale,
  84. EventFormatter.class.getClassLoader());
  85. }
  86. return format(event, bundle);
  87. }
  88. private static String format(Event event, ResourceBundle bundle) {
  89. String template = bundle.getString(event.getEventID());
  90. return format(event, processIncludes(template, bundle));
  91. }
  92. private static String processIncludes(String template, ResourceBundle bundle) {
  93. CharSequence input = template;
  94. int replacements;
  95. StringBuffer sb;
  96. do {
  97. sb = new StringBuffer(Math.max(16, input.length()));
  98. replacements = processIncludesInner(input, sb, bundle);
  99. input = sb;
  100. } while (replacements > 0);
  101. String s = sb.toString();
  102. return s;
  103. }
  104. private static int processIncludesInner(CharSequence template, StringBuffer sb,
  105. ResourceBundle bundle) {
  106. int replacements = 0;
  107. Matcher m = INCLUDES_PATTERN.matcher(template);
  108. while (m.find()) {
  109. String include = m.group();
  110. include = include.substring(2, include.length() - 2);
  111. m.appendReplacement(sb, bundle.getString(include));
  112. replacements++;
  113. }
  114. m.appendTail(sb);
  115. return replacements;
  116. }
  117. /**
  118. * Formats the event using a given pattern. The pattern needs to be compatible with
  119. * {@link AdvancedMessageFormat}.
  120. * @param event the event
  121. * @param pattern the pattern (compatible with {@link AdvancedMessageFormat})
  122. * @return the formatted message
  123. */
  124. public static String format(Event event, String pattern) {
  125. AdvancedMessageFormat format = new AdvancedMessageFormat(pattern);
  126. Map params = new java.util.HashMap(event.getParams());
  127. params.put("source", event.getSource());
  128. params.put("severity", event.getSeverity());
  129. return format.format(params);
  130. }
  131. private static class LookupFieldPart implements Part {
  132. private String fieldName;
  133. public LookupFieldPart(String fieldName) {
  134. this.fieldName = fieldName;
  135. }
  136. public boolean isGenerated(Map params) {
  137. return getKey(params) != null;
  138. }
  139. public void write(StringBuffer sb, Map params) {
  140. // TODO there's no defaultBundle anymore
  141. // sb.append(defaultBundle.getString(getKey(params)));
  142. }
  143. private String getKey(Map params) {
  144. return (String)params.get(fieldName);
  145. }
  146. /** {@inheritDoc} */
  147. public String toString() {
  148. return "{" + this.fieldName + ", lookup}";
  149. }
  150. }
  151. /** PartFactory for lookups. */
  152. public static class LookupFieldPartFactory implements PartFactory {
  153. /** {@inheritDoc} */
  154. public Part newPart(String fieldName, String values) {
  155. return new LookupFieldPart(fieldName);
  156. }
  157. /** {@inheritDoc} */
  158. public String getFormat() {
  159. return "lookup";
  160. }
  161. }
  162. }