svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / AddNewTranslations.java @ 40769
History | View | Annotate | Download (5.83 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 |
package org.gvsig.i18n.utils; |
25 |
|
26 |
import java.io.BufferedReader; |
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.FileNotFoundException; |
30 |
import java.io.IOException; |
31 |
import java.io.InputStreamReader; |
32 |
import java.io.UnsupportedEncodingException; |
33 |
import java.util.Enumeration; |
34 |
import java.util.Properties; |
35 |
|
36 |
/**
|
37 |
* @author cesar
|
38 |
*
|
39 |
*/
|
40 |
public class AddNewTranslations { |
41 |
// The filename which stores the configuration (may be overriden by the command line parameter)
|
42 |
private String configFileName = "config.xml"; |
43 |
|
44 |
// Object to load and store the config options
|
45 |
private ConfigOptions config;
|
46 |
|
47 |
private TranslationDatabase database;
|
48 |
|
49 |
/**
|
50 |
* @param args
|
51 |
*/
|
52 |
public static void main(String[] args) { |
53 |
AddNewTranslations process = new AddNewTranslations();
|
54 |
|
55 |
// load command line parameters
|
56 |
if (!process.readParameters(args)) {
|
57 |
usage(); |
58 |
System.exit(-1); |
59 |
} |
60 |
|
61 |
// transfer control to the program's main loop
|
62 |
process.start(); |
63 |
} |
64 |
|
65 |
private void start() { |
66 |
// load config options from the config file
|
67 |
if (!loadConfig()) {
|
68 |
System.out.println("Error leyendo el fichero de configuraci?n."); |
69 |
usage(); |
70 |
System.exit(-1); |
71 |
} |
72 |
|
73 |
loadDataBase(); |
74 |
|
75 |
readNewTranslations(); |
76 |
|
77 |
database.save(); |
78 |
} |
79 |
|
80 |
/**
|
81 |
* Reads the command line parameters */
|
82 |
private boolean readParameters(String[] args) { |
83 |
String configPair[]; |
84 |
|
85 |
for (int i=0; i<args.length; i++) { |
86 |
configPair = args[i].split("=",2); |
87 |
if ( (configPair[0].equals("-c") || configPair[0].equals("--config")) |
88 |
&& configPair.length==2) {
|
89 |
configFileName = configPair[1];
|
90 |
} |
91 |
else {
|
92 |
return false; |
93 |
} |
94 |
} |
95 |
return true; |
96 |
} |
97 |
|
98 |
private boolean loadConfig() { |
99 |
config = new ConfigOptions(configFileName);
|
100 |
config.load(); |
101 |
return true; |
102 |
} |
103 |
|
104 |
private static void usage() { |
105 |
System.out.println("Uso: AddNewTranslations [OPCION]"); |
106 |
System.out.println("\t-c\t--config=configFile"); |
107 |
} |
108 |
|
109 |
private void loadDataBase() { |
110 |
database = new TranslationDatabase(config);
|
111 |
database.load(); |
112 |
} |
113 |
|
114 |
private void readNewTranslations() { |
115 |
String lang, key, value, oldValue;
|
116 |
|
117 |
for (int i=0; i<config.languages.length; i++) { |
118 |
lang = config.languages[i]; |
119 |
try {
|
120 |
FileInputStream fisProp = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties"); |
121 |
Properties prop = new Properties(); |
122 |
try {
|
123 |
prop.load(fisProp); |
124 |
Enumeration keysEnum = prop.keys();
|
125 |
while (keysEnum.hasMoreElements()){
|
126 |
key = (String) keysEnum.nextElement();
|
127 |
value = prop.getProperty(key); |
128 |
if (value!=null && !value.equals("")) { |
129 |
if (!database.containsKey(lang, key)) {
|
130 |
System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value); |
131 |
database.setTranslation(lang, key, value); |
132 |
} |
133 |
else {
|
134 |
oldValue = database.setTranslation(lang, key, value); |
135 |
if (!oldValue.equals(value)) {
|
136 |
System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value); |
137 |
System.out.println("Valor anterior: "+database.getTranslation(lang, key)); |
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
} catch (IOException e) { |
143 |
System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+e.getLocalizedMessage()); |
144 |
} |
145 |
|
146 |
} catch (FileNotFoundException e) { |
147 |
try {
|
148 |
FileInputStream fis = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding); |
149 |
|
150 |
BufferedReader currentFile=null; |
151 |
try {
|
152 |
currentFile = new BufferedReader(new InputStreamReader(fis, config.outputEncoding)); |
153 |
} catch (UnsupportedEncodingException e1) { |
154 |
// TODO Auto-generated catch block
|
155 |
e1.printStackTrace(); |
156 |
} |
157 |
|
158 |
String line = null; |
159 |
try {
|
160 |
while((line = currentFile.readLine()) != null) { |
161 |
String[] parts = line.split("="); |
162 |
if (parts.length == 2) { |
163 |
key = parts[0];
|
164 |
value = parts[1];
|
165 |
if (value!=null && !value.equals("")) { |
166 |
if (!database.containsKey(lang, key)) {
|
167 |
System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value); |
168 |
database.setTranslation(lang, key, value); |
169 |
} |
170 |
else {
|
171 |
oldValue = database.setTranslation(lang, key, value); |
172 |
if (!oldValue.equals(value)) {
|
173 |
System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value); |
174 |
System.out.println("Valor anterior: "+database.getTranslation(lang, key)); |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
else {
|
180 |
System.err.println("Error leyendo traducciones para idioma ["+lang+"]."); |
181 |
System.err.println("L?nea: "+line); |
182 |
} |
183 |
} |
184 |
currentFile.close(); |
185 |
} catch (IOException ex) { |
186 |
System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+ex.getLocalizedMessage()); |
187 |
} |
188 |
|
189 |
} catch (FileNotFoundException e1) { |
190 |
System.out.println("Aviso -- no se encontraron nuevas traducciones para el idioma ["+lang+"]."); |
191 |
} |
192 |
} |
193 |
|
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
} |