Statistics
| Revision:

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

History | View | Annotate | Download (15.3 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.File;
27
import java.io.FileInputStream;
28
import java.io.FileNotFoundException;
29
import java.io.IOException;
30
import java.util.ArrayList;
31

    
32
import org.kxml2.io.KXmlParser;
33
import org.xmlpull.v1.XmlPullParserException;
34

    
35
public class ConfigOptions {
36
        // Default values
37
        public String defaultBaseName = "text";
38
        public String defaultBaseDir = ".";
39
        public String databaseDir = "database";
40
        private String configFileName = "config.xml";
41
        public String[] languages;
42
        public ArrayList projects = new ArrayList();
43
        private String defaultLangList="ca;cs;de;en;es;eu;fr;gl;it;pt";
44
        public String sourceKeys = "sources";
45
        private String defaultPropertyDir = "config";
46
        public String[] outputLanguages={"en", "es"};
47
        public String outputDir="output";
48
        public String inputDir="input";
49
        public String[] defaultSrcDirs={"src"};
50
        
51
        
52
        /**
53
         * The character encoding of the Java source files, used to search keys in the sources.
54
         */
55
        public String sourcesEncoding = "ISO8859_1";
56
        /**
57
         * The character encoding of the generated output files for missing keys.
58
         */
59
        public String outputEncoding = "UTF8";
60
        
61
        /**
62
         * Creates a new ConfigOptions object.
63
         */
64
        public ConfigOptions() {
65
                // Now we transform all directories to absolute canonical paths, so
66
                // that the are easier to manage afterwards.
67
                // It's also done at the end of parseVars method, but we must also do
68
                // it here, because parseVars() might not get executed.
69
                try {
70
                        this.defaultBaseDir = getAbsolutePath(".", this.defaultBaseDir);
71
                        this.databaseDir = getAbsolutePath(this.defaultBaseDir, this.databaseDir);
72
                        this.outputDir = getAbsolutePath(this.defaultBaseDir, this.outputDir);
73
                        this.inputDir = getAbsolutePath(this.defaultBaseDir, this.inputDir);
74
                        
75
                        /*
76
                         * 
77
                         File baseDirFile = new File(this.defaultBaseDir);
78
                        this.defaultBaseDir = baseDirFile.getCanonicalPath();
79
                        File databaseDirFile = new File(this.databaseDir);
80
                        if (databaseDirFile.isAbsolute()) {
81
                                this.databaseDir = databaseDirFile.getCanonicalPath();
82
                        }
83
                        else {
84
                                this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath();
85
                        }
86
                        File outputDirFile = new File(this.outputDir);
87
                        if (outputDirFile.isAbsolute()) {
88
                                this.outputDir = outputDirFile.getCanonicalPath();
89
                        }
90
                        else {
91
                                this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath();
92
                        }
93
                        File inputDirFile = new File(this.inputDir);
94
                        if (inputDirFile.isAbsolute()) {
95
                                this.inputDir = inputDirFile.getCanonicalPath();
96
                        }
97
                        else {
98
                                this.inputDir = (new File(this.defaultBaseDir+File.separator+this.inputDir)).getCanonicalPath();
99
                        }
100
                        */
101
                } catch (IOException e) {
102
                        System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage());
103
                }
104
        }
105
        
106
        /**
107
         *  Creates a new ConfigOptions object, defining the config file to use.
108
         *  
109
         * @param configFileName The file name of the config file to use.
110
         */
111
        public ConfigOptions(String configFileName) {
112
                this.configFileName = configFileName;
113
                
114
                // Now we transform all directories to absolute canonical paths, so
115
                // that the are easier to manage afterwards.
116
                // It's also done at the end of parseVars method, but we must also do
117
                // it here, because parseVars() might not get executed.
118
                try {
119
                        this.defaultBaseDir = getAbsolutePath(".", this.defaultBaseDir);
120
                        this.databaseDir = getAbsolutePath(this.defaultBaseDir, this.databaseDir);
121
                        this.outputDir = getAbsolutePath(this.defaultBaseDir, this.outputDir);
122
                        this.inputDir = getAbsolutePath(this.defaultBaseDir, this.inputDir);
123
                        
124
                        /*File baseDirFile = new File(this.defaultBaseDir);
125
                        this.defaultBaseDir = baseDirFile.getCanonicalPath();
126
                        File databaseDirFile = new File(this.databaseDir);
127
                        if (databaseDirFile.isAbsolute()) {
128
                                this.databaseDir = databaseDirFile.getCanonicalPath();
129
                        }
130
                        else {
131
                                this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath();
132
                        }
133
                        File outputDirFile = new File(this.outputDir);
134
                        if (outputDirFile.isAbsolute()) {
135
                                this.outputDir = outputDirFile.getCanonicalPath();
136
                        }
137
                        else {
138
                                this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath();
139
                        }
140
                        File inputDirFile = new File(this.inputDir);
141
                        if (inputDirFile.isAbsolute()) {
142
                                this.inputDir = inputDirFile.getCanonicalPath();
143
                        }
144
                        else {
145
                                this.inputDir = (new File(this.defaultBaseDir+File.separator+this.inputDir)).getCanonicalPath();
146
                        }*/
147
                } catch (IOException e) {
148
                        System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage());
149
                }
150
        }
