KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsXmlLanguageFile


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsXmlLanguageFile.java,v $
3 * Date : $Date: 2006/03/28 16:48:21 $
4 * Version: $Revision: 1.12 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28 package com.opencms.workplace;
29
30 /**
31  * Provides access to the localized lables for the workplace.<p>
32  *
33  * This class used to read language files from the OpenCms VFS in a proprietary
34  * XML format, hence the name "XmlLanguageFile". Since 5.0rc2 this class has
35  * been changed to use the standard <code>java.util.ResouceBundle</code> technology.<p>
36  *
37  * @author Alexander Kandzior
38  * @version $Revision: 1.12 $ $Date: 2006/03/28 16:48:21 $
39  *
40  * @deprecated Will not be supported past the OpenCms 6 release.
41  */

42 import org.opencms.file.CmsObject;
43 import org.opencms.file.CmsPropertyDefinition;
44 import org.opencms.i18n.CmsMessageException;
45 import org.opencms.i18n.CmsMessages;
46 import org.opencms.i18n.CmsResourceBundleLoader;
47 import org.opencms.main.CmsException;
48 import org.opencms.main.CmsLog;
49 import org.opencms.main.OpenCms;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.HashSet JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Locale JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.MissingResourceException JavaDoc;
57 import java.util.ResourceBundle JavaDoc;
58 import java.util.Set JavaDoc;
59
60 public class CmsXmlLanguageFile {
61
62     /** The name of the property file */
63     public static final String JavaDoc C_BUNDLE_NAME = "com.opencms.workplace.workplace";
64     
65     /** The Locales Support Runtime Property Name */
66     public static final String JavaDoc C_COMPATIBILITY_OLDLOCALES = "compatibility.support.oldlocales";
67     
68     /** Localized message access object for the default workplace */
69     private CmsMessages m_messages;
70     
71     /** CmsObject provided with the constructror */
72     private CmsObject m_cms;
73     
74     /** Locale (2 letter ISO country code like "en") */
75     private String JavaDoc m_locale;
76
77     // static data storages to prevent multiple lookups
78
/** The workplace default encoding */
79     private static String JavaDoc m_workplaceDefaultEncoding;
80     
81     /** Map of locales from the installed modules */
82     private static Map JavaDoc m_allModuleMessages = null;
83     
84     /** Set of locales from the installed modules */
85     private static Set JavaDoc m_moduleMessages = null;
86     
87     /** Map of encodings from the installed languages */
88     private static Map JavaDoc m_allEncodings = null;
89                        
90     /** Flag to indicate support for old locale mechanism */
91     private static Boolean JavaDoc m_supportOldLocale = null;
92
93     /** DEBUG flag */
94     private static final int DEBUG = 0;
95                  
96     /**
97      * Constructor for creating a new language file
98      * initialized with the workplace preferences locale of the current user.<p>
99      *
100      * @param cms for accessing system resources
101      */

102     public CmsXmlLanguageFile(CmsObject cms) throws CmsException {
103         this(cms, getCurrentUserLanguage(cms));
104     }
105
106     /**
107      * Constructor for creating a new language file
108      * initialized with the provided locale.<p>
109      *
110      * @param cms for accessing system resources
111      * @param locale the locale to initialize
112      */

113     public CmsXmlLanguageFile(CmsObject cms, String JavaDoc locale) throws CmsException {
114         m_cms = cms;
115         m_locale = locale;
116         m_messages = new CmsMessages(C_BUNDLE_NAME, m_locale);
117         // initialize the static encodings map if required
118
if (m_allEncodings == null) {
119             if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): initializing the static encodings");
120             synchronized (this) {
121                 m_allEncodings = new HashMap JavaDoc();
122                 m_workplaceDefaultEncoding = OpenCms.getWorkplaceManager().getEncoding();
123             }
124         }
125         // initialize the static hash if not already done
126
if (m_allModuleMessages == null) {
127             if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): initializing module messages hash");
128             synchronized (this) {
129                 m_allModuleMessages = new HashMap JavaDoc();
130             }
131         }
132         // initialize the static module messages
133
Object JavaDoc obj = m_allModuleMessages.get(m_locale);
134         if (obj == null) {
135             if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): collecting static module messages");
136             synchronized (this) {
137                 m_moduleMessages = collectModuleMessages(m_cms, m_locale);
138                 m_allModuleMessages.put(m_locale, m_moduleMessages);
139             }
140         } else {
141             m_moduleMessages = (Set JavaDoc)obj;
142         }
143         if (m_supportOldLocale == null) {
144             if (DEBUG > 0) System.err.println("CmsXmlLanguageFile(): reading old locale support property");
145             synchronized (this) {
146                 // set compatiblity flag for old locales
147
Boolean JavaDoc flag = Boolean.valueOf((String JavaDoc)OpenCms.getRuntimeProperty(C_COMPATIBILITY_OLDLOCALES));
148                 m_supportOldLocale = (flag != null)?flag:new Boolean JavaDoc(false);
149                 if (CmsLog.INIT.isInfoEnabled()) {
150                     CmsLog.INIT.info(". Old locale support : " + (m_supportOldLocale.booleanValue() ? "enabled" : "disabled"));
151                 }
152             }
153         }
154     }
155     
156     /**
157      * Gathers all localization files for the workplace from the different modules.<p>
158      *
159      * For a module named "my.module.name" the locale file must be named
160      * "my.module.name.workplace" and be located in the classpath so that the resource loader
161      * can find it.<p>
162      *
163      * @param cms for accessing system resources
164      */

