Statistics
| Revision:

root / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / Keys.java @ 11120

History | View | Annotate | Download (13.7 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.FileOutputStream;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
import java.io.UnsupportedEncodingException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Iterator;
17
import java.util.regex.Matcher;
18
import java.util.regex.Pattern;
19

    
20
import org.kxml2.io.KXmlParser;
21
import org.xmlpull.v1.XmlPullParserException;
22

    
23
/**
24
 * @author cesar
25
 *
26
 */
27
public class Keys {
28
        private ConfigOptions config;
29
        
30
        public Keys(ConfigOptions config) {
31
                this.config = config;
32
        }
33
        
34
        public void load() {
35
                Project project;
36
                for (int currentProject=0; currentProject<config.projects.size(); currentProject++) {
37
                        project = ((Project)config.projects.get(currentProject));
38
                        /**
39
                         * There is two options, "properties" and "sources". "sources" is the default, so
40
                         * if there was something different to "properties", we assume "sources".
41
                         */
42
                        if (!project.sourceKeys.equals("properties")) {
43
                                project.dictionaries = loadProjectFromSources(project);
44
                        }
45
                        else {
46
                                // load the keys for each language from the property file
47
                                 project.dictionaries = loadProjectFromProperties(project);
48
                                 // add missing keys tp each language
49
                                 completeKeys(project);
50
                         }
51
                }
52
        }
53
        
54
        public void save() {
55
                Project project;
56
                OrderedProperties dict;
57
                FileOutputStream stream=null;
58
                String lang;
59
                
60
                for (int currentProject=0; currentProject<config.projects.size(); currentProject++) {
61
                        project = ((Project)config.projects.get(currentProject));
62
                        
63
                        for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
64
                                lang = (String) config.languages[currentLang];
65
                                dict = (OrderedProperties) project.dictionaries.get(lang);
66
                                
67
                                if (dict.size()>0) {
68
                                        // ensure the directory exists
69
                                        File propertyDir = new File(project.propertyDir);
70
                                        if (propertyDir.mkdirs())
71
                                                System.out.println("Aviso -- directorio creado: "+project.propertyDir);
72
                                        
73
                                        try {
74
                                                // different for spanish...
75
                                                if (lang.equals("es")) {
76
                                                        stream = new FileOutputStream(project.propertyDir+File.separator+project.basename+".properties");
77
                                                }
78
                                                else {
79
                                                        stream = new FileOutputStream(project.propertyDir+File.separator+project.basename+"_"+lang+".properties");
80
                                                }
81
                                        } catch (FileNotFoundException e) {
82
                                                // TODO Auto-generated catch block
83
                                                e.printStackTrace();
84
                                        }
85
        
86
                                        try {
87
                                                dict.store(stream, "Translations for language ["+lang+"]");
88
                                        } catch (IOException e) {
89
                                                // TODO Auto-generated catch block
90
                                                e.printStackTrace();
91
                                        }
92
                                }
93
                        }
94
                }        
95
        }
96
        
97
        private HashMap loadProjectFromSources(Project project) {
98
                // always start with an empty HashMap when loading
99
                HashMap dictionaries = new HashMap();                
100
                String lang;
101
                
102
                /**
103
                 * The keys obtained from the sources and the config.xml files of the
104
                 * plugins.
105
                 */
106
                HashSet keys = new HashSet();
107
                /**
108
                 * The translations loaded from the property files of the project.
109
                 */
110
                
111
                dictionaries =  loadProjectFromProperties(project);
112
                
113
                for (int i=0; i<project.srcDirs.length; i++) {
114
                        try {
115
                                keys = loadKeysFromSources(ConfigOptions.getAbsolutePath(File.separator, project.dir+File.separator+project.srcDirs[i]), keys);
116
                        }
117
                        catch (IOException ex) {
118
                                // It there was an error reading the directory, just warn and skip the dir
119
                                System.err.println(project.dir +" -- Aviso: no se pudo leer el directorio "+project.dir+File.separator+project.srcDirs[i]);
120
                        }
121
                }
122
                
123
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
124
                        lang = config.languages[currentLang];
125

    
126
                        OrderedProperties currentDict = (OrderedProperties) dictionaries.get(lang);
127
                        Iterator keysIterator = keys.iterator();
128
                        String key;
129
                        // add missing keys
130
                        while (keysIterator.hasNext()) {
131
                                key = (String) keysIterator.next();
132
                                if (!currentDict.containsKey(key)) {
133
                                        currentDict.put(key, "");
134
                                        System.out.println(project.dir+" -- Aviso -- clave a?adida: "+key);
135
                                }
136
                        }
137
                        
138
                        // remove extra keys
139
                        // first make a list of keys to remove, because it's not possible to access the iterator and the TreeMap at the same time
140
                /*        HashSet removedKeys = new HashSet();
141
                        Iterator dictKey = currentDict.keySet().iterator();
142
                        while (dictKey.hasNext()) {
143
                                key = (String) dictKey.next();
144
                                if (!keys.contains(key)) {
145
                                        removedKeys.add(key);
146
                                        System.out.println(project.dir+" -- Aviso -- clave eliminada: "+key);
147
                                }
148
                        }
149
                        // now we really remove the keys
150
                        Iterator removedKeysIt = removedKeys.iterator();
151
                        while (removedKeysIt.hasNext()) {
152
                                key = (String) removedKeysIt.next();
153
                                currentDict.remove(key);
154
                        }*/        
155
                }
156

    
157
                return dictionaries;
158
        }
