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 5868 cesar
/**
2
 *
3
 */
4
package org.gvsig.i18n.utils;
5
6
import java.io.File;
7 6061 cesar
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11 6141 cesar
import java.util.ArrayList;
12 5868 cesar
import java.util.HashMap;
13 6067 cesar
import java.util.Iterator;
14
import java.util.Set;
15 5868 cesar
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 6067 cesar
        private HashMap dictionaries;
28 5868 cesar
29
        public void load() {
30 6067 cesar
                // always start with an empty HashMap when loading
31
                dictionaries = new HashMap();
32 5868 cesar
                String lang;
33 6141 cesar
                DoubleProperties dictionary;
34 6067 cesar
35 6061 cesar
                FileInputStream stream=null;
36
37
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
38
                        lang = config.languages[currentLang];
39 6141 cesar
                        dictionary = new DoubleProperties();
40 6129 cesar
                        try {
41
                                stream = new FileInputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
42 6061 cesar
                                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 6141 cesar
                DoubleProperties dictionary;
57 6061 cesar
58
                FileOutputStream stream=null;
59
60
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
61
                        lang = config.languages[currentLang];
62
63 6141 cesar
                        dictionary = ((DoubleProperties)dictionaries.get(lang));
64 6061 cesar
65 6129 cesar
                        try {
66
                                stream = new FileOutputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
67 6061 cesar
                                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 6141 cesar
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
82 6061 cesar
                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 6141 cesar
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
90 6061 cesar
                if (dictionary==null) return null;
91
                String oldvalue = (String) dictionary.get(key);
92
                dictionary.put(key, translation);
93
                return oldvalue;
94
        }
95
96 6067 cesar
        /**
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 6141 cesar
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
110 6067 cesar
                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 6141 cesar
                DoubleProperties dictionary;
127 6067 cesar
                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 6141 cesar
                        dictionary = (DoubleProperties) dictionaries.get(lang);
135 6067 cesar
                        if (dictionary.containsKey(key)) {
136
                                present=true;
137
                                dictionary.remove(key);
138
                        }
139
                }
140
                return present;
141
        }
142
143 6061 cesar
        public boolean containsLanguage(String lang) {
144
                return dictionaries.containsKey(lang);
145
        }
146 6067 cesar
147 6061 cesar
        public boolean containsKey(String lang, String key) {
148
                if (lang==null || key==null) return false;
149
150 6141 cesar
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
151 6061 cesar
                return dictionary.containsKey(key);
152
        }
153 6141 cesar
154
        public String getAssociatedKey(String lang, String value) {
155
                if (lang==null || value==null) return null;
156 6067 cesar
157 6141 cesar
                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 6061 cesar
        public boolean containsTranslation(String lang, String translation) {
169
                if (lang==null || translation==null) return false;
170
171 6141 cesar
                DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
172 6061 cesar
                return dictionary.containsValue(translation);
173
        }
174 5868 cesar
}