Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / AddNewTranslations.java @ 40559

History | View | Annotate | Download (5.84 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/**
25
 * 
26
 */
27
package org.gvsig.i18n.utils;
28

    
29
import java.io.BufferedReader;
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.IOException;
34
import java.io.InputStreamReader;
35
import java.io.UnsupportedEncodingException;
36
import java.util.Enumeration;
37
import java.util.Properties;
38

    
39
/**
40
 * @author cesar
41
 *
42
 */
43
public class AddNewTranslations {
44
        // The filename which stores the configuration (may be overriden by the command line parameter)
45
        private String configFileName = "config.xml";
46
        
47
        // Object to load and store the config options
48
        private ConfigOptions config;
49
        
50
        private TranslationDatabase database;
51

    
52
        /**
53
         * @param args
54
         */
55
        public static void main(String[] args) {
56
                AddNewTranslations process = new AddNewTranslations();
57
                
58
                // load command line parameters
59
                if (!process.readParameters(args)) {
60
                        usage();
61
                        System.exit(-1);
62
                }
63
                
64
                // transfer control to the program's main loop
65
                process.start();
66
        }
67
        
68
        private void start() {
69
                // load config options from the config file
70
                if (!loadConfig()) {
71
                        System.out.println("Error leyendo el fichero de configuraci?n.");
72
                        usage();
73
                        System.exit(-1);
74
                }
75
                
76
                loadDataBase();
77
                
78
                readNewTranslations();
79
                
80
                database.save();
81
        }
82

    
83
        /**
84
         *  Reads the command line parameters */
85
        private boolean readParameters(String[] args) {
86
                String configPair[];
87

    
88
                for (int i=0; i<args.length; i++) {
89
                        configPair = args[i].split("=",2);
90
                        if ( (configPair[0].equals("-c") || configPair[0].equals("--config"))
91
                                        && configPair.length==2) {
92
                                configFileName = configPair[1];
93
                        }
94
                        else {
95
                                return false;
96
                        }
97
                }
98
                return true;
99
        }
100
        
101
        private boolean loadConfig() {
102
                config = new ConfigOptions(configFileName);
103
                config.load();
104
                return true;
105
        }
106
        
107
        private static void usage() {
108
                System.out.println("Uso: AddNewTranslations [OPCION]");
109
                System.out.println("\t-c\t--config=configFile");
110
        }
111
        
112
        private void loadDataBase() {
113
                database = new TranslationDatabase(config);
114
                database.load();
115
        }
116
        
117
        private void readNewTranslations() {
118
                String lang, key, value, oldValue;
119
                
120
                for (int i=0; i<config.languages.length; i++) {
121
                        lang = config.languages[i];
122
                        try {
123
                                FileInputStream fisProp = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
124
                                Properties prop = new Properties();
125
                                try {
126
                                        prop.load(fisProp);
127
                                        Enumeration keysEnum  = prop.keys();
128
                                        while (keysEnum.hasMoreElements()){
129
                                                key = (String) keysEnum.nextElement();
130
                                                value = prop.getProperty(key);
131
                                                if (value!=null && !value.equals("")) {
132
                                                        if (!database.containsKey(lang, key)) {
133
                                                                System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value);
134
                                                                database.setTranslation(lang, key, value);
135
                                                        }
136
                                                        else {
137
                                                                oldValue = database.setTranslation(lang, key, value);
138
                                                                if (!oldValue.equals(value)) {
139
                                                                        System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value);
140
                                                                        System.out.println("Valor anterior: "+database.getTranslation(lang, key));
141
                                                                }
142
                                                        }
143
                                                }
144
                                        }
145
                                } catch (IOException e) {
146
                                        System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+e.getLocalizedMessage());
147
                                }
148
                                
149
                        } catch (FileNotFoundException e) {
150
                                try {
151
                                        FileInputStream fis = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding);
152
                                        
153
                                        BufferedReader currentFile=null;
154
                                        try {
155
                                                currentFile = new BufferedReader(new InputStreamReader(fis, config.outputEncoding));
156
                                        } catch (UnsupportedEncodingException e1) {
157
                                                // TODO Auto-generated catch block
158
                                                e1.printStackTrace();
159
                                        }
160
                                        
161
                                    String line = null;
162
                                    try {
163
                                                while((line = currentFile.readLine()) != null) {
164
                                                        String[] parts = line.split("=");
165
                                                        if (parts.length == 2) {
166
                                                                key = parts[0];
167
                                                                value = parts[1];
168
                                                                if (value!=null && !value.equals("")) {
169
                                                                        if (!database.containsKey(lang, key)) {
170
                                                                                System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value);
171
                                                                                database.setTranslation(lang, key, value);
172
                                                                        }
173
                                                                        else {
174
                                                                                oldValue = database.setTranslation(lang, key, value);
175
                                                                                if (!oldValue.equals(value)) {
176
                                                                                        System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value);
177
                                                                                        System.out.println("Valor anterior: "+database.getTranslation(lang, key));
178
                                                                                }
179
                                                                        }
180
                                                                }
181
                                                        }
182
                                                        else {
183
                                                                System.err.println("Error leyendo traducciones para idioma ["+lang+"].");
184
                                                                System.err.println("L?nea: "+line);
185
                                                        }
186
                                                }
187
                                            currentFile.close();
188
                                        } catch (IOException ex) {
189
                                                System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+ex.getLocalizedMessage());
190
                                        }
191
                                        
192
                                } catch (FileNotFoundException e1) {
193
                                        System.out.println("Aviso -- no se encontraron nuevas traducciones para el idioma ["+lang+"].");
194
                                }
195
                        }
196
                        
197
                }
198
                
199
        }
200
        
201
}