159
        
160
        /**
161
         * Reads the keys from all the languages, to make a unique list containing
162
         * all the keys. 
163
         */
164
        public void completeKeys(Project project) {
165
                /* The list of all the keys */
166
                HashSet keys = new HashSet();
167
                // always start with an empty HashMap when loading
168
                //HashMap dictionaries = new HashMap();                
169
                String lang;
170
                
171
                // calculate all the keys
172
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
173
                        lang = config.languages[currentLang];
174
                        
175
                        OrderedProperties currentDict = (OrderedProperties) project.dictionaries.get(lang);
176
                        if (currentDict==null) {
177
                                currentDict = new OrderedProperties();
178
                                project.dictionaries.put(lang, currentDict);
179
                        }
180
                        else {
181
                                Iterator keysIterator = currentDict.keySet().iterator();
182
                                String key;
183
                                // add missing keys
184
                                while (keysIterator.hasNext()) {
185
                                        key = (String) keysIterator.next();
186
                                        keys.add(key);
187
                                }        
188
                        }
189
                }
190
                
191
                // add missing keys to each language
192
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
193
                        lang = config.languages[currentLang];
194
                        
195
                        OrderedProperties currentDict = (OrderedProperties) project.dictionaries.get(lang);
196
                        Iterator keysIterator = keys.iterator();
197
                        String key;
198
                        // add missing keys
199
                        while (keysIterator.hasNext()) {
200
                                key = (String) keysIterator.next();
201
                                if (!currentDict.containsKey(key)) {
202
                                        currentDict.put(key, "");
203
                                        System.out.println(project.dir+" -- Aviso -- clave a?adida: "+key);
204
                                }
205
                        }                        
206
                }
207
        }
208
        
209
        
