KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > i18n > DefaultMessagesImpl


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.i18n;
14
15 import info.magnolia.cms.util.ClasspathResourcesUtil;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.PropertyResourceBundle JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 import org.apache.commons.collections.IteratorUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.io.IOUtils;
28
29
30 /**
31  * @author Philipp Bracher
32  * @version $Revision: 6679 $ ($Author: philipp $)
33  */

34 public class DefaultMessagesImpl extends AbstractMessagesImpl {
35
36     /**
37      * @param basename
38      * @param locale
39      */

40     protected DefaultMessagesImpl(String JavaDoc basename, Locale JavaDoc locale) {
41         super(basename, locale);
42     }
43
44     /**
45      * Get the message from the bundle
46      * @param key the key
47      * @return message
48      */

49     public String JavaDoc get(String JavaDoc key) {
50         if(key == null){
51             return "??????";
52         }
53         try {
54             return getBundle().getString(key);
55         }
56         catch (MissingResourceException JavaDoc e) {
57             return "???" + key + "???"; //$NON-NLS-1$ //$NON-NLS-2$
58
}
59     }
60
61     /**
62      * @return Returns the bundle for the current basename
63      */

64     protected ResourceBundle JavaDoc getBundle() {
65         if (bundle == null) {
66             InputStream JavaDoc stream = null;
67             try {
68                 stream = ClasspathResourcesUtil.getStream("/"
69                     + StringUtils.replace(basename, ".", "/")
70                     + "_"
71                     + getLocale().getLanguage()
72                     + "_"
73                     + getLocale().getCountry()
74                     + ".properties", false);
75                 if (stream == null) {
76                     stream = ClasspathResourcesUtil.getStream("/"
77                         + StringUtils.replace(basename, ".", "/")
78                         + "_"
79                         + getLocale().getLanguage()
80                         + ".properties", false);
81                 }
82                 if (stream == null) {
83                     stream = ClasspathResourcesUtil.getStream("/"
84                         + StringUtils.replace(basename, ".", "/")
85                         + "_"
86                         + MessagesManager.getDefaultLocale().getLanguage()
87                         + ".properties", false);
88                 }
89                 if (stream == null) {
90                     stream = ClasspathResourcesUtil.getStream("/"
91                         + StringUtils.replace(basename, ".", "/")
92                         + ".properties", false);
93                 }
94
95                 if (stream != null) {
96                     bundle = new PropertyResourceBundle JavaDoc(stream);
97                 }
98                 else {
99                     bundle = ResourceBundle.getBundle(getBasename(), getLocale());
100                 }
101             }
102             catch (IOException JavaDoc e) {
103                 log.error("can't load messages for " + basename);
104             } finally {
105                 IOUtils.closeQuietly(stream);
106             }
107         }
108         return bundle;
109     }
110
111     public void reload() throws Exception JavaDoc {
112         this.bundle = null;
113     }
114
115     /**
116      * Iterate over the keys
117      */

118     public Iterator JavaDoc keys() {
119         return IteratorUtils.asIterator(this.getBundle().getKeys());
120     }
121
122 }
123
Popular Tags