root / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / ConfigOptions.java @ 5868
History | View | Annotate | Download (5.52 KB)
1 |
package org.gvsig.i18n.utils; |
---|---|
2 |
|
3 |
import java.io.FileInputStream; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.util.ArrayList; |
7 |
import java.util.HashMap; |
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 |
private String configFileName = "config.txt"; |
17 |
public String languages[]; |
18 |
public ArrayList projects = new ArrayList(); |
19 |
private String defaultLangList="ca;cs;de;en;es;eu;fr;gl;it;pt"; |
20 |
|
21 |
/**
|
22 |
* Creates a new ConfigOptions object.
|
23 |
*/
|
24 |
public ConfigOptions() {
|
25 |
super();
|
26 |
} |
27 |
|
28 |
/**
|
29 |
* Creates a new ConfigOptions object, defining the config file to use.
|
30 |
*
|
31 |
* @param configFileName The file name of the config file to use.
|
32 |
*/
|
33 |
public ConfigOptions(String configFileName) { |
34 |
this.configFileName = configFileName;
|
35 |
} |
36 |
|
37 |
/**
|
38 |
* Sets the name of the config file to use.
|
39 |
*
|
40 |
* @param configFileName
|
41 |
*/
|
42 |
public void setConfigFile(String configFileName) { |
43 |
this.configFileName = configFileName;
|
44 |
} |
45 |
|
46 |
/**
|
47 |
* Gets the name of the config file in use.
|
48 |
*
|
49 |
* @return The name of the config file in use.
|
50 |
*/
|
51 |
public String getConfigFile() { |
52 |
return configFileName;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* Loads the config parameters and the projects to consider from the XML
|
57 |
* config file */
|
58 |
public boolean load() { |
59 |
KXmlParser parser = new KXmlParser();
|
60 |
|
61 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
62 |
try {
|
63 |
parser.setInput(new FileInputStream(configFileName), null); |
64 |
} catch (FileNotFoundException e1) { |
65 |
e1.getLocalizedMessage(); |
66 |
return false; |
67 |
} catch (XmlPullParserException e1) {
|
68 |
// No podemos leer el fichero de configuraci?n. Usamos valores por defecto
|
69 |
System.out.println("Aviso: no se pudo leer correctamente el fichero de configuraci?n. Se usar?n los valores por defecto."); |
70 |
return false; |
71 |
} |
72 |
|
73 |
try {
|
74 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
75 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
76 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
77 |
if (parser.getName().equals("config")) { |
78 |
parseVars(parser); |
79 |
} |
80 |
else if (parser.getName().equals("projects")) { |
81 |
parseProjects(parser); |
82 |
} |
83 |
} |
84 |
} |
85 |
} catch (XmlPullParserException e1) {
|
86 |
e1.getLocalizedMessage(); |
87 |
} catch (IOException e1) { |
88 |
e1.getLocalizedMessage(); |
89 |
} |
90 |
return true; |
91 |
} |
92 |
|
93 |
private void parseVars(KXmlParser parser) throws XmlPullParserException, IOException { |
94 |
// recorremos todas las etiquetas 'variable' dentro de config
|
95 |
int state;
|
96 |
String name, value;
|
97 |
|
98 |
for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("config") ; state=parser.next()) { |
99 |
|
100 |
if (state==KXmlParser.START_TAG) {
|
101 |
if (parser.getName().equals("variable")) { |
102 |
name = parser.getAttributeValue(null, "name"); |
103 |
value = parser.getAttributeValue(null, "value"); |
104 |
if (name!=null && value!=null) { |
105 |
value = parser.getAttributeValue(null, "value"); |
106 |
if (parser.getAttributeValue(null, "name").equals("basename")) { |
107 |
defaultBaseName = parser.getAttributeValue(null, "value"); |
108 |
} |
109 |
else if (parser.getAttributeValue(null, "name").equals("basedir")) { |
110 |
defaultBaseDir = parser.getAttributeValue(null, "value"); |
111 |
} |
112 |
else if (parser.getAttributeValue(null, "name").equals("languages")) { |
113 |
languages = parser.getAttributeValue(null, "value").split(";"); |
114 |
if (languages.length==0) { |
115 |
System.err.println("Aviso: No se definieron idiomas a considerar. Se usar? la lista de idiomas\n por defecto: "+defaultLangList); |
116 |
languages = defaultLangList.split(";");
|
117 |
} |
118 |
} |
119 |
} |
120 |
else {
|
121 |
if (name==null) |
122 |
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."); |
123 |
if (value==null) |
124 |
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."); |
125 |
} |
126 |
} |
127 |
else {
|
128 |
System.err.println("Aviso: se ignor? una etiqueta desconocida o inesperada: " + parser.getName()); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Parse the lines containing <project /> tags (between <projects> and </projects>).
|
136 |
*
|
137 |
* @param parser The KXmlParser, pointing to the next <project /> tag (if any)
|
138 |
* @throws XmlPullParserException
|
139 |
* @throws IOException
|
140 |
*/
|
141 |
private void parseProjects(KXmlParser parser) throws XmlPullParserException, IOException { |
142 |
// recorremos todos los proyectos dentro de 'projects'
|
143 |
int state;
|
144 |
String dir, basename;
|
145 |
String project[] = new String[2]; |
146 |
|
147 |
for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("projects") ; state=parser.next()) { |
148 |
if (state==KXmlParser.START_TAG) {
|
149 |
if (parser.getName().equals("project")) { |
150 |
if (parser.getAttributeValue(null, "dir")!=null) { |
151 |
dir = parser.getAttributeValue(null, "dir"); |
152 |
if (dir!=null) { |
153 |
project[0] = dir;
|
154 |
project[1] = parser.getAttributeValue(null, "basename"); |
155 |
if (project[1]==null) |
156 |
project[1] = defaultBaseName;
|
157 |
projects.add(project); |
158 |
} |
159 |
else
|
160 |
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."); |
161 |
} |
162 |
} |
163 |
else {
|
164 |
System.err.println("Aviso: se ignorar? una etiqueta desconocida o inesperada: " + parser.getName()); |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
} |
170 |
} |