KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > bundle > DefaultBundleManager


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35
36 /**
37  * Default implementation for bundle manager
38  *
39  * @author Stephan J. Schmidt
40  * @version $id$
41  * @since 1.0
42  */

43 public class DefaultBundleManager implements BundleManager {
44   private List JavaDoc<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 JavaDoc<ResourceBundleFactory>();
54     factories.add(new XmlResourceBundleFactory());
55     factories.add(new BaseResourceBundleFactory());
56   }
57
58   /**
59    * Construct bundle manager with cache
60    *
61    * @param cache context cache to use
62    */

63   public DefaultBundleManager(ContextCache cache) {
64     this();
65     contextCache = cache;
66   }
67
68   /**
69    * Add bundle to the bundle manager and keep
70    * storage data structures valid
71    *
72    * @param context context to be added
73    * @param bundleName bundle name associated with the context
74    */

75   private void addBundle(Context context, String JavaDoc bundleName) {
76     bundles.put(context, bundleName);
77   }
78
79   /**
80    * Set bundle for full scope
81    *
82    * @param bundleName bundle to set
83    */

84   public void setBundle(String JavaDoc bundleName) {
85     addBundle(Contexts.create(), bundleName);
86   }
87
88   /**
89    * Set bundle with for package pattern
90    *
91    * @param bundleName bundle to set
92    * @param packageName package scope of the bundle
93    */

94   public void setBundle(String JavaDoc bundleName, String JavaDoc packageName) {
95     addBundle(Contexts.createFromString(packageName), bundleName);
96   }
97
98   /**
99    * Set bundle for context
100    *
101    * @param bundleName bundle to set
102    * @param context context with the scope of the bundle
103    */

104   public void setBundle(String JavaDoc bundleName, Context context) {
105     addBundle(context, bundleName);
106   }
107
108   /**
109    * Return full scope bundle
110    *
111    * @param locale locale for which the bundle should be returned
112    * @return corresponding bundle
113    */

114   public ResourceBundle JavaDoc getBundle(Locale JavaDoc locale) {
115     return getBundle(Contexts.create(), locale);
116   }
117
118   /**
119    * Return bundle for given context and locale. Searches all
120    * bundleNames by context from the most specific context to the
121    * less specific context.
122    *
123    * @param contextObject object which defines the scope
124    * @param locale locale for which the bundle should be created
125    * @return matching bundle
126    */

127   public ResourceBundle JavaDoc getBundle(Object JavaDoc contextObject, Locale JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc name, Locale JavaDoc locale) {
176     BaseBundle first = null;
177     BaseBundle child = null;
178     BaseBundle parent = null;
179     for (ResourceBundleFactory factory : factories) {
180       List JavaDoc<String JavaDoc> names = factory.expand(name, locale);
181       for (String JavaDoc canditateName : names) {
182         InputStream JavaDoc input = getInputStream(canditateName);
183         if (null != input) {
184           try {
185             parent = factory.loadBundle(input, locale);
186           } catch (IOException JavaDoc 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 JavaDoc getInputStream(String JavaDoc canditateName) {
205     InputStream JavaDoc 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