KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > engine > JLanguageManager


1 /*
2     =========================================================================
3     Package engine - Implements the engine package.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JLanguageManager.java,v 1.15 2007/02/23 21:54:23 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.15 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.engine;
40
41 import org.planetamessenger.security.*;
42 import org.planetamessenger.io.*;
43 import org.planetamessenger.db.*;
44
45 import java.sql.*;
46 import java.sql.Connection JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.util.jar.*;
50 import java.util.*;
51 import java.io.*;
52
53
54 public class JLanguageManager {
55
56   public static final String JavaDoc DEFAULT_LANGUAGE = "english_us";
57   public static final String JavaDoc DEFAULT_LOCALE = "en";
58   private final String JavaDoc LANGUAGE_ATTRIBUTE = "Language";
59   private final String JavaDoc OWNER_ATTRIBUTE = "Owner";
60   private final String JavaDoc LICENSE_ATTRIBUTE = "License";
61   private final String JavaDoc HOMEPAGE_ATTRIBUTE = "HomePage";
62   private final String JavaDoc DESCRIPTION_ATTRIBUTE = "Description";
63   private final String JavaDoc VERSION_ATTRIBUTE = "Version";
64
65   private JJarClassLoader languageClassLoader = null;
66   private JDatabase database = null;
67   private String JavaDoc strLanguageId;
68
69
70   /**
71    * Constructor. Initialize all class data.
72    */

73   public JLanguageManager() {
74
75     languageClassLoader = new JJarClassLoader();
76     database = JSharedObjects.getDatabase();
77     
78     try {
79       loadDefaultLanguage();
80     } catch( Exception JavaDoc e ) {
81       try {
82         loadLanguage( DEFAULT_LANGUAGE );
83       } catch( Exception JavaDoc ex ) {
84         System.err.println( "JLanguageManager.JLanguageManager() - " + ex );
85       }
86     }
87   }
88
89   /**
90    * Destroy the object.
91    * Releases all allocated resources.
92    */

93   public void destroy() {
94     
95     try {
96       languageClassLoader.close();
97     } catch( java.util.jar.JarException JavaDoc e ) {
98       System.err.println( "JLanguageManager.destroy() - " + e );
99     }
100   }
101   
102   /**
103    * Load the default language assigned to
104    * the system.
105    */

106   public void loadDefaultLanguage() throws Exception JavaDoc {
107
108     String JavaDoc strLanguageId;
109     int nProfileId = JSharedObjects.getProfileManager().getDefaultProfile();
110     ResultSet JavaDoc resultSet = null;
111     Connection JavaDoc conn = null;
112     PreparedStatement JavaDoc st = null;
113     
114     
115     try{
116       conn = JSharedObjects.getDatabase().getConnection();
117       st = conn.prepareStatement( "SELECT language_id FROM profile WHERE profile_id = ?" );
118       st.setLong( 1, nProfileId );
119       
120       resultSet = st.executeQuery();
121   
122       if( resultSet == null )
123         throw new Exception JavaDoc( "JLanguageManager.loadDefaultLanguage() - Database error" );
124
125       resultSet.next();
126       strLanguageId = ( resultSet.getString( "language_id" ) == null ? "" : resultSet.getString( "language_id" ) );
127       resultSet.close();
128       loadLanguage( strLanguageId );
129     } catch( java.sql.SQLException JavaDoc e ) {
130       throw new Exception JavaDoc( "JLanguageManager.loadDefaultLanguage() - SQLException " + e );
131     } catch( Exception JavaDoc e ) {
132       throw new Exception JavaDoc( "JLanguageManager.loadDefaultLanguage() - Exception " + e );
133     } finally {
134       resultSet.close();
135       st.close();
136     }
137   }
138   
139   /**
140    * Load the language jar file.<br>
141    * @param strLanguageId The language to load;<br>
142    */

143   public void loadLanguage( String JavaDoc strLanguageId ) throws Exception JavaDoc {
144     
145     try {
146       languageClassLoader.open( JSharedObjects.getConfiguration().getLanguagesPath() + strLanguageId + ".jar" );
147       this.strLanguageId = strLanguageId;
148       JSharedObjects.getMainWindow().windowRefresh( org.planetamessenger.mos.forms.JMOSWindow.LANGUAGE_REFRESH );
149     } catch( JarException e ) {
150       System.err.println( "JLanguageManager.loadLanguage() - " + e );
151       strLanguageId = "";
152       throw new Exception JavaDoc( "JLanguageManager.loadLanguage() - Language not found" );
153     }
154   }
155
156   /**
157    * Return the current loaded language.
158    */

159   public String JavaDoc getCurrentLanguage() {
160     
161     return strLanguageId;
162   }
163
164   /**
165    * Return the languages list.
166    * This list is acquired from languages
167    * directory where PlanetaMessenger lives.
168    */

