1 31 32 package org.opencms.i18n; 33 34 import org.opencms.main.CmsIllegalArgumentException; 35 import org.opencms.main.CmsLog; 36 37 import java.util.ArrayList ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Locale ; 42 import java.util.Map ; 43 44 import org.apache.commons.logging.Log; 45 46 60 public class CmsMultiMessages extends CmsMessages { 61 62 63 public static final String MULTI_BUNDLE_NAME = CmsMultiMessages.class.getName(); 64 65 66 public static final String NULL_STRING = "null"; 67 68 69 private static final Log LOG = CmsLog.getLog(CmsMultiMessages.class); 70 71 72 private Map m_messageCache; 73 74 75 private List m_messages; 76 77 82 public CmsMultiMessages(Locale locale) { 83 84 super(); 85 setBundleName(CmsMultiMessages.MULTI_BUNDLE_NAME); 87 setLocale(locale); 88 m_messages = new ArrayList (); 90 m_messageCache = new Hashtable (); 92 } 93 94 101 public void addBundle(I_CmsMessageBundle bundle) { 102 103 addMessages(bundle.getBundle(getLocale())); 105 } 106 107 118 public void addMessages(CmsMessages messages) throws CmsIllegalArgumentException { 119 120 Locale locale = messages.getLocale(); 121 if (!getLocale().equals(locale)) { 122 if (!(messages instanceof CmsMultiMessages)) { 124 String bundleName = messages.getBundleName(); 126 messages = new CmsMessages(bundleName, getLocale()); 127 } else { 128 throw new CmsIllegalArgumentException(Messages.get().container( 130 Messages.ERR_MULTIMSG_LOCALE_DOES_NOT_MATCH_2, 131 messages.getLocale(), 132 getLocale())); 133 } 134 } 135 if (!m_messages.contains(messages)) { 136 if ((m_messageCache != null) && (m_messageCache.size() > 0)) { 137 m_messageCache = new Hashtable (); 139 } 140 m_messages.add(messages); 141 } 142 } 143 144 149 public void addMessages(List messages) { 150 151 if (messages == null) { 152 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_MULTIMSG_EMPTY_LIST_0)); 153 } 154 155 Iterator i = messages.iterator(); 156 while (i.hasNext()) { 157 addMessages((CmsMessages)i.next()); 158 } 159 } 160 161 166 public List getMessages() { 167 168 return m_messages; 169 } 170 171 174 public String getString(String keyName) { 175 176 return resolveKey(keyName); 177 } 178 179 182 public boolean isInitialized() { 183 184 return (m_messages != null) && !m_messages.isEmpty(); 185 } 186 187 190 public String key(String keyName, boolean allowNull) { 191 192 String result = resolveKey(keyName); 194 if ((result == null) && !allowNull) { 195 result = formatUnknownKey(keyName); 196 } 197 return result; 198 } 199 200 209 private String resolveKey(String keyName) { 210 211 if (LOG.isDebugEnabled()) { 212 LOG.debug(Messages.get().getBundle().key(Messages.LOG_RESOLVE_MESSAGE_KEY_1, keyName)); 213 } 214 215 String result = (String )m_messageCache.get(keyName); 216 if (result == NULL_STRING) { 217 return null; 219 } 220 if (result == null) { 221 for (int i = 0; (result == null) && (i < m_messages.size()); i++) { 223 try { 224 result = ((CmsMessages)m_messages.get(i)).getString(keyName); 225 } catch (CmsMessageException e) { 227 if (LOG.isDebugEnabled()) { 229 LOG.debug(e.getMessage(), e); 230 } 231 } 232 } 233 } else { 234 if (LOG.isDebugEnabled()) { 236 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_FOUND_CACHED_2, keyName, result)); 237 } 238 return result; 239 } 240 if (result == null) { 241 if (LOG.isDebugEnabled()) { 243 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_NOT_FOUND_1, keyName)); 244 } 245 m_messageCache.put(keyName, NULL_STRING); 247 } else { 248 if (LOG.isDebugEnabled()) { 250 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_FOUND_2, keyName, result)); 251 } 252 m_messageCache.put(keyName, result); 254 } 255 return result; 257 } 258 } | Popular Tags |