151
        
152
        /**
153
         * Sets the name of the config file to use.
154
         * 
155
         * @param configFileName
156
         */
157
        public void setConfigFile(String configFileName) {
158
                this.configFileName = configFileName;
159
        }
160
        
161
        /**
162
         * Gets the name of the config file in use.
163
         * 
164
         * @return The name of the config file in use.
165
         */
166
        public String getConfigFile() {
167
                return configFileName;
168
        }
169
        
170
        /**
171
         *  Loads the config parameters and the projects to consider from the XML
172
         * config file */
173
        public boolean load() {
174
                KXmlParser parser = new KXmlParser();
175
                
176
                // we use null encoding, in this way kxml2 tries to detect the encoding
177
                try {
178
                        parser.setInput(new FileInputStream(configFileName), null);
179
                } catch (FileNotFoundException e1) {
180
                        System.err.println(e1.getLocalizedMessage());
181
                        return false;
182
                } catch (XmlPullParserException e1) {
183
                        // No podemos leer el fichero de configuraci?n. Usamos valores por defecto
184
                        System.err.println("Aviso: no se pudo leer correctamente el fichero de configuraci?n. Se usar?n los valores por defecto.");
185
                        return false;
186
                }
187
                
188
                try {
189
                        for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
190
                                // este bucle externo recorre las etiquetas de primer y segundo nivel
191
                                if (parser.getEventType()==KXmlParser.START_TAG) {
192
                                        if (parser.getName().equals("config")) {
193
                                                parseVars(parser);
194
                                        }
195
                                        else if (parser.getName().equals("projects")) {
196
                                                parseProjects(parser);
197
                                        }
198
                                }
199
                        }        
200
                } catch (XmlPullParserException e1) {
201
                        e1.getLocalizedMessage();
202
                } catch (IOException e1) {
203
                        e1.getLocalizedMessage();
204
                }
205
                
206
                File outputDirFile = new File(outputDir);
207
                outputDirFile.mkdirs();
208
                File databaseDirFile = new File(databaseDir);
209
                databaseDirFile.mkdirs();
210
                return true;
211
        }
