Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / TranslationDatabase.java @ 40769

History | View | Annotate | Download (6.31 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.i18n.utils;
25

    
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileNotFoundException;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.Iterator;
34
import java.util.Set;
35

    
36
/**
37
 * @author cesar
38
 *
39
 */
40
public class TranslationDatabase {
41
        
42
        public TranslationDatabase(ConfigOptions config){
43
                this.config = config; 
44
        }
45
        
46
        private ConfigOptions config;
47
        private HashMap dictionaries;
48
        
49
        public void load() {
50
                // always start with an empty HashMap when loading
51
                dictionaries = new HashMap();
52
                String lang;
53
                DoubleProperties dictionary;
54
                
55
                FileInputStream stream=null;
56
                
57
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
58
                        lang = config.languages[currentLang];
59
                        dictionary = new DoubleProperties();
60
                        try {
61
                                stream = new FileInputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
62
                                try {
63
                                        dictionary.load(stream);
64
                                } catch (IOException e) {
65
                                        System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
66
                                }
67
                        } catch (FileNotFoundException e) {
68
                                System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
69
                        }
70
                        dictionaries.put(lang, dictionary);
71
                }
72
        }
73
        
74
        public void save() {
75
                String lang;
76
                DoubleProperties dictionary;
77
                
78
                FileOutputStream stream=null;
79

    
80
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
81
                        lang = config.languages[currentLang];
82
                        
83
                        dictionary = ((DoubleProperties)dictionaries.get(lang));
84
                        
85
                        try {
86
                                stream = new FileOutputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
87
                                try {
88
                                        dictionary.store(stream, "Translations for language: " + lang);
89
                                } catch (IOException e) {
90
                                        System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
91
                                }
92
                        } catch (FileNotFoundException e) {
93
                                System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
94
                        }
95
                }
96
        }
97
        
98
        public String getTranslation(String lang, String key) {
99
                if (lang==null || key==null) return null;
100
                
101
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
102
                if (dictionary==null) return null;
103
                return (String) dictionary.get(key);
104
        }
105
        
106
        public String setTranslation(String lang, String key, String translation) {
107
                if (lang==null || key==null) return null;
108

    
109
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
110
                if (dictionary==null) return null;
111
                String oldvalue = (String) dictionary.get(key);
112
                dictionary.put(key, translation);
113
                return oldvalue;
114
        }
115
        
116
        /**
117
         * Removes the key from the specified dictionary, and its associated translation.
118
         * It has no effect if the key was not present in the dictionary.
119
         * 
120
         * @param lang The language from which the key should be removed.
121
         * @param key  The key to be removed.
122
         * @return The translation associated with the key, or null if the
123
         * key was not present in the dictionary. It also returns null if any of the parameters is
124
         * null, or if there was no dictionary for the specified language.
125
         */
126
        public String removeTranslation(String lang, String key) {
127
                if (lang==null || key==null) return null;
128

    
129
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
130
                if (dictionary==null) return null;
131
                String oldvalue = (String) dictionary.get(key);
132
                dictionary.remove(key);
133
                return oldvalue;
134
        }
135
        
136
        /**
137
         * Removes the key and its associated translation from all the dictionaries.
138
         * The key will be deleted from the dictionaries in which it is present (if any).
139
         * 
140
         * @param key  The key to be removed.
141
         * @return True if the key was removed from any dictionary, or false if the key
142
         * was not present in any dictionary.
143
         * @throws NullPointerException if the key is null.
144
         */
145
        public boolean removeTranslation(String key) throws NullPointerException {
146
                DoubleProperties dictionary;
147
                String lang;
148
                boolean present=false;
149
                
150
                Set keys = dictionaries.keySet();
151
                Iterator langIterator = keys.iterator();
152
                while (langIterator.hasNext()) {
153
                        lang = (String) langIterator.next();
154
                        dictionary = (DoubleProperties) dictionaries.get(lang);
155
                        if (dictionary.containsKey(key)) {
156
                                present=true;
157
                                dictionary.remove(key);
158
                        }
159
                }
160
                return present;
161
        }
162
        
163
        public boolean containsLanguage(String lang) {
164
                return dictionaries.containsKey(lang);
165
        }
166

    
167
        public boolean containsKey(String lang, String key) {
168
                if (lang==null || key==null) return false;
169

    
170
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
171
                return dictionary.containsKey(key);
172
        }
173
        
174
        public String getAssociatedKey(String lang, String value) {
175
                if (lang==null || value==null) return null;
176

    
177
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
178
                return dictionary.getAssociatedKey(value);
179
        }
180
        
181
        public ArrayList getAssociatedKeys(String lang, String value) {
182
                if (lang==null || value==null) return null;
183

    
184
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
185
                return dictionary.getAssociatedKeys(value);
186
        }
187

    
188
        public boolean containsTranslation(String lang, String translation) {
189
                if (lang==null || translation==null) return false;
190

    
191
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
192
                return dictionary.containsValue(translation);
193
        }
194
}