Statistics
| Revision:

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

History | View | Annotate | Download (14.3 KB)

1
package org.gvsig.i18n.utils;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.util.ArrayList;
8

    
9
import org.kxml2.io.KXmlParser;
10
import org.xmlpull.v1.XmlPullParserException;
11

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

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

    
343
                                                        String srcDirs = parser.getAttributeValue(null, "srcDirs");
344
                                                        if (srcDirs!=null) {
345
                                                                project.srcDirs = srcDirs.split(";");
346
                                                        }
347
                                                        else {
348
                                                                project.srcDirs = this.defaultSrcDirs;
349
                                                        }
350

    
351
                                                        project.sourceKeys = parser.getAttributeValue(null, "sourceKeys");
352
                                                        if (project.sourceKeys==null)
353
                                                                project.sourceKeys = this.sourceKeys;
354
                                                        projects.add(project);
355
                                                }
356
                                                else
357
                                                        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.");
358
                                        }
359
                                }
360
                                else {
361
                                        System.err.println("Aviso: se ignorar? una etiqueta desconocida o inesperada: " + parser.getName());
362
                                }
363
                        }
364
                }
365

    
366
        }
367
        
368
        public void setLanguages(String[] languages) {
369
                this.languages = languages; 
370
        }
371
        
372
        public void setProjects(ArrayList projectList) {
373
                this.projects = projectList;
374
        }
375
        
376
        /**
377
         * Calculates the canonical path for the given path.
378
         * If the given path is relative, it is calculated from
379
         * the given baseDir.
380
         * The 'path' parameter uses the '/' character to as path
381
         * separator. The returned value uses the default system
382
         * separator as path separator.  
383
         * 
384
         * @param baseDir
385
         * @param path
386
         * @return
387
         * @throws IOException 
388
         */
389
        public static String getAbsolutePath(String baseDir, String path) throws IOException {
390
                if ('/'!=File.separatorChar)
391
                        path = path.replace('/', File.separatorChar);
392
                File pathFile = new File(path);
393
                if (pathFile.isAbsolute())
394
                        path = pathFile.getCanonicalPath();
395
                else {
396
                        File newFile = new File(baseDir+File.separator+path);
397
                        path = newFile.getAbsolutePath();
398
                }
399
                return path;
400
        }
401
}