Revision 11265 trunk/libraries/libGPE-GML/src/org/gvsig/gpe/gml/GPEGmlParser.java
GPEGmlParser.java | ||
---|---|---|
1 | 1 |
package org.gvsig.gpe.gml; |
2 |
|
|
3 | 2 |
import java.io.File; |
4 | 3 |
import java.io.FileInputStream; |
5 | 4 |
import java.io.FileNotFoundException; |
6 | 5 |
import java.io.IOException; |
7 | 6 |
import java.io.InputStream; |
7 |
import java.net.ConnectException; |
|
8 |
import java.net.MalformedURLException; |
|
9 |
import java.net.URL; |
|
10 |
import java.net.UnknownHostException; |
|
11 |
import java.util.StringTokenizer; |
|
8 | 12 |
|
9 |
import org.gvsig.gpe.GPEContentHandler; |
|
10 |
import org.gvsig.gpe.GPEErrorHandler; |
|
13 |
import org.gvsig.exceptions.BaseException; |
|
14 |
import org.gvsig.gpe.gml.factories.XMLSchemasFactory; |
|
15 |
import org.gvsig.gpe.gml.utils.Utilities; |
|
11 | 16 |
import org.gvsig.gpe.gml.writer.GPEGmlWriterHandler; |
12 | 17 |
import org.gvsig.gpe.writers.GPEWriterHandler; |
13 | 18 |
import org.gvsig.gpe.xml.GPEXmlParser; |
19 |
import org.xml.sax.SAXException; |
|
20 |
import org.xmlpull.v1.XmlPullParser; |
|
14 | 21 |
import org.xmlpull.v1.XmlPullParserException; |
15 | 22 |
|
23 |
import com.sun.xml.xsom.parser.XSOMParser; |
|
24 |
|
|
16 | 25 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
17 | 26 |
* |
18 | 27 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
... | ... | |
57 | 66 |
* |
58 | 67 |
* $Id$ |
59 | 68 |
* $Log$ |
60 |
* Revision 1.3 2007-04-18 12:57:13 csanchez
|
|
69 |
* Revision 1.4 2007-04-19 11:51:43 csanchez
|
|
61 | 70 |
* Actualizacion protoripo libGPE |
62 | 71 |
* |
63 |
* Revision 1.2 2007/04/18 12:54:45 csanchez
|
|
72 |
* Revision 1.1 2007/04/18 12:54:45 csanchez
|
|
64 | 73 |
* Actualizacion protoripo libGPE |
65 | 74 |
* |
66 |
* Revision 1.1 2007/04/14 16:07:30 jorpiell |
|
67 |
* The writer has been created |
|
68 | 75 |
* |
69 |
* |
|
70 | 76 |
*/ |
71 | 77 |
/** |
72 |
* Esta clase ha sido creada solo por necesidad. Debe |
|
73 |
* ser sustituida por la de Carlos |
|
74 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es) |
|
78 |
* @author Carlos S?nchez Peri??n (sanchez_carper@gva.es) |
|
75 | 79 |
*/ |
76 |
public class GPEGmlParser extends GPEXmlParser {
|
|
80 |
public class GPEGmlParser extends GPEXmlParser{ |
|
77 | 81 |
|
78 |
public GPEGmlParser(GPEContentHandler contents, GPEErrorHandler errors) { |
|
79 |
super(contents, errors); |
|
82 |
private XSOMParser schemaParser = null; |
|
83 |
private XmlPullParser fileParser = null; |
|
84 |
private String mainTag = null; |
|
85 |
private File mainFile = null; |
|
86 |
private File schemaFile = null; |
|
87 |
private int no_schema = 1; |
|
88 |
|
|
89 |
public GPEGmlParser() { |
|
90 |
//First, it gets the file parser. |
|
91 |
fileParser = getParser(); |
|
92 |
// TODO Ap?ndice de constructor generado autom?ticamente |
|
80 | 93 |
} |
81 |
|
|
94 |
|
|
82 | 95 |
/* |
83 | 96 |
* (non-Javadoc) |
84 |
* @see org.gvsig.gpe.xml.GPEXmlParser#createInputStream(java.io.File)
|
|
97 |
* @see org.gvsig.gpe.GPEParser#getFormats()
|
|
85 | 98 |
*/ |
99 |
public String[] getFormats() { |
|
100 |
String[] formats = new String[2]; |
|
101 |
formats[0] = "GML"; |
|
102 |
formats[1] = "XML"; |
|
103 |
return formats; |
|
104 |
} |
|
105 |
|
|
106 |
public boolean accept(File file) { |
|
107 |
this.mainFile = file; |
|
108 |
if ((file.getName().toUpperCase().endsWith("GML")) |
|
109 |
|| (file.getName().toUpperCase().endsWith("XML"))) { |
|
110 |
return true; |
|
111 |
} |
|
112 |
return false; |
|
113 |
} |
|
114 |
|
|
86 | 115 |
protected InputStream createInputStream(File file) throws FileNotFoundException { |
116 |
// TODO Ap?ndice de m?todo generado autom?ticamente |
|
87 | 117 |
return new FileInputStream(file); |
88 | 118 |
} |
89 |
|
|
119 |
/** |
|
120 |
* |
|
121 |
* AQUI SE IMPLEMENTA LA BASE PARA PODER PARSEAR CUALQUIER FICHERO GML: |
|
122 |
* -PARTIMOS DE QUE TENEMOS EL FICHERO ABIERTO EN MODO LECTURA Y UN FILE PARSER |
|
123 |
* - |
|
124 |
* -ABRIR EN MODO LECTURA CON LA CODIFICACI?N CORRECTA |
|
125 |
* -CREAR EL PARSER DE XML ?"XSOM"? |
|
126 |
* -DETECTAR FORMATO FICHERO Y VERSI?N ?"EST? REGISTRADO"? |
|
127 |
* -SI EXISTE, LLAMADA AL PARSER DEL FORMATO CONCRETO |
|
128 |
* |
|
129 |
**/ |
|
130 |
protected void initParse() { |
|
131 |
//First of all it has to parse the head |
|
132 |
parseHead(); |
|
133 |
} |
|
90 | 134 |
/* |
91 | 135 |
* (non-Javadoc) |
92 |
* @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
|
|
136 |
* @see org.gvsig.gpe.GPEParser#getWriter(java.lang.String)
|
|
93 | 137 |
*/ |
94 |
protected void initParse() { |
|
138 |
public GPEWriterHandler getWriter(String format) { |
|
139 |
return new GPEGmlWriterHandler(); |
|
140 |
} |
|
141 |
|
|
142 |
public String[] getVersions() { |
|
143 |
// TODO Ap?ndice de m?todo generado autom?ticamente |
|
144 |
return null; |
|
145 |
} |
|
146 |
private void parseHead(){ |
|
147 |
//nextTag() --> Method from KXML library to get next full tag |
|
95 | 148 |
try { |
96 |
parser.next();
|
|
149 |
fileParser.nextTag();
|
|
97 | 150 |
} catch (XmlPullParserException e) { |
98 | 151 |
// TODO Bloque catch generado autom?ticamente |
99 |
e.printStackTrace(); |
|
152 |
System.out.println("Error en XmlPullParser al intentar obtener la siguiente etiqueta: "+e.getMessage()); |
|
153 |
|
|
100 | 154 |
} catch (IOException e) { |
101 | 155 |
// TODO Bloque catch generado autom?ticamente |
102 |
e.printStackTrace(); |
|
156 |
System.out.println("Error al leer la siguiente etiqueta en XmlPullParser: "+e.getMessage()); |
|
157 |
|
|
103 | 158 |
} |
104 |
// TODO Auto-generated method stub |
|
159 |
//It keeps the name of the start tag to compare with the close tag later |
|
160 |
mainTag = fileParser.getName(); |
|
161 |
//If it has namespace before the ":" we keep the maintag without namespace |
|
162 |
int pos = mainTag.indexOf(":"); |
|
163 |
if (pos > 0){ |
|
164 |
mainTag = mainTag.substring(mainTag.indexOf(":") + 1,mainTag.length()); |
|
165 |
} |
|
166 |
//It start to get all the attributes and values from the header tag |
|
167 |
for (int i = 0 ; i < fileParser.getAttributeCount() ; i++){ |
|
168 |
String attName = fileParser.getAttributeName(i); |
|
169 |
String attValue = fileParser.getAttributeValue(i); |
|
170 |
|
|
171 |
|
|
172 |
//it splits the attributes names at the both sides from ":" |
|
173 |
String[] ns = attName.split(":"); |
|
174 |
|
|
175 |
//If it founds the 'xmlns' is a new namespace declaration and it has to parse it |
|
176 |
if ((ns.length>1) && (ns[0].compareTo(GMLTags.XML_NAMESPACE)==0)){ |
|
177 |
parseNameSpace(ns[1],attValue); |
|
178 |
} |
|
179 |
|
|
180 |
//If its the "SCHEMA LOCATION" attribute, it means that there are schema and it tries to parse it |
|
181 |
if ((ns.length>1) && (ns[1].compareTo(GMLTags.XML_SCHEMA_LOCATION)==0)){ |
|
182 |
no_schema=0; |
|
183 |
parseSchemaLocation(ns[0],attValue); |
|
184 |
} |
|
185 |
} |
|
186 |
if (no_schema==1){ |
|
187 |
//Alert that th GML File hasn't schema but it tries to parse |
|
188 |
//warnings.setElement(new GMLWarningNoSchema()); |
|
189 |
} |
|
105 | 190 |
} |
106 |
|
|
107 |
/*
|
|
108 |
* (non-Javadoc)
|
|
109 |
* @see org.gvsig.gpe.GPEParser#accept(java.io.File)
|
|
191 |
/** |
|
192 |
* It downloads the schema's file and parse it
|
|
193 |
* @param xmlnsName : Alias
|
|
194 |
* @param xmlnsValue: URI
|
|
110 | 195 |
*/ |
111 |
public boolean accept(File file) { |
|
112 |
if ((file.getName().toUpperCase().endsWith("GML")) |
|
113 |
|| (file.getName().toUpperCase().endsWith("XML"))) { |
|
114 |
return true; |
|
115 |
} |
|
116 |
return false; |
|
196 |
private void parseSchemaLocation(String schemaAlias, String schemaURI){ |
|
197 |
//It take the name of the schemas file to open or downlad |
|
198 |
StringTokenizer tokenizer = new StringTokenizer(schemaURI, " \t"); |
|
199 |
while (tokenizer.hasMoreTokens()){ |
|
200 |
String URI = tokenizer.nextToken(); |
|
201 |
if (!tokenizer.hasMoreTokens()){ |
|
202 |
//If it hasn't the name of the schemas file or dont find it, |
|
203 |
//it exits, and tries to parse without schema |
|
204 |
//warnings.setElement(new GMLWarningNotFound(null,null)); |
|
205 |
System.out.println("Error, esquema no encontrado.PARSEO SIN ESQUEMA "); |
|
206 |
|
|
207 |
} |
|
208 |
else |
|
209 |
{ |
|
210 |
String schemaLocation = tokenizer.nextToken(); |
|
211 |
//It add the schemaLocation to the hashtable |
|
212 |
try { |
|
213 |
XMLSchemasFactory.addSchemaLocation(schemaAlias,URI,schemaLocation); |
|
214 |
} |
|
215 |
catch (BaseException e) { |
|
216 |
// TODO Auto-generated catch block |
|
217 |
//warnings.setElement(new GMLWarningMalformed()); |
|
218 |
System.out.println("Error al a?adir el esquema o esquema mal formado: "+e.getMessage()); |
|
219 |
} |
|
220 |
//It downloads the schema if it's a remote schema |
|
221 |
schemaFile = getSchemaFile(schemaLocation); |
|
222 |
//It parses the schema. |
|
223 |
schemaParser = new XSOMParser(); |
|
224 |
try { |
|
225 |
schemaParser.parse(schemaFile); |
|
226 |
} catch (SAXException e) { |
|
227 |
// TODO Bloque catch generado autom?ticamente |
|
228 |
System.out.println("Error al parsear el Schema: "+e.getMessage()); |
|
229 |
} catch (IOException e) { |
|
230 |
// TODO Bloque catch generado autom?ticamente |
|
231 |
System.out.println("Error de lectura/escritura del esquema: "+e.getMessage()); |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
117 | 235 |
} |
118 |
|
|
119 |
/*
|
|
120 |
* (non-Javadoc)
|
|
121 |
* @see org.gvsig.gpe.GPEParser#getFormats()
|
|
236 |
/** |
|
237 |
* It adds an XML namespace tag to the hashtable
|
|
238 |
* @param xmlnsName : Namespace
|
|
239 |
* @param xmlnsValue: URI
|
|
122 | 240 |
*/ |
123 |
public String[] getFormats() { |
|
124 |
String[] formats = new String[2]; |
|
125 |
formats[0] = "GML"; |
|
126 |
formats[1] = "XML"; |
|
127 |
return formats; |
|
241 |
private void parseNameSpace(String xmlnsName,String xmlnsValue){ |
|
242 |
XMLSchemasFactory.addType(xmlnsName,xmlnsValue); |
|
128 | 243 |
} |
129 |
|
|
130 |
/*
|
|
131 |
* (non-Javadoc)
|
|
132 |
* @see org.gvsig.gpe.GPEParser#getWriter(java.lang.String)
|
|
244 |
/** |
|
245 |
* It downloads the schema if it's a remote schema
|
|
246 |
* else it tries to open a local file and return if it's succesfull
|
|
247 |
* @param String : schemaLocation
|
|
133 | 248 |
*/ |
134 |
public GPEWriterHandler getWriter(String format) { |
|
135 |
return new GPEGmlWriterHandler(format, getErrorHandler()); |
|
249 |
private File getSchemaFile(String schemaLocation){ |
|
250 |
File f = null; |
|
251 |
|
|
252 |
//If it is a local file, it has to construct the absolute route |
|
253 |
if (schemaLocation.indexOf("http://") != 0){ |
|
254 |
f = new File(schemaLocation); |
|
255 |
if (!(f.isAbsolute())){ |
|
256 |
schemaLocation = mainFile.getParentFile().getAbsolutePath() + File.separator + schemaLocation; |
|
257 |
f = new File(schemaLocation); |
|
258 |
} |
|
259 |
return f; |
|
260 |
} |
|
261 |
//Else it is an URL direction and it has to download it. |
|
262 |
else { |
|
263 |
URL url; |
|
264 |
|
|
265 |
try { |
|
266 |
url = new URL(schemaLocation); |
|
267 |
//Download the schema without cancel option. |
|
268 |
f = Utilities.downloadFile(url,"gml_schmema.xsd"); |
|
269 |
} catch (MalformedURLException e) { |
|
270 |
// TODO Bloque catch generado autom?ticamente |
|
271 |
System.out.println("Error, URL del esquema mal formada: "+e.getMessage()); |
|
272 |
} catch (ConnectException e) { |
|
273 |
// TODO Bloque catch generado autom?ticamente |
|
274 |
System.out.println("Error al descargar esquema, Imposible conectar: "+e.getMessage()); |
|
275 |
} catch (UnknownHostException e) { |
|
276 |
// TODO Bloque catch generado autom?ticamente |
|
277 |
System.out.println("Error al descargar esquema, Host desconocido: "+e.getMessage()); |
|
278 |
} catch (IOException e) { |
|
279 |
// TODO Bloque catch generado autom?ticamente |
|
280 |
System.out.println("Erroral descargar esquema, Fallo de lectura/escritura: "+e.getMessage()); |
|
281 |
} |
|
282 |
return f; |
|
283 |
} |
|
136 | 284 |
} |
137 |
|
|
138 | 285 |
} |
Also available in: Unified diff