165     private synchronized Set JavaDoc collectModuleMessages(CmsObject cms, String JavaDoc locale) {
166         HashSet JavaDoc bundles = new HashSet JavaDoc();
167         Set JavaDoc names = OpenCms.getModuleManager().getModuleNames();
168         if (names != null) {
169             Iterator JavaDoc i = names.iterator();
170             while (i.hasNext()) {
171                 String JavaDoc bundleName = ((String JavaDoc)i.next()) + ".workplace";
172                 // this should result in a name like "my.module.name.workplace"
173
try {
174                     ResourceBundle JavaDoc bundle = CmsResourceBundleLoader.getBundle(bundleName, new Locale JavaDoc(locale));
175                     bundles.add(bundle);
176                 } catch (MissingResourceException JavaDoc e) {
177                     // can be ignored
178
}
179             }
180         }
181         return bundles;
182     }
183
184     /**
185      * Returns the content encoding defined for this language.<p>
186      *
187      * @return String the the content encoding defined for this language
188      */

189     public String JavaDoc getEncoding() {
190         // try to read from static map
191
String JavaDoc result = (String JavaDoc)m_allEncodings.get(m_locale);
192         if (result != null) return result;
193
194         // encoding not stored so far, let's try to figure it out
195
if (DEBUG > 0) System.err.println("CmsXmlLanguageFile.getEncoding(): looking up encoding for locale " + m_locale);
196         try {
197             result = m_messages.getString(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING);
198         } catch (CmsMessageException e) {
199             // exception - just use the default encoding
200
result = m_workplaceDefaultEncoding;
201         }
202         if (result.startsWith("{")) {
203             // this is a "supported set" - try to figure out the encoding to use
204
if (result.indexOf(m_workplaceDefaultEncoding) >= 0) {
205                 // the current default encoding is supported, so we use this
206
result = m_workplaceDefaultEncoding;
207             } else {
208                 // default encoding is not supported, so we use the first given encoding in the set
209
int index = result.indexOf(";");
210                 if (index <= 1) {
211                     result = m_workplaceDefaultEncoding;
212                 } else {
213                     result = result.substring(1, index);
214                 }
215             }
216         }
217         // now store the result in the static map
218
m_allEncodings.put(m_locale, result);
219         return result;
220     }
221
222     /**
223      * Returns the messages initialized for this language file.
224      *
225      * @return CmsMessages the messages initialized for this language file
226      */

227     public CmsMessages getMessages() {
228         return m_messages;
229     }
230
231     /**
232      * Returns the language value of the requested label key.<p>
233      *
234      * @param keyName the key for the desired string
235      * @return the resource string for the given key
236      */

237     public String JavaDoc getLanguageValue(String JavaDoc keyName) {
238         if (DEBUG > 2) System.err.println("CmsXmlLanguageFile.getLanguageValue(): looking key " + keyName);
239         try {
240             return m_messages.getString(keyName);
241         } catch (CmsMessageException e) {}
242
243         // key was not found in default workplace bundles
244
if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' not found in workplace messages");
245         Iterator JavaDoc i = m_moduleMessages.iterator();
246         while (i.hasNext()) {
247             try {
248                 return ((ResourceBundle JavaDoc) i.next()).getString(keyName);
249                 // if no exception is thrown here we have found the result
250
} catch (MissingResourceException JavaDoc e) {
251                 // ignore and continue looking in the other bundles
252
}
253         }
254         if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' also not found in module messages (this is not good)");
255         if (m_supportOldLocale.booleanValue()) {
256             // we have not found the key and we are in old compatiblity mode,
257
// so let's look up the XML locales
258
try {
259                 CmsXmlLanguageFileContent langFile = new CmsXmlLanguageFileContent(m_cms, m_locale);
260                 String JavaDoc value = langFile.getLanguageValue(keyName);
261                 if (value != null) return value;
262             } catch (CmsException e) {
263                 // we have not found the keyName in the XML files either
264
}
265         }
266         
267         if (keyName.startsWith("help.")) {
268             // online help might not have been installed or missing help key, return default page
269
return "index.html";
270         }
271         
272         // key was not found
273
if (DEBUG > 1) System.err.println("CmsXmlLanguageFile.getLanguageValue(): '" + keyName + "' not found at all (this is bad)");
274         if (CmsLog.getLog(this).isWarnEnabled()) {
275             CmsLog.getLog(this).warn("Missing value for locale key: " + keyName);
276         }
277         return "??? " + keyName + " ???";
278     }
279         
280     /**
281      * Returns the Workplace locale setting of the current user.<p>
282      *
283      * @param cms for accessing system resources
284      * @return the locale of the current user as a String (e.g. "en", "de", ...)
285      */

286     public static String JavaDoc getCurrentUserLanguage(CmsObject cms) {
287         return cms.getRequestContext().getLocale().toString();
288     }
289 }
290
Popular Tags