KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsXmlLanguageFileContent.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.3 $
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
29
30 package com.opencms.workplace;
31
32 import org.opencms.file.CmsFile;
33 import org.opencms.file.CmsFolder;
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.workplace.CmsWorkplace;
39
40 import com.opencms.template.A_CmsXmlContent;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * Provides backward compatibility with pre 5.0 XML-style localization.<p>
47  *
48  * The use of this class is <em>deprecated</em> and it is provided only to make old modules
49  * work without modifications. It is suggested that you modify you modules
50  * to use the 5.0 style ResourceBundle approach, because of performance issues.<p>
51  *
52  * Support for XML-style locales will be removed in a future release.<p>
53  *
54  * @author Alexander Lucas
55  * @version $Revision: 1.3 $ $Date: 2005/06/27 23:22:07 $
56  *
57  * @deprecated Will not be supported past the OpenCms 6 release.
58  */

59 public class CmsXmlLanguageFileContent extends A_CmsXmlContent {
60
61     /**
62      * Constructor for creating a new language file object containing the content
63      * of the corresponding system language file for the actual user.<p<
64      *
65      * The position of the language file will be looked up in workplace.ini.
66      * The selected language of the current user can be searched in the user object.
67      *
68      * @param cms CmsObject object for accessing system resources.
69      * @param locale name of the locale to initialize
70      */

71     public CmsXmlLanguageFileContent(CmsObject cms, String JavaDoc locale) throws CmsException {
72         super();
73         try {
74             mergeLanguageFiles(cms, locale);
75         }
76         catch(Exception JavaDoc e) {
77             throwException("Error while merging language files in folder " + CmsWorkplace.VFS_PATH_LOCALES + locale + "/.");
78         }
79     }
80
81     /**
82      * Gets a description of this content type.
83      * @return Content type description.
84      */

85     public String JavaDoc getContentDescription() {
86         return "Language definition file";
87     }
88
89     /**
90      * Gets the language value vor the requested tag.
91      * @param tag requested tag.
92      * @return Language value.
93      */

94     public String JavaDoc getLanguageValue(String JavaDoc tag) throws CmsException {
95         String JavaDoc result = null;
96         if(hasData(tag)) {
97             result = getDataValue(tag);
98         }
99         return result;
100     }
101
102     /**
103      * Gets the expected tagname for the XML documents of this content type
104      * @return Expected XML tagname.
105      */

106     public String JavaDoc getXmlDocumentTagName() {
107         return "LANGUAGE";
108     }
109
110     /**
111      * Checks if there exists a language value vor the requested tag.
112      * @param tag requested tag.
113      * @return Language value.
114      */

115     public boolean hasLanguageValue(String JavaDoc tag) {
116         return hasData(tag);
117     }
118
119     /**
120      * Merges all language files available for current language settings.
121      * Language files have to be stored in a folder like
122      * "system/workplace/config/language/[language shortname]/"
123      *
124      * @author Matthias Schreiber
125      * @param cms CmsObject object for accessing system resources.
126      * @param language Current language
127      */

128     private void mergeLanguageFiles(CmsObject cms, String JavaDoc language) throws CmsException {
129         List JavaDoc langFiles = (List JavaDoc) new ArrayList JavaDoc();
130
131         // Make sure old "uk" stuff still works
132
if ("uk".equals(language)) language = "en";
133         
134         try {
135             langFiles = cms.getFilesInFolder(CmsWorkplace.VFS_PATH_LOCALES + language + "/");
136         } catch (CmsException e) {
137             // noop
138
}
139
140         // get all modules-language Files
141
List JavaDoc modules = (List JavaDoc) new ArrayList JavaDoc();
142         modules = cms.getSubFolders(CmsWorkplace.VFS_PATH_MODULES);
143         String JavaDoc lang = CmsWorkplaceDefault.C_VFS_DIR_LOCALES + language + "/";
144         // make sure old modules language files still work
145
String JavaDoc oldLang = "language/" + language + "/";
146         for(int i = 0;i < modules.size();i++) {
147             List JavaDoc moduleLangFiles = (List JavaDoc) new ArrayList JavaDoc();
148             try {
149                 moduleLangFiles = cms.getFilesInFolder(cms.getSitePath((CmsFolder)modules.get(i)) + lang);
150             } catch (CmsException e) {
151                 // try read from old module locales path
152
try {
153                     moduleLangFiles = cms.getFilesInFolder(cms.getSitePath((CmsFolder)modules.get(i)) + oldLang);
154                     if(CmsLog.getLog(this).isWarnEnabled() ) {
155                         CmsLog.getLog(this).warn("Old module 'locales' path used: " + cms.getSitePath((CmsFolder)modules.get(i)) + oldLang);
156                     }
157                 } catch (CmsException ex) {
158                     // no language files found, we can live with that, probably the module just has none
159
}
160             }
161             for(int j = 0;j < moduleLangFiles.size();j++) {
162                 langFiles.add(moduleLangFiles.get(j));
163             }
164         }
165         CmsFile file = null;
166         for(int i = 0;i < langFiles.size();i++) {
167             file = (CmsFile)langFiles.get(i);
168             if(! file.getName().toLowerCase().endsWith(".txt") && file.getState() != CmsResource.STATE_DELETED) {
169                 try {
170                     init(cms, cms.getSitePath(file));
171                 } catch(Exception JavaDoc exc) {
172                     if(CmsLog.getLog(this).isErrorEnabled() ) {
173                         CmsLog.getLog(this).error("Error merging language file: " + cms.getSitePath(file), exc);
174                     }
175                 }
176             }
177         }
178     }
179 }
Popular Tags