root / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / Keys.java @ 6129
History | View | Annotate | Download (8.01 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.i18n.utils; |
5 |
|
6 |
import java.io.BufferedReader; |
7 |
import java.io.File; |
8 |
import java.io.FileInputStream; |
9 |
import java.io.FileNotFoundException; |
10 |
import java.io.IOException; |
11 |
import java.io.InputStreamReader; |
12 |
import java.io.UnsupportedEncodingException; |
13 |
import java.util.Enumeration; |
14 |
import java.util.HashMap; |
15 |
import java.util.HashSet; |
16 |
import java.util.Iterator; |
17 |
import java.util.Properties; |
18 |
import java.util.regex.Matcher; |
19 |
import java.util.regex.Pattern; |
20 |
import java.util.regex.PatternSyntaxException; |
21 |
|
22 |
import org.kxml2.io.KXmlParser; |
23 |
import org.xmlpull.v1.XmlPullParserException; |
24 |
|
25 |
/**
|
26 |
* @author cesar
|
27 |
*
|
28 |
*/
|
29 |
public class Keys { |
30 |
private ConfigOptions config;
|
31 |
|
32 |
public Keys(ConfigOptions config) {
|
33 |
this.config = config;
|
34 |
} |
35 |
|
36 |
public void load() { |
37 |
Project project; |
38 |
for (int currentProject=0; currentProject<config.projects.size(); currentProject++) { |
39 |
project = ((Project)config.projects.get(currentProject)); |
40 |
/**
|
41 |
* There is two options, "properties" and "sources". "sources" is the default, so
|
42 |
* if there was something different to "properties", we assume "sources".
|
43 |
*/
|
44 |
if (!project.sourceKeys.equals("properties")) { |
45 |
project.dictionaries = loadProjectFromSources(project); |
46 |
} |
47 |
else {
|
48 |
project.dictionaries = loadProjectFromProperties(project); |
49 |
} |
50 |
} |
51 |
} |
52 |
|
53 |
private HashMap loadProjectFromSources(Project project) { |
54 |
// always start with an empty HashMap when loading
|
55 |
HashMap dictionaries = new HashMap(); |
56 |
String lang;
|
57 |
|
58 |
/**
|
59 |
* The keys obtained from the sources and the config.xml files of the
|
60 |
* plugins.
|
61 |
*/
|
62 |
HashSet keys = new HashSet(); |
63 |
/**
|
64 |
* The translations loaded from the property files of the project.
|
65 |
*/
|
66 |
|
67 |
project.dictionaries = loadProjectFromProperties(project); |
68 |
|
69 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
70 |
lang = config.languages[currentLang]; |
71 |
|
72 |
keys = loadKeysFromSources(project.dir, keys); |
73 |
Properties currentDict = (Properties) project.dictionaries.get(lang); |
74 |
Iterator keysIterator = keys.iterator();
|
75 |
String key;
|
76 |
// add missing keys
|
77 |
while (keysIterator.hasNext()) {
|
78 |
key = (String) keysIterator.next();
|
79 |
if (!currentDict.containsKey(key)) {
|
80 |
currentDict.put(key, "");
|
81 |
} |
82 |
} |
83 |
// remove extra keys
|
84 |
Enumeration dictKey = currentDict.keys();
|
85 |
while (dictKey.hasMoreElements()) {
|
86 |
key = (String) dictKey.nextElement();
|
87 |
if (!keys.contains(key)) {
|
88 |
currentDict.remove(key); |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
return dictionaries;
|
94 |
} |
95 |
|
96 |
|
97 |
private HashSet loadKeysFromSources(String directory, HashSet keys) { |
98 |
File dir = new File(directory); |
99 |
File files[] = dir.listFiles(); |
100 |
if (files!=null) { |
101 |
|
102 |
Pattern keyPattern1 = Pattern.compile("(PluginServices|Messages)\\.(getText|getString|get)\\(.*\"(.*)\".*\\)"); |
103 |
Matcher keyMatcher1 = keyPattern1.matcher(""); |
104 |
|
105 |
for (int i=0; i<files.length; i++) { |
106 |
if (files[i].isDirectory()) {
|
107 |
keys = loadKeysFromSources(files[i].toString(), keys); |
108 |
} |
109 |
if (files[i].getName().toLowerCase().equals("PluginServices")) { |
110 |
//[Messages.]getText(...)
|
111 |
Pattern PsPattern = Pattern.compile("(Messages\\.)*getText\\(.*\"(.*)\".*\\)"); |
112 |
Matcher PsMatcher = PsPattern.matcher(""); |
113 |
|
114 |
FileInputStream fis=null; |
115 |
try {
|
116 |
fis = new FileInputStream(files[i]); |
117 |
|
118 |
BufferedReader currentFile=null; |
119 |
try {
|
120 |
currentFile = new BufferedReader(new InputStreamReader(fis, config.encoding)); |
121 |
} catch (UnsupportedEncodingException e1) { |
122 |
// TODO Auto-generated catch block
|
123 |
e1.printStackTrace(); |
124 |
} |
125 |
|
126 |
String line = null; |
127 |
try {
|
128 |
while((line = currentFile.readLine()) != null) { |
129 |
PsMatcher.reset(line); |
130 |
while (PsMatcher.find()) {
|
131 |
keys.add(PsMatcher.group(2));
|
132 |
} |
133 |
} |
134 |
currentFile.close(); |
135 |
} catch (IOException e) { |
136 |
System.err.println("Error inesperado:"+ e.getLocalizedMessage()); |
137 |
} |
138 |
} catch (FileNotFoundException e) { |
139 |
// the file is supposed to exist, so we are never supposed to arrive here
|
140 |
System.err.println("Error inesperado:"+ e.getLocalizedMessage()); |
141 |
} |
142 |
} |
143 |
else if (files[i].getName().toLowerCase().endsWith(".java")) { |
144 |
FileInputStream fis=null; |
145 |
try {
|
146 |
fis = new FileInputStream(files[i]); |
147 |
|
148 |
BufferedReader currentFile=null; |
149 |
try {
|
150 |
currentFile = new BufferedReader(new InputStreamReader(fis, config.encoding)); |
151 |
} catch (UnsupportedEncodingException e1) { |
152 |
// TODO Auto-generated catch block
|
153 |
e1.printStackTrace(); |
154 |
} |
155 |
|
156 |
String line = null; |
157 |
try {
|
158 |
while((line = currentFile.readLine()) != null) { |
159 |
keyMatcher1.reset(line); |
160 |
while (keyMatcher1.find()) {
|
161 |
keys.add(keyMatcher1.group(3));
|
162 |
System.out.println(keyMatcher1.group(3)); |
163 |
} |
164 |
} |
165 |
currentFile.close(); |
166 |
} catch (IOException e) { |
167 |
System.err.println("Error inesperado:"+ e.getLocalizedMessage()); |
168 |
} |
169 |
} catch (FileNotFoundException e) { |
170 |
// the file is supposed to exist, so we are never supposed to arrive here
|
171 |
System.err.println("Error inesperado:"+ e.getLocalizedMessage()); |
172 |
} |
173 |
} |
174 |
else if (files[i].getName().equalsIgnoreCase("config.xml")) { |
175 |
keys = loadKeysFromXml(files[i], keys); |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
return keys;
|
181 |
} |
182 |
|
183 |
|
184 |
private HashSet loadKeysFromXml(File fileName, HashSet keys) { |
185 |
KXmlParser parser = new KXmlParser();
|
186 |
String tagname, attribute;
|
187 |
|
188 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
189 |
try {
|
190 |
parser.setInput(new FileInputStream(fileName), null); |
191 |
} catch (FileNotFoundException e1) { |
192 |
System.err.println(e1.getLocalizedMessage());
|
193 |
return keys;
|
194 |
} catch (XmlPullParserException e1) {
|
195 |
// No podemos leer el fichero de configuraci?n. Usamos valores por defecto
|
196 |
System.err.println("Aviso: error al cargar el fichero "+fileName); |
197 |
return keys;
|
198 |
} |
199 |
|
200 |
try {
|
201 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
202 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
203 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
204 |
tagname = parser.getName(); |
205 |
if (tagname.equals("menu") || tagname.equals("action-tool") || tagname.equals("selectable-tool") || tagname.equals("entry")) { |
206 |
attribute = parser.getAttributeValue(null, "text"); |
207 |
if (attribute!=null) { |
208 |
String menuParts[] = attribute.split("/"); |
209 |
for (int i=0; i<menuParts.length; i++) { |
210 |
keys.add(menuParts[i]); |
211 |
} |
212 |
} |
213 |
|
214 |
attribute = parser.getAttributeValue(null, "tooltip"); |
215 |
if (attribute!=null) { |
216 |
keys.add(attribute); |
217 |
} |
218 |
|
219 |
|
220 |
} |
221 |
else if (tagname.equals("extension")) { |
222 |
attribute = parser.getAttributeValue(null, "description"); |
223 |
if (attribute!=null) { |
224 |
keys.add(attribute); |
225 |
} |
226 |
} |
227 |
} |
228 |
} |
229 |
} catch (XmlPullParserException e1) {
|
230 |
e1.getLocalizedMessage(); |
231 |
} catch (IOException e1) { |
232 |
e1.getLocalizedMessage(); |
233 |
} |
234 |
return keys;
|
235 |
} |
236 |
|
237 |
private HashMap loadProjectFromProperties(Project project) { |
238 |
// always start with an empty HashMap when loading
|
239 |
HashMap dictionaries = new HashMap(); |
240 |
String lang;
|
241 |
Properties dictionary;
|
242 |
|
243 |
FileInputStream stream=null; |
244 |
|
245 |
for (int currentLang=0; currentLang<config.languages.length; currentLang++) { |
246 |
lang = config.languages[currentLang]; |
247 |
dictionary = new Properties(); |
248 |
try {
|
249 |
// different for spanish...
|
250 |
if (lang.equals("es")) { |
251 |
stream = new FileInputStream(project.propertyDir+File.separator+project.basename+".properties"); |
252 |
} |
253 |
else {
|
254 |
stream = new FileInputStream(project.propertyDir+File.separator+project.basename+"_"+lang+".properties"); |
255 |
} |
256 |
try {
|
257 |
dictionary.load(stream); |
258 |
} catch (IOException e) { |
259 |
System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
260 |
} |
261 |
} catch (FileNotFoundException e) { |
262 |
System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage()); |
263 |
} |
264 |
dictionaries.put(lang, dictionary); |
265 |
} |
266 |
return dictionaries;
|
267 |
} |
268 |
} |