210
        private HashSet loadKeysFromSources(String directory, HashSet keys) {
211
                String key;
212
                File dir = new File(directory);
213
                File files[] = dir.listFiles();
214
                final int BLOCKSIZE = 8192;
215
                char[] partialBuffer = new char[BLOCKSIZE+1];
216
                String text;
217
                StringBuffer buffer;
218
                
219
                // stores the position of the newlines
220
                //ArrayList newLines = new ArrayList();
221
                //        int lineNumber;
222
                
223
                if (files!=null) {
224
                        //Pattern keyPattern = Pattern.compile("(PluginServices|Messages)\\.(getText|getString|get)\\([^\"\\)]*\"([^\"]*)\"[^\\)]*\\)");
225
                        Pattern keyPattern = Pattern.compile(
226
                                        "(PluginServices|Messages)\\p{Space}*\\.\\p{Space}*(getText|getString|get)\\p{Space}*\\([^\"\\)]*\"([^\"]*)\"[^\\+\\)]*(\\+[^\\)]*)*\\)");
227
                        Matcher keyMatcher = keyPattern.matcher("");
228
                                        //"(PluginServices|Messages)\\.(getText|getString|get)\\p{Space}*\\([^\"\\)]*\"([^\"]*)\"[^\\+\\)]*(\\+[^\"]*\"([^\"]*)\"[^\\+\\)]*)*\\)");
229
                        //Pattern newLinePattern = Pattern.compile("\n");
230
                        //Matcher newLineMatcher = newLinePattern.matcher("");
231
                        
232
                        for (int i=0; i<files.length; i++) {
233
                                if (files[i].isDirectory()) {
234
                                        keys = loadKeysFromSources(files[i].toString(), keys);
235
                                        continue;
236
                                }
237
                                else if (files[i].getName().toLowerCase().equals("PluginServices.java")) {
238
                                        
239
                                        //[Messages.]getText(...)
240
                                        Pattern PsPattern = Pattern.compile("(Messages\\.)*getText\\([^\"\\)]*\"([^\"]*)\"[^\\)]*\\)");
241
                                        Matcher PsMatcher = PsPattern.matcher("");
242
                                        
243
                                        FileInputStream fis=null;
244
                                        try {
245
                                                fis = new FileInputStream(files[i]);
246
                                                
247
                                                buffer = new StringBuffer();
248
                                                BufferedReader currentFile=null;
249
                                                
250
                                                currentFile = new BufferedReader(new InputStreamReader(fis, config.sourcesEncoding));
251
                                                while (currentFile.read(partialBuffer, 0, BLOCKSIZE)!=-1) {
252
                                                        buffer.append(partialBuffer);
253
                                                }
254
                                                text = buffer.toString();
255
                                                
256
                                                
257
                                                PsMatcher.reset(text);
258
                                                
259
                                                while (PsMatcher.find()) {
260
                                                        key = PsMatcher.group(2);
261
                                                        if (!key.equals(""))
262
                                                                keys.add(key);
263
                                                }
264
                                                currentFile.close();
265
                                        } catch (UnsupportedEncodingException e1) {
266
                                                System.err.println(e1.getLocalizedMessage());
267
                                                continue;
268
                                        }
269
                                        catch (IOException e1) {
270
                                                System.err.println(e1.getLocalizedMessage());
271
                                                continue;
272
                                        }
273
                                }
274
                                else if (files[i].getName().toLowerCase().endsWith(".java")) {
275
                                        FileInputStream fis=null;
276
                                        try {
277
                                                fis = new FileInputStream(files[i]);
278
                                                BufferedReader currentFile=null;
279
                                                
280
                                                currentFile = new BufferedReader(new InputStreamReader(fis, config.sourcesEncoding));
281
                                                buffer = new StringBuffer();
282
                                                
283
                                                int readChars; // number of characters which were read
284
                                                while ( (readChars = currentFile.read(partialBuffer, 0, BLOCKSIZE)) != -1) {
285
                                                        buffer.append(partialBuffer, 0, readChars);
286
                                                }
287
                                                text = buffer.toString();
288
                                                
289
                                                /*newLineMatcher.reset(text);
290
                                                 while (newLineMatcher.find()) {
291
                                                 newLines.add(new Integer(newLineMatcher.end()-1));
292
                                                 }*/
293
                                                // lineNumber=1;
294
                                                
295
                                                keyMatcher.reset(text);
296
                                                
297
                                                while (keyMatcher.find()) {
298
                                                        
299
                                                        // find out in which line number we are
300
                                                        /*while (keyMatcher.start() > ((Integer)newLines.get(lineNumber)).intValue() ) lineNumber++;
301
                                                         System.out.println("FileName: "+files[i].getCanonicalPath()+"; lineNumber: "+lineNumber);*/
302
                                                        
303
//                                                        StringBuffer keyBuffer = new StringBuffer();
304
                                                        
305
//                                                        for (int ii=0; ii<=keyMatcher.groupCount(); ii++) {
306
//                                                        System.out.println("group: "+ ii+ "; " + keyMatcher.group(ii));
307
//                                                        }
308
//                                                        for (int groupNumb=3; groupNumb<=keyMatcher.groupCount(); groupNumb+=2) {
309
//                                                        if (keyMatcher.group(groupNumb)!=null)
310
//                                                        keyBuffer.append(keyMatcher.group(groupNumb));
311
//                                                        }
312
                                                        
313
                                                        if (keyMatcher.group(4)!=null) {
314
                                                                System.err.println("\nError: clave repartida en varias l?neas");
315
                                                                System.err.println("Fichero: "+files[i].getCanonicalPath());
316
                                                                System.err.println("C?digo: ");
317
                                                                System.err.println(keyMatcher.group());
318
                                                        }
319
                                                        else {
320
                                                                key = keyMatcher.group(3);
321
                                                                if (!key.equals(""))
322
                                                                        keys.add(key);
323
                                                        }
324
                                                }
325
                                                currentFile.close();
326
                                        } catch (UnsupportedEncodingException e1) {
327
                                                System.err.println(e1.getLocalizedMessage());
328
                                                continue;
329
                                        }
330
                                        catch (IOException e1) {
331
                                                System.err.println(e1.getLocalizedMessage());
332
                                                continue;
333
                                        }
334
                                }
335
                                else if (files[i].getName().equalsIgnoreCase("config.xml")) {
336
                                        keys = loadKeysFromXml(files[i], keys);
337
                                }
338
                        }
339
                }
340
                
341
                return keys;
342
        }
