root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / GPEXmlParser.java @ 11265
History | View | Annotate | Download (5.9 KB)
1 |
package org.gvsig.gpe.xml; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.lang.reflect.InvocationTargetException; |
8 |
|
9 |
import org.gvsig.gpe.GPEParser; |
10 |
import org.xmlpull.v1.XmlPullParser; |
11 |
import org.xmlpull.v1.XmlPullParserException; |
12 |
|
13 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
14 |
*
|
15 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
16 |
*
|
17 |
* This program is free software; you can redistribute it and/or
|
18 |
* modify it under the terms of the GNU General Public License
|
19 |
* as published by the Free Software Foundation; either version 2
|
20 |
* of the License, or (at your option) any later version.
|
21 |
*
|
22 |
* This program is distributed in the hope that it will be useful,
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25 |
* GNU General Public License for more details.
|
26 |
*
|
27 |
* You should have received a copy of the GNU General Public License
|
28 |
* along with this program; if not, write to the Free Software
|
29 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
30 |
*
|
31 |
* For more information, contact:
|
32 |
*
|
33 |
* Generalitat Valenciana
|
34 |
* Conselleria d'Infraestructures i Transport
|
35 |
* Av. Blasco Ib??ez, 50
|
36 |
* 46010 VALENCIA
|
37 |
* SPAIN
|
38 |
*
|
39 |
* +34 963862235
|
40 |
* gvsig@gva.es
|
41 |
* www.gvsig.gva.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* IVER T.I. S.A
|
46 |
* Salamanca 50
|
47 |
* 46005 Valencia
|
48 |
* Spain
|
49 |
*
|
50 |
* +34 963163400
|
51 |
* dac@iver.es
|
52 |
*/
|
53 |
/* CVS MESSAGES:
|
54 |
*
|
55 |
* $Id: GPEXmlParser.java 11265 2007-04-19 11:56:03Z csanchez $
|
56 |
* $Log$
|
57 |
* Revision 1.5 2007-04-19 11:56:03 csanchez
|
58 |
* Actualizacion protoripo libGPE
|
59 |
*
|
60 |
* Revision 1.4 2007/04/19 07:30:40 jorpiell
|
61 |
* Add the add methods to teh contenhandler and change the register mode
|
62 |
*
|
63 |
* Revision 1.3 2007/04/18 12:54:45 csanchez
|
64 |
* Actualizacion protoripo libGPE
|
65 |
*
|
66 |
* Revision 1.2 2007/04/12 11:47:15 jorpiell
|
67 |
* Add a getParser method
|
68 |
*
|
69 |
* Revision 1.1 2007/04/12 10:23:41 jorpiell
|
70 |
* Add some writers and the GPEXml parser
|
71 |
*
|
72 |
*
|
73 |
*/
|
74 |
/**
|
75 |
* This class can be implemented by all the classes that
|
76 |
* implements a GPE driver based on the XML format.
|
77 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
78 |
*/
|
79 |
public abstract class GPEXmlParser extends GPEParser { |
80 |
private InputStream inputStream = null; |
81 |
private XmlPullParser parser = null; |
82 |
|
83 |
public GPEXmlParser() {
|
84 |
super();
|
85 |
} |
86 |
|
87 |
/*
|
88 |
* (non-Javadoc)
|
89 |
* @see org.gvsig.gpe.GPEParser#parse(org.gvsig.gpe.GPEContentHandler, org.gvsig.gpe.GPEErrorHandler, java.io.File)
|
90 |
*/
|
91 |
public void parse(GPEContentHandler contents, GPEErrorHandler errors, File file) { |
92 |
super.parse(contents, errors, file);
|
93 |
try {
|
94 |
inputStream = createInputStream(file); |
95 |
parser = GPEXmlParserFactory.getParser(); |
96 |
parser.setInput(getInputStream(), getEncoding()); |
97 |
} // EL PARSER LANZAR? UN EVENTO AL ERRORHANDLER SEG?N EL ERROR RECOGIDO
|
98 |
catch (FileNotFoundException e) { |
99 |
System.out.println("ERROR: Fichero "+ file.getName() +" no encontrado"); |
100 |
errors.addError(e); |
101 |
} catch (IllegalArgumentException e) { |
102 |
System.out.println("ERROR: Argumento Incorrecto para KXML: "+ e.getMessage()); |
103 |
errors.addError(e); |
104 |
} catch (SecurityException e) { |
105 |
System.out.println("ERROR: Fallo de seguridad en la clase KXML: "+ e.getMessage()); |
106 |
errors.addError(e); |
107 |
} catch (InstantiationException e) { |
108 |
System.out.println("ERROR: Instancia de KXML incorrecta: "+ e.getMessage()); |
109 |
errors.addError(e); |
110 |
} catch (IllegalAccessException e) { |
111 |
System.out.println("ERROR: Acceso Ilegal a memoria de KXML: "+ e.getMessage()); |
112 |
errors.addError(e); |
113 |
} catch (InvocationTargetException e) { |
114 |
System.out.println("ERROR: Invocaci?n de KXML Incorrecta: "+ e.getMessage()); |
115 |
errors.addError(e); |
116 |
} catch (NoSuchMethodException e) { |
117 |
System.out.println("ERROR: M?todo no implementado del parser KXML: "+ e.getMessage()); |
118 |
errors.addError(e); |
119 |
} catch (ClassNotFoundException e) { |
120 |
System.out.println("ERROR: Clase KXML no encontrada: "+ e.getMessage()); |
121 |
errors.addError(e); |
122 |
} catch (XmlPullParserException e) {
|
123 |
System.out.println("ERROR: Excepci?n en el parser KXML: "+ e.getMessage()); |
124 |
errors.addError(e); |
125 |
} catch (IOException e) { |
126 |
System.out.println("ERROR: No se puede leer/escribir la codificaci?n del fichero: "+ e.getMessage()); |
127 |
errors.addError(e); |
128 |
} |
129 |
initParse(); |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Creates an input stream from a file.
|
134 |
* @param file
|
135 |
* @return InputStream
|
136 |
* @throws FileNotFoundException
|
137 |
*/
|
138 |
protected abstract InputStream createInputStream(File file) throws FileNotFoundException; |
139 |
|
140 |
/**
|
141 |
* This method start the parse process. It is called
|
142 |
* after the XML parser is initialized
|
143 |
*/
|
144 |
protected abstract void initParse(); |
145 |
|
146 |
/**
|
147 |
* @return Returns the encoding or UTF-8 encoding by default.
|
148 |
* @throws IOException
|
149 |
* @throws KmlException
|
150 |
*/
|
151 |
private String getEncoding() throws IOException { |
152 |
String encoding = "UTF-8"; |
153 |
InputStream is = getInputStream();
|
154 |
int index = -1; |
155 |
int endIndex = -1; |
156 |
String header = ""; |
157 |
boolean endHeader = false; |
158 |
|
159 |
int i = 1; |
160 |
while((!endHeader) && (i < 100)){ |
161 |
byte[] buffer = new byte[1]; |
162 |
is.read(buffer); |
163 |
header = header + new String(buffer); |
164 |
//This if is to locate the encoding string
|
165 |
if (index == -1) { |
166 |
String searchText = "encoding=\""; |
167 |
index = header.indexOf(searchText); |
168 |
if (index > -1){ |
169 |
header = "";
|
170 |
} |
171 |
} |
172 |
//This if is to locate the encoding value
|
173 |
if (index > -1){ |
174 |
String searchText = "\""; |
175 |
endIndex = header.indexOf(searchText); |
176 |
if (endIndex > -1){ |
177 |
encoding = header.substring(0, endIndex);
|
178 |
} |
179 |
} |
180 |
//To finish to parse the header
|
181 |
if (endIndex > -1){ |
182 |
String searchText = ">"; |
183 |
int headerEndIndex = header.indexOf(searchText);
|
184 |
if (headerEndIndex > -1){ |
185 |
endHeader = true;
|
186 |
} |
187 |
} |
188 |
i++; |
189 |
} |
190 |
return encoding;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* @return the inputStream
|
195 |
*/
|
196 |
private InputStream getInputStream() { |
197 |
return inputStream;
|
198 |
} |
199 |
|
200 |
/**
|
201 |
* @return the parser
|
202 |
*/
|
203 |
protected XmlPullParser getParser() {
|
204 |
return parser;
|
205 |
} |
206 |
|
207 |
} |