svn-gvsig-desktop / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / ConfigOptions.java @ 6232
History | View | Annotate | Download (11.1 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 |
import java.util.HashMap; |
9 |
|
10 |
import org.kxml2.io.KXmlParser; |
11 |
import org.xmlpull.v1.XmlPullParserException; |
12 |
|
13 |
public class ConfigOptions { |
14 |
// Default values
|
15 |
public String defaultBaseName = "text"; |
16 |
public String defaultBaseDir = "."; |
17 |
public String databaseDir = "database"; |
18 |
private String configFileName = "config.xml"; |
19 |
public String[] languages; |
20 |
public ArrayList projects = new ArrayList(); |
21 |
private String defaultLangList="ca;cs;de;en;es;eu;fr;gl;it;pt"; |
22 |
public String sourceKeys = "sources"; |
23 |
private String defaultPropertyDir = "config"; |
24 |
public String[] outputLanguages={"en", "es"}; |
25 |
public String outputDir="output"; |
26 |
|
27 |
/**
|
28 |
* The character encoding of the Java source files, used to search keys in the sources.
|
29 |
*/
|
30 |
public String sourcesEncoding = "ISO-8859-15"; |
31 |
/**
|
32 |
* The character encoding of the generated output files for missing keys.
|
33 |
*/
|
34 |
public String outputEncoding = "UTF8"; |
35 |
|
36 |
/**
|
37 |
* Creates a new ConfigOptions object.
|
38 |
*/
|
39 |
public ConfigOptions() {
|
40 |
// Now we transform all directories to absolute canonical paths, so
|
41 |
// that the are easier to manage afterwards.
|
42 |
// It's also done at the end of parseVars method, but we must also do
|
43 |
// it here, because parseVars() might not get executed.
|
44 |
try {
|
45 |
File baseDirFile = new File(this.defaultBaseDir); |
46 |
this.defaultBaseDir = baseDirFile.getCanonicalPath();
|
47 |
File databaseDirFile = new File(this.databaseDir); |
48 |
if (databaseDirFile.isAbsolute()) {
|
49 |
this.databaseDir = databaseDirFile.getCanonicalPath();
|
50 |
} |
51 |
else {
|
52 |
this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath(); |
53 |
} |
54 |
File outputDirFile = new File(this.outputDir); |
55 |
if (outputDirFile.isAbsolute()) {
|
56 |
this.outputDir = outputDirFile.getCanonicalPath();
|
57 |
} |
58 |
else {
|
59 |
this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath(); |
60 |
} |
61 |
} catch (IOException e) { |
62 |
System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage()); |
63 |
} |
64 |
} |
65 |
|
66 |
/**
|
67 |
* Creates a new ConfigOptions object, defining the config file to use.
|
68 |
*
|
69 |
* @param configFileName The file name of the config file to use.
|
70 |
*/
|
71 |
public ConfigOptions(String configFileName) { |
72 |
this.configFileName = configFileName;
|
73 |
System.out.println(configFileName);
|
74 |
|
75 |
// Now we transform all directories to absolute canonical paths, so
|
76 |
// that the are easier to manage afterwards.
|
77 |
// It's also done at the end of parseVars method, but we must also do
|
78 |
// it here, because parseVars() might not get executed.
|
79 |
try {
|
80 |
File baseDirFile = new File(this.defaultBaseDir); |
81 |
this.defaultBaseDir = baseDirFile.getCanonicalPath();
|
82 |
File databaseDirFile = new File(this.databaseDir); |
83 |
if (databaseDirFile.isAbsolute()) {
|
84 |
this.databaseDir = databaseDirFile.getCanonicalPath();
|
85 |
} |
86 |
else {
|
87 |
this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath(); |
88 |
} |
89 |
File outputDirFile = new File(this.outputDir); |
90 |
if (outputDirFile.isAbsolute()) {
|
91 |
this.outputDir = outputDirFile.getCanonicalPath();
|
92 |
} |
93 |
else {
|
94 |
this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath(); |
95 |
} |
96 |
} catch (IOException e) { |
97 |
System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage()); |
98 |
} |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Sets the name of the config file to use.
|
103 |
*
|
104 |
* @param configFileName
|
105 |
*/
|
106 |
public void setConfigFile(String configFileName) { |
107 |
this.configFileName = configFileName;
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* Gets the name of the config file in use.
|
112 |
*
|
113 |
* @return The name of the config file in use.
|
114 |
*/
|
115 |
public String getConfigFile() { |
116 |
return configFileName;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Loads the config parameters and the projects to consider from the XML
|
121 |
* config file */
|
122 |
public boolean load() { |
123 |
KXmlParser parser = new KXmlParser();
|
124 |
|
125 |
// we use null encoding, in this way kxml2 tries to detect the encoding
|
126 |
try {
|
127 |
parser.setInput(new FileInputStream(configFileName), null); |
128 |
} catch (FileNotFoundException e1) { |
129 |
System.err.println(e1.getLocalizedMessage());
|
130 |
return false; |
131 |
} catch (XmlPullParserException e1) {
|
132 |
// No podemos leer el fichero de configuraci?n. Usamos valores por defecto
|
133 |
System.out.println("Aviso: no se pudo leer correctamente el fichero de configuraci?n. Se usar?n los valores por defecto."); |
134 |
return false; |
135 |
} |
136 |
|
137 |
try {
|
138 |
for (parser.next(); parser.getEventType()!=KXmlParser.END_DOCUMENT; parser.next()) {
|
139 |
// este bucle externo recorre las etiquetas de primer y segundo nivel
|
140 |
if (parser.getEventType()==KXmlParser.START_TAG) {
|
141 |
if (parser.getName().equals("config")) { |
142 |
parseVars(parser); |
143 |
} |
144 |
else if (parser.getName().equals("projects")) { |
145 |
parseProjects(parser); |
146 |
} |
147 |
} |
148 |
} |
149 |
} catch (XmlPullParserException e1) {
|
150 |
e1.getLocalizedMessage(); |
151 |
} catch (IOException e1) { |
152 |
e1.getLocalizedMessage(); |
153 |
} |
154 |
return true; |
155 |
} |
156 |
|
157 |
private void parseVars(KXmlParser parser) throws XmlPullParserException, IOException { |
158 |
// recorremos todas las etiquetas 'variable' dentro de config
|
159 |
int state;
|
160 |
String name, value;
|
161 |
|
162 |
for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("config") ; state=parser.next()) { |
163 |
if (state==KXmlParser.START_TAG) {
|
164 |
if (parser.getName().equals("variable")) { |
165 |
name = parser.getAttributeValue(null, "name"); |
166 |
value = parser.getAttributeValue(null, "value"); |
167 |
if (name!=null && value!=null) { |
168 |
value = parser.getAttributeValue(null, "value"); |
169 |
if (parser.getAttributeValue(null, "name").equals("basename")) { |
170 |
defaultBaseName = parser.getAttributeValue(null, "value"); |
171 |
} |
172 |
else if (parser.getAttributeValue(null, "name").equals("basedir")) { |
173 |
defaultBaseDir = parser.getAttributeValue(null, "value"); |
174 |
} |
175 |
else if (parser.getAttributeValue(null, "name").equals("databaseDir")) { |
176 |
databaseDir = parser.getAttributeValue(null, "value"); |
177 |
} |
178 |
else if (parser.getAttributeValue(null, "name").equals("defaultPropertyDir")) { |
179 |
defaultPropertyDir = parser.getAttributeValue(null, "value"); |
180 |
} |
181 |
else if (parser.getAttributeValue(null, "name").equals("sourceKeys")) { |
182 |
sourceKeys = parser.getAttributeValue(null, "value"); |
183 |
} |
184 |
else if (parser.getAttributeValue(null, "name").equals("languages")) { |
185 |
languages = parser.getAttributeValue(null, "value").split(";"); |
186 |
if (languages.length==0) { |
187 |
System.err.println("Aviso: No se definieron idiomas a considerar. Se usar? la lista de idiomas\n por defecto: "+defaultLangList); |
188 |
languages = defaultLangList.split(";");
|
189 |
} |
190 |
} |
191 |
} |
192 |
else {
|
193 |
if (name==null) |
194 |
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."); |
195 |
if (value==null) |
196 |
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."); |
197 |
} |
198 |
} |
199 |
else {
|
200 |
System.err.println("Aviso: se ignor? una etiqueta desconocida o inesperada: " + parser.getName()); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
// Now we transform all directories to absolute canonical paths, so
|
206 |
// that they are easier to manage afterwards.
|
207 |
try {
|
208 |
File baseDirFile = new File(this.defaultBaseDir); |
209 |
this.defaultBaseDir = baseDirFile.getCanonicalPath();
|
210 |
System.out.println(this.defaultBaseDir); |
211 |
File databaseDirFile = new File(this.databaseDir); |
212 |
if (databaseDirFile.isAbsolute()) {
|
213 |
this.databaseDir = databaseDirFile.getCanonicalPath();
|
214 |
} |
215 |
else {
|
216 |
this.databaseDir = (new File(this.defaultBaseDir+File.separator+this.databaseDir)).getCanonicalPath(); |
217 |
} |
218 |
File outputDirFile = new File(this.outputDir); |
219 |
if (outputDirFile.isAbsolute()) {
|
220 |
this.outputDir = outputDirFile.getCanonicalPath();
|
221 |
} |
222 |
else {
|
223 |
this.outputDir = (new File(this.defaultBaseDir+File.separator+this.outputDir)).getCanonicalPath(); |
224 |
} |
225 |
} catch (IOException e) { |
226 |
System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage()); |
227 |
} |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Parse the lines containing <project /> tags (between <projects> and </projects>).
|
232 |
*
|
233 |
* @param parser The KXmlParser, pointing to the next <project /> tag (if any)
|
234 |
* @throws XmlPullParserException
|
235 |
* @throws IOException
|
236 |
*/
|
237 |
private void parseProjects(KXmlParser parser) throws XmlPullParserException, IOException { |
238 |
// recorremos todos los proyectos dentro de 'projects'
|
239 |
int state;
|
240 |
String dir;
|
241 |
File dirFile;
|
242 |
Project project; |
243 |
|
244 |
for (state = parser.next(); state!=KXmlParser.END_TAG || !parser.getName().equals("projects") ; state=parser.next()) { |
245 |
if (state==KXmlParser.START_TAG) {
|
246 |
if (parser.getName().equals("project")) { |
247 |
if (parser.getAttributeValue(null, "dir")!=null) { |
248 |
dir = parser.getAttributeValue(null, "dir"); |
249 |
if (dir!=null) { |
250 |
// we transform it to absolute canonical paths, so
|
251 |
// that it is easier to manage afterwards.
|
252 |
dirFile = new File(dir); |
253 |
try {
|
254 |
if (dirFile.isAbsolute()) {
|
255 |
dir = dirFile.getCanonicalPath(); |
256 |
} |
257 |
else {
|
258 |
dir = new File(this.defaultBaseDir+File.separator+dir).getCanonicalPath(); |
259 |
} |
260 |
} catch (IOException e) { |
261 |
System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage()); |
262 |
} |
263 |
project = new Project();
|
264 |
project.dir = dir; |
265 |
project.basename = parser.getAttributeValue(null, "basename"); |
266 |
if (project.basename==null) |
267 |
project.basename = this.defaultBaseName;
|
268 |
|
269 |
project.propertyDir = parser.getAttributeValue(null, "propertyDir"); |
270 |
if (project.propertyDir==null) { |
271 |
project.propertyDir = this.defaultPropertyDir;
|
272 |
} |
273 |
// we transform it to absolute canonical paths, so
|
274 |
// that it is easier to manage afterwards.
|
275 |
File propDirFile = new File(project.propertyDir); |
276 |
try {
|
277 |
if (propDirFile.isAbsolute()) {
|
278 |
project.propertyDir = propDirFile.getCanonicalPath(); |
279 |
} |
280 |
else {
|
281 |
project.propertyDir = new File(dir+File.separator+project.propertyDir).getCanonicalPath(); |
282 |
} |
283 |
} catch (IOException e) { |
284 |
System.err.println("Error accediendo a los directorios de las traducciones: "+e.getLocalizedMessage()); |
285 |
} |
286 |
|
287 |
project.sourceKeys = parser.getAttributeValue(null, "sourceKeys"); |
288 |
if (project.sourceKeys==null) |
289 |
project.sourceKeys = this.sourceKeys;
|
290 |
projects.add(project); |
291 |
} |
292 |
else
|
293 |
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."); |
294 |
} |
295 |
} |
296 |
else {
|
297 |
System.err.println("Aviso: se ignorar? una etiqueta desconocida o inesperada: " + parser.getName()); |
298 |
} |
299 |
} |
300 |
} |
301 |
|
302 |
} |
303 |
|
304 |
public void setLanguages(String[] languages) { |
305 |
this.languages = languages;
|
306 |
} |
307 |
|
308 |
public void setProjects(ArrayList projectList) { |
309 |
this.projects = projectList;
|
310 |
} |
311 |
} |