1 15 package org.apache.hivemind.impl; 16 17 import java.text.MessageFormat ; 18 import java.util.Locale ; 19 20 import org.apache.hivemind.HiveMind; 21 import org.apache.hivemind.Messages; 22 import org.apache.hivemind.util.Defense; 23 24 31 public abstract class AbstractMessages implements Messages 32 { 33 public final String format(String key, Object [] args) 34 { 35 String pattern = getMessage(key); 36 37 for (int i = 0; i < args.length; i++) 38 { 39 Object arg = args[i]; 40 41 if (arg != null && arg instanceof Throwable ) 42 args[i] = extractMessage((Throwable ) arg); 43 } 44 45 51 MessageFormat messageFormat = new MessageFormat (""); 52 messageFormat.setLocale(getLocale()); 53 messageFormat.applyPattern(pattern); 54 55 return messageFormat.format(args); 56 } 57 58 private final String extractMessage(Throwable t) 59 { 60 String message = t.getMessage(); 61 62 return HiveMind.isNonBlank(message) ? message : t.getClass().getName(); 63 } 64 65 public final String format(String key, Object arg0) 66 { 67 return format(key, new Object [] 68 { arg0 }); 69 } 70 71 public final String format(String key, Object arg0, Object arg1) 72 { 73 return format(key, new Object [] 74 { arg0, arg1 }); 75 } 76 77 public final String format(String key, Object arg0, Object arg1, Object arg2) 78 { 79 return format(key, new Object [] 80 { arg0, arg1, arg2 }); 81 } 82 83 84 public final boolean containsMessage(String key) 85 { 86 return findMessage(key) != null; 87 } 88 89 public final String getMessage(String key) 90 { 91 Defense.notNull(key, "key"); 92 93 String result = findMessage(key); 94 95 if (result == null) 96 result = "[" + key.toUpperCase() + "]"; 97 98 return result; 99 } 100 101 104 105 protected abstract Locale getLocale(); 106 107 117 118 protected abstract String findMessage(String key); 119 120 } | Popular Tags |