212

    
213
        private void parseVars(KXmlParser parser) throws XmlPullParserException, IOException {
214
                // recorremos todas las etiquetas 'variable' dentro de config
215
                int state;
216
                String name, value;
217
                
218
                for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("config") ; state=parser.next()) {
219
                        if (state==KXmlParser.START_TAG) {
220
                                if (parser.getName().equals("variable")) {
221
                                        name = parser.getAttributeValue(null, "name");
222
                                        value = parser.getAttributeValue(null, "value");
223
                                        if (name!=null && value!=null) {
224
                                                value = parser.getAttributeValue(null, "value");
225
                                                if (parser.getAttributeValue(null, "name").equals("basename")) {
226
                                                        defaultBaseName = parser.getAttributeValue(null, "value");
227
                                                }
228
                                                else if (parser.getAttributeValue(null, "name").equals("basedir")) {
229
                                                        defaultBaseDir = parser.getAttributeValue(null, "value");
230
                                                }
231
                                                else if (parser.getAttributeValue(null, "name").equals("databaseDir")) {
232
                                                        databaseDir = parser.getAttributeValue(null, "value");
233
                                                }
234
                                                else if (parser.getAttributeValue(null, "name").equals("defaultPropertyDir")) {
235
                                                        defaultPropertyDir = parser.getAttributeValue(null, "value");
236
                                                }
237
                                                else if (parser.getAttributeValue(null, "name").equals("outputDir")) {
238
                                                        outputDir = parser.getAttributeValue(null, "value");
239
                                                }
240
                                                else if (parser.getAttributeValue(null, "name").equals("inputDir")) {
241
                                                        inputDir = parser.getAttributeValue(null, "value");
242
                                                }
243
                                                else if (parser.getAttributeValue(null, "name").equals("sourceKeys")) {
244
                                                        sourceKeys = parser.getAttributeValue(null, "value");
245
                                                }
246
                                                else if (parser.getAttributeValue(null, "name").equals("srcDirs")) {
247
                                                        String srcDirs = parser.getAttributeValue(null, "value");
248
                                                        this.defaultSrcDirs = srcDirs.split(";");
249
                                                }
250
                                                else if (parser.getAttributeValue(null, "name").equals("languages")) {
251
                                                        languages = parser.getAttributeValue(null, "value").split(";");
252
                                                        if (languages.length==0) {
253
                                                                System.err.println("Aviso: No se definieron idiomas a considerar. Se usar? la lista de idiomas\n por defecto: "+defaultLangList);
254
                                                                languages = defaultLangList.split(";");
255
                                                        }
256
                                                }
257
                                        }
258
                                        else {
259
                                                if (name==null)
260
                                                        System.err.println("Error leyendo el fichero de configuraci?n. No se encontr? el atributo 'name'\nrequerido en la etiqueta <variable>. La etiqueta ser? ignorada.");
261
                                                if (value==null)
262
                                                        System.err.println("Error leyendo el fichero de configuraci?n. No se encontr? el atributo 'value'\nrequerido en la etiqueta <variable>. La etiqueta ser? ignorada.");
263
                                        }
264
                                }
265
                                else {
266
                                        System.err.println("Aviso: se ignor? una etiqueta desconocida o inesperada: " + parser.getName());
267
                                }
268
                        }
269
                }
270
                
271
                // Now we transform all directories to absolute canonical paths, so
272
                // that they are easier to manage afterwards.
273
                try {
274
                        this.defaultBaseDir = getAbsolutePath(".", this.defaultBaseDir);
275
                        this.databaseDir = getAbsolutePath(this.defaultBaseDir, this.databaseDir);
276
                        this.outputDir = getAbsolutePath(this.defaultBaseDir, this.outputDir);
277
                        this.inputDir = getAbsolutePath(this.defaultBaseDir, this.inputDir);
278
                        /**
279
                        File baseDirFile = new File(this.defaultBaseDir);
280
                        this.defaultBaseDir = baseDirFile.getCanonicalPath();
281
                        System.out.println(this.defaultBaseDir);
282
                        File databaseDirFile = new File(this.databaseDir);
283
                        if (databaseDirFile.isAbsolute()) {
284
                                this.databaseDir = databaseDirFile.getCanonicalPath();
285
                        }
286
                        else {
287
                                this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath();
288
                        }
289
                        File outputDirFile = new File(this.outputDir);
290
                        if (outputDirFile.isAbsolute()) {
291
                                this.outputDir = outputDirFile.getCanonicalPath();
292
                        }
293
                        else {
294
                                this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath();
295
                        }
296
                        File inputDirFile = new File(this.inputDir);
297
                        if (inputDirFile.isAbsolute()) {
298
                                this.inputDir = inputDirFile.getCanonicalPath();
299
                        }
300
                        else {
301
                                this.inputDir = (new File(this.defaultBaseDir+File.separator+this.inputDir)).getCanonicalPath();
302
                        }
303
                        */
304
                } catch (IOException e) {
305
                        System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage());
306
                }
307
        }
308
        
309
        /**
310
         * Parse the lines containing <project /> tags (between <projects> and </projects>).
311
         * 
312
         * @param parser The KXmlParser, pointing to the next <project /> tag (if any)
313
         * @throws XmlPullParserException
314
         * @throws IOException
315
         */