343
        
344
        
345
        private HashSet loadKeysFromXml(File fileName, HashSet keys) {
346
                KXmlParser parser = new KXmlParser();
347
                String tagname, attribute;
348
                
349
                // we use null encoding, in this way kxml2 tries to detect the encoding
350
                try {
351
                        parser.setInput(new FileInputStream(fileName), null);
352
                } catch (FileNotFoundException e1) {
353
                        System.err.println(e1.getLocalizedMessage());
354
                        return keys;
355
                } catch (XmlPullParserException e1) {
356
                        // No podemos leer el fichero de configuraci?n. Usamos valores por defecto
357
                        System.err.println("Aviso: error al cargar el fichero "+fileName);
358
                        return keys;
359
                }
360
                
361
                try {
362
                        for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
363
                                // este bucle externo recorre las etiquetas de primer y segundo nivel
364
                                if (parser.getEventType()==KXmlParser.START_TAG) {
365
                                        tagname = parser.getName();
366
                                        if (tagname.equals("menu") || tagname.equals("action-tool") || tagname.equals("selectable-tool") || tagname.equals("entry")) {
367
                                                attribute = parser.getAttributeValue(null, "text");
368
                                                if (attribute!=null) {
369
                                                        String menuParts[] = attribute.split("/");
370
                                                        for (int i=0; i<menuParts.length; i++) {
371
                                                                if (!menuParts[i].equals(""))
372
                                                                        keys.add(menuParts[i]);
373
                                                        }
374
                                                }
375
                                                
376
                                                attribute = parser.getAttributeValue(null, "tooltip");
377
                                                if (attribute!=null && !attribute.equals("")) {
378
                                                        keys.add(attribute);
379
                                                }
380
                                                
381
                                                
382
                                        }
383
                                        else if (tagname.equals("combo-scale")) {
384
                                                attribute = parser.getAttributeValue(null, "label");
385
                                                if (attribute!=null && !attribute.equals("")) {
386
                                                        keys.add(attribute);
387
                                                }
388
                                        }
389
                                        else if (tagname.equals("tool-bar")) {
390
                                                attribute = parser.getAttributeValue(null, "name");
391
                                                if (attribute!=null && !attribute.equals("")) {
392
                                                        keys.add(attribute);
393
                                                }
394
                                        }
395

    
396
                                }
397
                        }        
398
                } catch (XmlPullParserException e1) {
399
                        e1.getLocalizedMessage();
400
                } catch (IOException e1) {
401
                        e1.getLocalizedMessage();
402
                }
403
                return keys;
404
        }
405
        
406
        private HashMap loadProjectFromProperties(Project project) {
407
                // always start with an empty HashMap when loading
408
                HashMap dictionaries = new HashMap();
409
                String lang;
410
                OrderedProperties dictionary;
411
                
412
                FileInputStream stream=null;
413
                
414
                for (int currentLang=0; currentLang<config.languages.length; currentLang++) {
415
                        lang = config.languages[currentLang];
416
                        dictionary = new OrderedProperties();
417
                        try {
418
                                // different for spanish...
419
                                if (lang.equals("es")) {
420
                                        stream = new FileInputStream(project.propertyDir+File.separator+project.basename+".properties");
421
                                }
422
                                else {
423
                                        stream = new FileInputStream(project.propertyDir+File.separator+project.basename+"_"+lang+".properties");
424
                                }
425
                                try {
426
                                        dictionary.load(stream);
427
                                } catch (IOException e) {
428
                                        System.err.println("Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
429
                                }
430
                        } catch (FileNotFoundException e) {
431
                                System.err.println(project.dir + " -- Error cargando la base de datos para el idioma: ["+lang+"]. "+e.getLocalizedMessage());
432
                        }
433
                        dictionaries.put(lang, dictionary);
434
                }
435
                return dictionaries;
436
        }
437
}