1 16 package cintoo.messages.bundle; 17 18 import api.cintoo.messages.bundle.BaseBundle; 19 import api.cintoo.messages.bundle.BundleManager; 20 import api.cintoo.messages.bundle.ResourceBundleFactory; 21 import api.cintoo.messages.context.Context; 22 import api.cintoo.messages.context.ContextCache; 23 import api.cintoo.messages.context.ContextMap; 24 import api.cintoo.messages.context.Contexts; 25 import cintoo.messages.bundle.xml.XmlResourceBundleFactory; 26 import cintoo.messages.context.DefaultContextMap; 27 import cintoo.messages.context.DefaultContextCache; 28 29 import java.io.InputStream ; 30 import java.io.IOException ; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 import java.util.Locale ; 34 import java.util.ResourceBundle ; 35 36 43 public class DefaultBundleManager implements BundleManager { 44 private List <ResourceBundleFactory> factories; 45 46 private ContextMap bundles; 47 private ContextCache contextCache; 48 49 public DefaultBundleManager() { 50 bundles = new DefaultContextMap(); 51 contextCache = new DefaultContextCache(); 52 53 factories = new ArrayList <ResourceBundleFactory>(); 54 factories.add(new XmlResourceBundleFactory()); 55 factories.add(new BaseResourceBundleFactory()); 56 } 57 58 63 public DefaultBundleManager(ContextCache cache) { 64 this(); 65 contextCache = cache; 66 } 67 68 75 private void addBundle(Context context, String bundleName) { 76 bundles.put(context, bundleName); 77 } 78 79 84 public void setBundle(String bundleName) { 85 addBundle(Contexts.create(), bundleName); 86 } 87 88 94 public void setBundle(String bundleName, String packageName) { 95 addBundle(Contexts.createFromString(packageName), bundleName); 96 } 97 98 104 public void setBundle(String bundleName, Context context) { 105 addBundle(context, bundleName); 106 } 107 108 114 public ResourceBundle getBundle(Locale locale) { 115 return getBundle(Contexts.create(), locale); 116 } 117 118 127 public ResourceBundle getBundle(Object contextObject, Locale locale) { 128 Context context; 129 if (null != contextObject && contextObject instanceof Context) { 130 context = (Context) contextObject; 131 } else { 132 context = Contexts.create(contextObject); 133 } 134 135 BaseBundle bundle = null; 136 if (contextCache.isCached(context, locale)) { 137 bundle = contextCache.getFromCache(context, locale); 138 } else { 139 Context bundleContext = bundles.findMatchingContext(context); 140 if (null != bundleContext) { 141 String name = bundles.get(bundleContext); 142 bundle = findBundle(name, locale); 143 contextCache.addToCache(context, locale, bundle); 144 wireParents(context, bundle, locale); 145 } 146 } 147 return bundle; 148 } 149 150 private void wireParents(Context context, BaseBundle bundle, Locale locale) { 151 BaseBundle last = bundle; 152 while (last != null && null != last.getParent()) { 153 last = (BaseBundle) last.getParent(); 154 } 155 156 Context parentContext = bundles.findParentContext(context); 157 if (null != parentContext) { 158 BaseBundle parentBundle = (BaseBundle) getBundle(parentContext, locale); 159 if (null != parentBundle && last != parentBundle) { 160 last.setParent(parentBundle); 161 } 162 } 163 164 Context childContext = bundles.findChildContext(context); 165 if (null != childContext) { 166 BaseBundle childBundle = (BaseBundle) getBundle(childContext, locale); 167 if (null != childBundle) { 168 BaseBundle parentBundle = (BaseBundle) childBundle.getParent(); 169 childBundle.setParent(bundle); 170 last.setParent(parentBundle); 171 } 172 } 173 } 174 175 private BaseBundle findBundle(String name, Locale locale) { 176 BaseBundle first = null; 177 BaseBundle child = null; 178 BaseBundle parent = null; 179 for (ResourceBundleFactory factory : factories) { 180 List <String > names = factory.expand(name, locale); 181 for (String canditateName : names) { 182 InputStream input = getInputStream(canditateName); 183 if (null != input) { 184 try { 185 parent = factory.loadBundle(input, locale); 186 } catch (IOException e) { 187 e.printStackTrace(); 188 } 189 if (null != parent) { 190 if (null == first) { 191 first = parent; 192 } 193 if (null != child) { 194 child.setParent(parent); 195 } 196 child = parent; 197 } 198 } 199 } 200 } 201 return first; 202 } 203 204 private InputStream getInputStream(String canditateName) { 205 InputStream input = this.getClass().getResourceAsStream("/" + canditateName); 206 return input; 207 } 208 209 public void setContextCache(ContextCache contextCache) { 210 this.contextCache = contextCache; 211 } 212 213 public void clear() { 214 if (null != contextCache) { 215 contextCache.clear(); 216 } 217 if (null != bundles) { 218 bundles.clear(); 219 } 220 } 221 } 222 223 | Popular Tags |