169   public ArrayList<JLanguageField> getLanguageList() {
170    
171     JarFile langJarFile;
172     JarEntry jarEntry;
173     JLanguageField item = null;
174     JFilenameFilter filter = new JFilenameFilter();
175     File langDir = new File( JSharedObjects.getConfiguration().getLanguagesPath() );
176     ArrayList<JLanguageField> langVector = new ArrayList<JLanguageField>();
177     String JavaDoc[] strDirList;
178     
179         
180     filter.add( "jar" );
181     filter.add( "zip" );
182       
183     strDirList = langDir.list( filter );
184
185     // Get all languages from PlanetaMessenger's languages dir
186
for( int nCount = 0; nCount < strDirList.length; nCount++ ) {
187       Manifest manifest;
188       String JavaDoc strLanguageName = strDirList[nCount].substring( 0, strDirList[nCount].indexOf( '.' ) );
189
190       try {
191         langJarFile = new JarFile( JSharedObjects.getConfiguration().getLanguagesPath() + strDirList[nCount] );
192         manifest = langJarFile.getManifest();
193         item = new JLanguageField();
194         
195         item.setLanguageId( strLanguageName );
196         item.setLanguage( manifest.getMainAttributes().getValue( LANGUAGE_ATTRIBUTE ) );
197         item.setOwner( manifest.getMainAttributes().getValue( OWNER_ATTRIBUTE ) );
198         item.setLicense( manifest.getMainAttributes().getValue( LICENSE_ATTRIBUTE ) );
199         item.setHomePage( manifest.getMainAttributes().getValue( HOMEPAGE_ATTRIBUTE ) );
200         item.setDescription( manifest.getMainAttributes().getValue( DESCRIPTION_ATTRIBUTE ) );
201         item.setVersion( manifest.getMainAttributes().getValue( VERSION_ATTRIBUTE ) );
202
203         langVector.add( item );
204         langJarFile.close();
205         langJarFile = null;
206       } catch( java.io.IOException JavaDoc e ) {
207         System.err.println( "JLanguageManager.getLanguagesList() - " + e );
208         continue;
209       }
210     }
211
212     langDir = null;
213     filter = null;
214
215     return langVector;
216   }
217   
218   /**
219    * Return the specified language field from a language list.
220    * @param strLanguageId The desired language to find;<br>
221    * @param vLanguageList The list to search the language;<br>
222    */

223   public JLanguageField getLanguage( String JavaDoc strLanguageId, ArrayList<JLanguageField> vLanguageList ) {
224    
225     JLanguageField item = null;
226     
227     for( int nCount = 0; nCount <= vLanguageList.size(); nCount++ ) {
228       item = vLanguageList.get( nCount );
229       
230       if( item.getLanguageId().compareTo( strLanguageId ) == 0 )
231         return item;
232     }
233
234     return null;
235   }
236   
237   /**
238    * Get the internationalizated string
239    * @param strKeyName The key name of string to get.
240    */

241   public String JavaDoc getString( String JavaDoc strKeyName ) throws Exception JavaDoc {
242
243     ResourceBundle resourceBundle = ResourceBundle.getBundle( strLanguageId, Locale.getDefault(), languageClassLoader );
244
245     if( resourceBundle == null )
246       throw new Exception JavaDoc( "JLanguageManager.getString() - Language not loaded" );
247     else {
248       String JavaDoc strValue = resourceBundle.getString( strKeyName );
249
250       return ( strValue == null ? "RESOURCE ID " + strKeyName + " NOT FOUND" : strValue );
251     }
252   }
253   
254   /**
255    * Get the internationalizated string but doesn't throw a Exception when<br>
256    * errors happens. If any error happens to retrieve the internationalizated<br>
257    * string, the strKeyName is returned.<br>
258    * @param strKeyName The key name of string to get.<br>
259    */

260   public String JavaDoc getStringEx( String JavaDoc strKeyName ) {
261
262     try {
263       return getString( strKeyName );
264     } catch( Exception JavaDoc e ) {
265       System.err.println( "JLanguageManager.getStringEx() - " + e );
266       return strKeyName;
267     }
268   }
269
270   /**
271    * Save a language information for a specified profile;<br>
272    * @param nProfileId The profile Id to save the information;<br>
273    * @param strLanguageId The languageId to update;
274    */

275   public boolean updateProfile( long nProfileId, String JavaDoc strLanguageId ) {
276     
277     Connection JavaDoc conn = null;
278     PreparedStatement JavaDoc st = null;
279
280     
281     if( nProfileId == -1 )
282       return false;
283     
284     try {
285       conn = JSharedObjects.getDatabase().getConnection();
286       st = conn.prepareStatement( "UPDATE profile SET language_id=? WHERE profile_id=?" );
287       st.setString( 1, strLanguageId );
288       st.setLong( 2, nProfileId );
289       
290       if( st.executeUpdate() <= 0 )
291         return false;
292       
293     } catch( SQLException e ) {
294       System.err.println( "JLanguageManager.updateProfile() - " + e );
295       return false;
296     } finally {
297       if( st != null )
298         try {
299           st.close();
300         } catch( SQLException e ) {
301           System.err.println( "JLanguageManager.updateProfile() - " + e );
302         }
303     }
304
305     return true;
306   }
307 }
308
309 // JLanguageManager class
310
Popular Tags