1 18 19 package org.apache.strutsel.taglib.utils; 20 21 import java.util.HashMap ; 22 import java.util.Locale ; 23 24 import org.apache.struts.util.MessageResources; 25 import org.apache.struts.util.MessageResourcesFactory; 26 27 28 33 public class HashMapMessageResources 34 extends MessageResources { 35 36 private HashMap messages = new HashMap (); 37 38 public HashMapMessageResources(MessageResourcesFactory factory, 39 String config) { 40 super(factory, config); 41 } 42 43 public HashMapMessageResources(MessageResourcesFactory factory, 44 String config, boolean returnNull) { 45 super(factory, config, returnNull); 46 } 47 48 public void addMessage(String key, String value) { 49 addMessage("", key, value); 50 } 51 52 public void addMessage(String locale, String key, String value) { 53 messages.put(locale + "." + key, value); 54 } 55 56 67 public String getMessage(Locale locale, String key) { 68 69 String localeKey = localeKey(locale); 71 String originalKey = messageKey(localeKey, key); 72 String messageKey = null; 73 String message = null; 74 int underscore = 0; 75 boolean addIt = false; 77 while (true) { 79 80 loadLocale(localeKey); 82 83 messageKey = messageKey(localeKey, key); 85 86 synchronized (messages) { 87 message = (String )messages.get(messageKey); 88 if (message != null) { 89 if (addIt) 90 messages.put(originalKey, message); 91 return (message); 92 } 93 } 94 95 addIt = true; 97 underscore = localeKey.lastIndexOf("_"); 98 99 if (underscore < 0) 100 101 break; 102 103 localeKey = localeKey.substring(0, underscore); 104 } 105 106 if (!defaultLocale.equals(locale)) { 108 localeKey = localeKey(defaultLocale); 109 messageKey = messageKey(localeKey, key); 110 loadLocale(localeKey); 111 112 synchronized (messages) { 113 message = (String )messages.get(messageKey); 114 if (message != null) { 115 if (addIt) 116 messages.put(originalKey, message); 117 return (message); 118 } 119 } 120 } 121 122 localeKey = ""; 124 messageKey = messageKey(localeKey, key); 125 loadLocale(localeKey); 126 127 synchronized (messages) { 128 message = (String )messages.get(messageKey); 129 130 if (message != null) { 131 if (addIt) 132 messages.put(originalKey, message); 133 return (message); 134 } 135 } 136 137 if (returnNull) 139 return (null); 140 else 141 return ("???" + messageKey(locale, key) + "???"); 142 } 143 144 protected void loadLocale(String localeKey) { 145 } 146 } 147 | Popular Tags |