Revision 6141
trunk/libraries/libInternationalization/src-utils/org/gvsig/i18n/utils/TranslationDatabase.java | ||
---|---|---|
8 | 8 |
import java.io.FileNotFoundException; |
9 | 9 |
import java.io.FileOutputStream; |
10 | 10 |
import java.io.IOException; |
11 |
import java.util.ArrayList; |
|
11 | 12 |
import java.util.HashMap; |
12 | 13 |
import java.util.Iterator; |
13 |
import java.util.Properties; |
|
14 | 14 |
import java.util.Set; |
15 | 15 |
|
16 | 16 |
/** |
... | ... | |
30 | 30 |
// always start with an empty HashMap when loading |
31 | 31 |
dictionaries = new HashMap(); |
32 | 32 |
String lang; |
33 |
Properties dictionary; |
|
33 |
DoubleProperties dictionary;
|
|
34 | 34 |
|
35 | 35 |
FileInputStream stream=null; |
36 | 36 |
|
37 | 37 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
38 | 38 |
lang = config.languages[currentLang]; |
39 |
dictionary = new Properties(); |
|
39 |
dictionary = new DoubleProperties();
|
|
40 | 40 |
try { |
41 | 41 |
stream = new FileInputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
42 | 42 |
try { |
... | ... | |
53 | 53 |
|
54 | 54 |
public void save() { |
55 | 55 |
String lang; |
56 |
Properties dictionary; |
|
56 |
DoubleProperties dictionary;
|
|
57 | 57 |
|
58 | 58 |
FileOutputStream stream=null; |
59 | 59 |
|
60 | 60 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
61 | 61 |
lang = config.languages[currentLang]; |
62 | 62 |
|
63 |
dictionary = ((Properties)dictionaries.get(lang)); |
|
63 |
dictionary = ((DoubleProperties)dictionaries.get(lang));
|
|
64 | 64 |
|
65 | 65 |
try { |
66 | 66 |
stream = new FileOutputStream(config.databaseDir+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
... | ... | |
78 | 78 |
public String getTranslation(String lang, String key) { |
79 | 79 |
if (lang==null || key==null) return null; |
80 | 80 |
|
81 |
Properties dictionary = (Properties) dictionaries.get(lang);
|
|
81 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
82 | 82 |
if (dictionary==null) return null; |
83 | 83 |
return (String) dictionary.get(key); |
84 | 84 |
} |
... | ... | |
86 | 86 |
public String setTranslation(String lang, String key, String translation) { |
87 | 87 |
if (lang==null || key==null) return null; |
88 | 88 |
|
89 |
Properties dictionary = (Properties) dictionaries.get(lang);
|
|
89 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
90 | 90 |
if (dictionary==null) return null; |
91 | 91 |
String oldvalue = (String) dictionary.get(key); |
92 | 92 |
dictionary.put(key, translation); |
... | ... | |
106 | 106 |
public String removeTranslation(String lang, String key) { |
107 | 107 |
if (lang==null || key==null) return null; |
108 | 108 |
|
109 |
Properties dictionary = (Properties) dictionaries.get(lang);
|
|
109 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
110 | 110 |
if (dictionary==null) return null; |
111 | 111 |
String oldvalue = (String) dictionary.get(key); |
112 | 112 |
dictionary.remove(key); |
... | ... | |
123 | 123 |
* @throws NullPointerException if the key is null. |
124 | 124 |
*/ |
125 | 125 |
public boolean removeTranslation(String key) throws NullPointerException { |
126 |
Properties dictionary; |
|
126 |
DoubleProperties dictionary;
|
|
127 | 127 |
String lang; |
128 | 128 |
boolean present=false; |
129 | 129 |
|
... | ... | |
131 | 131 |
Iterator langIterator = keys.iterator(); |
132 | 132 |
while (langIterator.hasNext()) { |
133 | 133 |
lang = (String) langIterator.next(); |
134 |
dictionary = (Properties) dictionaries.get(lang); |
|
134 |
dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
135 | 135 |
if (dictionary.containsKey(key)) { |
136 | 136 |
present=true; |
137 | 137 |
dictionary.remove(key); |
... | ... | |
147 | 147 |
public boolean containsKey(String lang, String key) { |
148 | 148 |
if (lang==null || key==null) return false; |
149 | 149 |
|
150 |
Properties dictionary = (Properties) dictionaries.get(lang);
|
|
150 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
151 | 151 |
return dictionary.containsKey(key); |
152 | 152 |
} |
153 |
|
|
154 |
public String getAssociatedKey(String lang, String value) { |
|
155 |
if (lang==null || value==null) return null; |
|
153 | 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 |
|
|
154 | 168 |
public boolean containsTranslation(String lang, String translation) { |
155 | 169 |
if (lang==null || translation==null) return false; |
156 | 170 |
|
157 |
Properties dictionary = (Properties) dictionaries.get(lang);
|
|
171 |
DoubleProperties dictionary = (DoubleProperties) dictionaries.get(lang);
|
|
158 | 172 |
return dictionary.containsValue(translation); |
159 | 173 |
} |
160 | 174 |
} |
trunk/libraries/libInternationalization/src-utils/org/gvsig/i18n/utils/DoubleProperties.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package org.gvsig.i18n.utils; |
|
5 |
|
|
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.Enumeration; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.Properties; |
|
12 |
|
|
13 |
/** |
|
14 |
* The DoubleProperties class represents a set of properties. It provides the |
|
15 |
* same functionality as its parent class, Properties. Besides that, it also |
|
16 |
* provides an efficient method to get the key associated with a value. |
|
17 |
* |
|
18 |
* @author cesar |
|
19 |
* |
|
20 |
*/ |
|
21 |
public class DoubleProperties extends Properties { |
|
22 |
/** |
|
23 |
* |
|
24 |
*/ |
|
25 |
private static final long serialVersionUID = -1738114064256800193L; |
|
26 |
HashMap reverseMap = new HashMap(); |
|
27 |
|
|
28 |
public DoubleProperties() { |
|
29 |
super(); |
|
30 |
} |
|
31 |
|
|
32 |
|
|
33 |
public DoubleProperties(Properties defaults) { |
|
34 |
super(defaults); |
|
35 |
Enumeration keysEnum = this.keys(); |
|
36 |
ArrayList keySet; |
|
37 |
|
|
38 |
String key, value; |
|
39 |
while (keysEnum.hasMoreElements()) { |
|
40 |
key = (String) keysEnum.nextElement(); |
|
41 |
value = this.getProperty(key); |
|
42 |
if (reverseMap.containsKey(value)) { |
|
43 |
keySet = (ArrayList) reverseMap.get(value); |
|
44 |
keySet.add(key); |
|
45 |
} |
|
46 |
else { |
|
47 |
keySet = new ArrayList(); |
|
48 |
keySet.add(key); |
|
49 |
reverseMap.put(value, keySet); |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
|
|
55 |
public void load(InputStream stream) throws IOException { |
|
56 |
try { |
|
57 |
super.load(stream); |
|
58 |
} catch (IOException e) { |
|
59 |
// TODO Auto-generated catch block |
|
60 |
e.printStackTrace(); |
|
61 |
} |
|
62 |
Enumeration keysEnum = this.keys(); |
|
63 |
ArrayList keySet; |
|
64 |
|
|
65 |
String key, value; |
|
66 |
while (keysEnum.hasMoreElements()) { |
|
67 |
key = (String) keysEnum.nextElement(); |
|
68 |
value = this.getProperty(key); |
|
69 |
if (reverseMap.containsKey(value)) { |
|
70 |
keySet = (ArrayList) reverseMap.get(value); |
|
71 |
keySet.add(key); |
|
72 |
} |
|
73 |
else { |
|
74 |
keySet = new ArrayList(); |
|
75 |
keySet.add(key); |
|
76 |
reverseMap.put(value, keySet); |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
public Object setProperty(String key, String value) { |
|
82 |
ArrayList keySet; |
|
83 |
|
|
84 |
Object returnValue = super.setProperty(key, value); |
|
85 |
if (reverseMap.containsKey(value)) { |
|
86 |
keySet = (ArrayList) reverseMap.get(value); |
|
87 |
keySet.add(key); |
|
88 |
} |
|
89 |
else { |
|
90 |
keySet = new ArrayList(); |
|
91 |
keySet.add(key); |
|
92 |
reverseMap.put(value, keySet); |
|
93 |
} |
|
94 |
|
|
95 |
return returnValue; |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* Gets the key associated with the provided value. If there |
|
100 |
* are several associated keys, returns one of them. |
|
101 |
* |
|
102 |
* @param value |
|
103 |
* @return The key associated with the provided value, or null |
|
104 |
* if the value is not present in the dictionary. If there |
|
105 |
* are several associated keys, returns one of them. |
|
106 |
*/ |
|
107 |
public String getAssociatedKey(String value) { |
|
108 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
|
109 |
if (keySet==null) return null; |
|
110 |
return (String) keySet.get(0); |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* Returns the keys associated with the provided value. If there |
|
115 |
* are several associated keys, returns one of them. |
|
116 |
* |
|
117 |
* @param value |
|
118 |
* @return An ArrayList containing the keys associated with the |
|
119 |
* provided value, or null if the value is not present in the |
|
120 |
* dictionary. |
|
121 |
*/ |
|
122 |
public ArrayList getAssociatedKeys(String value) { |
|
123 |
return (ArrayList) reverseMap.get(value); |
|
124 |
} |
|
125 |
|
|
126 |
public Object remove(Object key) { |
|
127 |
Object value = super.remove(key); |
|
128 |
if (value==null) return null; |
|
129 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
|
130 |
if (keySet.size()<=1) { |
|
131 |
//if it's the last key associated wit the value, remove the |
|
132 |
// value from the reverseDictionary |
|
133 |
reverseMap.remove(value); |
|
134 |
} |
|
135 |
else { |
|
136 |
// otherwise, remove the key from the list of associated keys |
|
137 |
keySet.remove(key); |
|
138 |
} |
|
139 |
return value; |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* |
|
144 |
* @param key An String object containing the key. |
|
145 |
* @param value An String object containing the value to associate with the |
|
146 |
* key. |
|
147 |
* |
|
148 |
* @throws ClassCastException If the key or the value are not String objects. |
|
149 |
*/ |
|
150 |
public Object put(Object key, Object value) throws ClassCastException { |
|
151 |
String stringKey, stringValue; |
|
152 |
Object oldValue=null; |
|
153 |
|
|
154 |
try { |
|
155 |
stringKey = (String) key; |
|
156 |
stringValue =(String) value; |
|
157 |
oldValue = setProperty(stringKey, stringValue); |
|
158 |
} |
|
159 |
catch (ClassCastException ex) { |
|
160 |
throw new ClassCastException("La clave y el valor deben ser objetos String."); |
|
161 |
} |
|
162 |
return oldValue; |
|
163 |
} |
|
164 |
|
|
165 |
} |
|
0 | 166 |
Also available in: Unified diff