Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_905 / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / TranslationDatabase.java @ 10767

History | View | Annotate | Download (5.38 KB)

1
/**
2
 * 
3
 */
4
package org.gvsig.i18n.utils;
5

    
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.Iterator;
14
import java.util.Set;
15

    
16
/**
17
 * @author cesar
18
 *
19
 */
20
public class TranslationDatabase {
21
        
22
        public TranslationDatabase(ConfigOptions config){
23
                this.config = config; 
24
        }
25
        
26
        private ConfigOptions config;
27
        private HashMap dictionaries;
28
        
29
        public void load() {
30
                // always start with an empty HashMap when loading
31
                dictionaries = new HashMap();
32
                String lang;
33
                DoubleProperties dictionary;
34
                
35
                FileInputStream stream=null;
36
                
37
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
38
                        lang = config.languages[currentLang];
39
                        dictionary = new DoubleProperties();
40
                        try {
41
                                stream = new FileInputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
42
                                try {
43
                                        dictionary.load(stream);
44
                                } catch (IOException e) {
45
                                        System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
46
                                }
47
                        } catch (FileNotFoundException e) {
48
                                System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
49
                        }
50
                        dictionaries.put(lang, dictionary);
51
                }
52
        }
53
        
54
        public void save() {
55
                String lang;
56
                DoubleProperties dictionary;
57
                
58
                FileOutputStream stream=null;
59

    
60
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
61
                        lang = config.languages[currentLang];
62
                        
63
                        dictionary = ((DoubleProperties)dictionaries.get(lang));
64
                        
65
                        try {
66
                                stream = new FileOutputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
67
                                try {
68
                                        dictionary.store(stream, "Translations for language: " + lang);
69
                                } catch (IOException e) {
70
                                        System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
71
                                }
72
                        } catch (FileNotFoundException e) {
73
                                System.err.println("Error guardando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
74
                        }
75
                }
76
        }
77
        
78
        public String getTranslation(String lang, String key) {
79
                if (lang==null || key==null) return null;
80
                
81
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
82
                if (dictionary==null) return null;
83
                return (String) dictionary.get(key);
84
        }
85
        
86
        public String setTranslation(String lang, String key, String translation) {
87
                if (lang==null || key==null) return null;
88

    
89
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
90
                if (dictionary==null) return null;
91
                String oldvalue = (String) dictionary.get(key);
92
                dictionary.put(key, translation);
93
                return oldvalue;
94
        }
95
        
96
        /**
97
         * Removes the key from the specified dictionary, and its associated translation.
98
         * It has no effect if the key was not present in the dictionary.
99
         * 
100
         * @param lang The language from which the key should be removed.
101
         * @param key  The key to be removed.
102
         * @return The translation associated with the key, or null if the
103
         * key was not present in the dictionary. It also returns null if any of the parameters is
104
         * null, or if there was no dictionary for the specified language.
105
         */
106
        public String removeTranslation(String lang, String key) {
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.remove(key);
113
                return oldvalue;
114
        }
115
        
116
        /**
117
         * Removes the key and its associated translation from all the dictionaries.
118
         * The key will be deleted from the dictionaries in which it is present (if any).
119
         * 
120
         * @param key  The key to be removed.
121
         * @return True if the key was removed from any dictionary, or false if the key
122
         * was not present in any dictionary.
123
         * @throws NullPointerException if the key is null.
124
         */
125
        public boolean removeTranslation(String key) throws NullPointerException {
126
                DoubleProperties dictionary;
127
                String lang;
128
                boolean present=false;
129
                
130
                Set keys = dictionaries.keySet();
131
                Iterator langIterator = keys.iterator();
132
                while (langIterator.hasNext()) {
133
                        lang = (String) langIterator.next();
134
                        dictionary = (DoubleProperties) dictionaries.get(lang);
135
                        if (dictionary.containsKey(key)) {
136
                                present=true;
137
                                dictionary.remove(key);
138
                        }
139
                }
140
                return present;
141
        }
142
        
143
        public boolean containsLanguage(String lang) {
144
                return dictionaries.containsKey(lang);
145
        }
146

    
147
        public boolean containsKey(String lang, String key) {
148
                if (lang==null || key==null) return false;
149

    
150
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
151
                return dictionary.containsKey(key);
152
        }
153
        
154
        public String getAssociatedKey(String lang, String value) {
155
                if (lang==null || value==null) return null;
156

    
157
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
158
                return dictionary.getAssociatedKey(value);
159
        }
160
        
161
        public ArrayList getAssociatedKeys(String lang, String value) {
162
                if (lang==null || value==null) return null;
163

    
164
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
165
                return dictionary.getAssociatedKeys(value);
166
        }
167

    
168
        public boolean containsTranslation(String lang, String translation) {
169
                if (lang==null || translation==null) return false;
170

    
171
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
172
                return dictionary.containsValue(translation);
173
        }
174
}