316
        private void parseProjects(KXmlParser parser) throws XmlPullParserException, IOException {
317
                // recorremos todos los proyectos dentro de 'projects'
318
                int state;
319
                String dir;
320
                File dirFile;
321
                Project project;
322
                
323
                for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("projects") ; state=parser.next()) {
324
                        if (state==KXmlParser.START_TAG) {
325
                                if (parser.getName().equals("project")) {
326
                                        if (parser.getAttributeValue(null, "dir")!=null) {
327
                                                dir = parser.getAttributeValue(null,  "dir");
328
                                                if (dir!=null) {
329
                                                        // we transform it to absolute canonical paths, so
330
                                                        // that it is easier to manage afterwards.
331
                                                        dirFile = new File(dir);
332
                                                        try {
333
                                                                if (dirFile.isAbsolute()) {
334
                                                                        dir = dirFile.getCanonicalPath();
335
                                                                }
336
                                                                else {
337
                                                                        dir = new File(this.defaultBaseDir+File.separator+dir).getCanonicalPath();
338
                                                                }
339
                                                        } catch (IOException e) {
340
                                                                System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage());
341
                                                        }
342
                                                        project = new Project();
343
                                                        project.dir = dir;
344
                                                        project.basename = parser.getAttributeValue(null, "basename");
345
                                                        if (project.basename==null)
346
                                                                project.basename = this.defaultBaseName;
347
                                                        
348
                                                        project.propertyDir = parser.getAttributeValue(null, "propertyDir");
349
                                                        if (project.propertyDir==null) {
350
                                                                project.propertyDir = this.defaultPropertyDir;
351
                                                        }
352
                                                        // we transform it to absolute canonical paths, so
353
                                                        // that it is easier to manage afterwards.
354
                                                        File propDirFile = new File(project.propertyDir);
355
                                                        try {
356
                                                                if (propDirFile.isAbsolute()) {
357
                                                                        project.propertyDir = propDirFile.getCanonicalPath();
358
                                                                }
359
                                                                else {
360
                                                                        project.propertyDir = new File(dir+File.separator+project.propertyDir).getCanonicalPath();
361
                                                                }
362
                                                        } catch (IOException e) {
363
                                                                System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage());
364
                                                        }
365

    
366
                                                        String srcDirs = parser.getAttributeValue(null, "srcDirs");
367
                                                        if (srcDirs!=null) {
368
                                                                project.srcDirs = srcDirs.split(";");
369
                                                        }
370
                                                        else {
371
                                                                project.srcDirs = this.defaultSrcDirs;
372
                                                        }
373

    
374
                                                        project.sourceKeys = parser.getAttributeValue(null, "sourceKeys");
375
                                                        if (project.sourceKeys==null)
376
                                                                project.sourceKeys = this.sourceKeys;
377
                                                        projects.add(project);
378
                                                }
379
                                                else
380
                                                        System.err.println("Error leyendo el fichero de configuraci?n. No se encontr? el atributo 'dir'\nrequerido en la etiqueta <project>. La etiqueta ser? ignorada.");
381
                                        }
382
                                }
383
                                else {
384
                                        System.err.println("Aviso: se ignorar? una etiqueta desconocida o inesperada: " + parser.getName());
385
                                }
386
                        }
387
                }
388

    
389
        }
390
        
391
        public void setLanguages(String[] languages) {
392
                this.languages = languages; 
393
        }
394
        
395
        public void setProjects(ArrayList projectList) {
396
                this.projects = projectList;
397
        }
398
        
399
        /**
400
         * Calculates the canonical path for the given path.
401
         * If the given path is relative, it is calculated from
402
         * the given baseDir.
403
         * The 'path' parameter uses the '/' character to as path
404
         * separator. The returned value uses the default system
405
         * separator as path separator.  
406
         * 
407
         * @param baseDir
408
         * @param path
409
         * @return
410
         * @throws IOException 
411
         */
412
        public static String getAbsolutePath(String baseDir, String path) throws IOException {
413
                if ('/'!=File.separatorChar)
414
                        path = path.replace('/', File.separatorChar);
415
                File pathFile = new File(path);
416
                if (pathFile.isAbsolute())
417
                        path = pathFile.getCanonicalPath();
418
                else {
419
                        File newFile = new File(baseDir+File.separator+path);
420
                        path = newFile.getAbsolutePath();
421
                }
422
                return path;
423
        }
424
}