root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / schemas / XMLSchemaParser.java @ 8017
History | View | Annotate | Download (9.98 KB)
1 |
package org.gvsig.remoteClient.gml.schemas; |
---|---|
2 |
|
3 |
import java.io.BufferedReader; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.FileReader; |
8 |
import java.io.IOException; |
9 |
import java.util.Hashtable; |
10 |
import java.util.Iterator; |
11 |
import java.util.Set; |
12 |
import java.util.Vector; |
13 |
|
14 |
import org.gvsig.remoteClient.gml.GMLTags; |
15 |
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory; |
16 |
import org.gvsig.remoteClient.gml.factories.XMLTypesFactory; |
17 |
import org.gvsig.remoteClient.utils.CapabilitiesTags; |
18 |
import org.kxml2.io.KXmlParser; |
19 |
import org.xmlpull.v1.XmlPullParserException; |
20 |
|
21 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
22 |
*
|
23 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
24 |
*
|
25 |
* This program is free software; you can redistribute it and/or
|
26 |
* modify it under the terms of the GNU General Public License
|
27 |
* as published by the Free Software Foundation; either version 2
|
28 |
* of the License, or (at your option) any later version.
|
29 |
*
|
30 |
* This program is distributed in the hope that it will be useful,
|
31 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
32 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
33 |
* GNU General Public License for more details.
|
34 |
*
|
35 |
* You should have received a copy of the GNU General Public License
|
36 |
* along with this program; if not, write to the Free Software
|
37 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
38 |
*
|
39 |
* For more information, contact:
|
40 |
*
|
41 |
* Generalitat Valenciana
|
42 |
* Conselleria d'Infraestructures i Transport
|
43 |
* Av. Blasco Ib??ez, 50
|
44 |
* 46010 VALENCIA
|
45 |
* SPAIN
|
46 |
*
|
47 |
* +34 963862235
|
48 |
* gvsig@gva.es
|
49 |
* www.gvsig.gva.es
|
50 |
*
|
51 |
* or
|
52 |
*
|
53 |
* IVER T.I. S.A
|
54 |
* Salamanca 50
|
55 |
* 46005 Valencia
|
56 |
* Spain
|
57 |
*
|
58 |
* +34 963163400
|
59 |
* dac@iver.es
|
60 |
*/
|
61 |
/* CVS MESSAGES:
|
62 |
*
|
63 |
* $Id: XMLSchemaParser.java 8017 2006-10-10 12:52:28Z jorpiell $
|
64 |
* $Log$
|
65 |
* Revision 1.4 2006-10-10 12:52:28 jorpiell
|
66 |
* Soporte para features complejas.
|
67 |
*
|
68 |
* Revision 1.3 2006/10/02 08:33:49 jorpiell
|
69 |
* Cambios del 10 copiados al head
|
70 |
*
|
71 |
* Revision 1.1.2.1 2006/09/19 12:23:15 jorpiell
|
72 |
* Ya no se depende de geotools
|
73 |
*
|
74 |
* Revision 1.2 2006/09/18 12:08:55 jorpiell
|
75 |
* Se han hecho algunas modificaciones que necesitaba el WFS
|
76 |
*
|
77 |
* Revision 1.1 2006/08/10 12:00:49 jorpiell
|
78 |
* Primer commit del driver de Gml
|
79 |
*
|
80 |
* Revision 1.1 2006/05/16 14:12:56 jorpiell
|
81 |
* A?adido el parseador de Esquemas
|
82 |
*
|
83 |
*
|
84 |
*/
|
85 |
/**
|
86 |
* Thas class is used to parse a schema XSD
|
87 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
88 |
*/
|
89 |
public class XMLSchemaParser extends KXmlParser { |
90 |
private String schema = ""; |
91 |
private String encoding = "UTF-8"; |
92 |
private String nameSpace = ""; |
93 |
private Hashtable attributes = null; |
94 |
|
95 |
public XMLSchemaParser(){
|
96 |
super();
|
97 |
attributes = new Hashtable(); |
98 |
} |
99 |
|
100 |
public XMLSchemaParser(String schema){ |
101 |
super();
|
102 |
this.schema = schema;
|
103 |
attributes = new Hashtable(); |
104 |
} |
105 |
|
106 |
/**
|
107 |
* It gets the schema from a tag. The schema is separated
|
108 |
* of the tag name by ":".
|
109 |
* @param tag
|
110 |
*/
|
111 |
public void setSchemaFromMainTag(String tag){ |
112 |
int pos = tag.indexOf(":"); |
113 |
if (pos > 0){ |
114 |
this.schema = tag.substring(0,pos); |
115 |
}else{
|
116 |
this.schema = ""; |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* @return Returns the schema.
|
122 |
*/
|
123 |
public String getSchema() { |
124 |
return schema;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* @param schema The schema to set.
|
129 |
*/
|
130 |
public void setSchema(String schema) { |
131 |
this.schema = schema;
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* Returns a SCHEMA:TAG
|
136 |
* @param tag
|
137 |
* @return SCHEMA:TAG
|
138 |
*/
|
139 |
private String getTag(String tag){ |
140 |
if (tag == null){ |
141 |
return null; |
142 |
} |
143 |
if ((schema == null) || (schema.equals(""))){ |
144 |
return tag;
|
145 |
} |
146 |
return schema + ":" + tag; |
147 |
} |
148 |
|
149 |
/*
|
150 |
* (non-Javadoc)
|
151 |
* @see org.xmlpull.v1.XmlPullParser#require(int, java.lang.String, java.lang.String)
|
152 |
*/
|
153 |
public void require(int type, String namespace, String name) |
154 |
throws XmlPullParserException, IOException{ |
155 |
super.require(type,namespace,getTag(name));
|
156 |
} |
157 |
|
158 |
/*
|
159 |
* (non-Javadoc)
|
160 |
* @see org.xmlpull.v1.XmlPullParser#getName()
|
161 |
*/
|
162 |
public String getName(){ |
163 |
try{
|
164 |
String name = super.getName(); |
165 |
if ((schema != null) || (!(schema.equals("")))){ |
166 |
return name.substring(name.indexOf(":") + 1,name.length()); |
167 |
} |
168 |
return name;
|
169 |
}catch (NullPointerException e){ |
170 |
return ""; |
171 |
} |
172 |
} |
173 |
|
174 |
public void parse(File f,String nameSpace) { |
175 |
this.nameSpace = nameSpace;
|
176 |
FileReader reader = null; |
177 |
try
|
178 |
{ |
179 |
reader = new FileReader(f); |
180 |
BufferedReader br = new BufferedReader(reader); |
181 |
char[] buffer = new char[100]; |
182 |
br.read(buffer); |
183 |
StringBuffer st = new StringBuffer(new String(buffer)); |
184 |
String searchText = "encoding=\""; |
185 |
int index = st.indexOf(searchText);
|
186 |
if (index>-1) { |
187 |
st.delete(0, index+searchText.length());
|
188 |
encoding = st.substring(0, st.indexOf("\"")); |
189 |
} |
190 |
} catch (FileNotFoundException ex) { |
191 |
ex.printStackTrace(); |
192 |
} catch (IOException e) { |
193 |
e.printStackTrace(); |
194 |
} |
195 |
|
196 |
int tag;
|
197 |
|
198 |
try
|
199 |
{ |
200 |
setInput(new FileInputStream(f), encoding); |
201 |
nextTag(); |
202 |
|
203 |
if ( getEventType() != KXmlParser.END_DOCUMENT )
|
204 |
{ |
205 |
setSchemaFromMainTag(getName()); |
206 |
|
207 |
require(KXmlParser.START_TAG, null, CapabilitiesTags.WFS_SCHEMAROOT);
|
208 |
tag = nextTag(); |
209 |
while(tag != KXmlParser.END_DOCUMENT)
|
210 |
{ |
211 |
switch(tag)
|
212 |
{ |
213 |
case KXmlParser.START_TAG:
|
214 |
if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){ |
215 |
for (int i=0 ; i<getAttributeCount() ; i++){ |
216 |
if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){ |
217 |
XMLComplexType complexType = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i)); |
218 |
parseComplexType(complexType); |
219 |
attributes.put(complexType.getName(),complexType); |
220 |
} |
221 |
|
222 |
} |
223 |
} else if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){ |
224 |
XMLElement entity = XMLElementsFactory.addType(this);
|
225 |
try{
|
226 |
attributes.put(entity.getName(),entity.getEntityType()); |
227 |
}catch(NullPointerException e){ |
228 |
//Type not defined
|
229 |
} |
230 |
|
231 |
} |
232 |
break;
|
233 |
case KXmlParser.END_TAG:
|
234 |
break;
|
235 |
case KXmlParser.TEXT:
|
236 |
if (getName()!=null) |
237 |
System.out.println("[TEXT]["+getText().trim()+"]"); |
238 |
break;
|
239 |
} |
240 |
tag = next(); |
241 |
} |
242 |
require(KXmlParser.END_DOCUMENT, null, null); |
243 |
} |
244 |
} |
245 |
catch(XmlPullParserException parser_ex){
|
246 |
System.out.println(parser_ex.getMessage());
|
247 |
parser_ex.printStackTrace(); |
248 |
return;
|
249 |
} |
250 |
catch (IOException ioe) { |
251 |
ioe.printStackTrace(); |
252 |
return;
|
253 |
} |
254 |
} |
255 |
|
256 |
private void parseComplexType(XMLComplexType complexType) throws IOException, XmlPullParserException{ |
257 |
int currentTag;
|
258 |
boolean end = false; |
259 |
|
260 |
require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXTYPE);
|
261 |
currentTag = next(); |
262 |
|
263 |
while (!end)
|
264 |
{ |
265 |
switch(currentTag)
|
266 |
{ |
267 |
case KXmlParser.START_TAG:
|
268 |
if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT)==0){ |
269 |
parseComplexContent(complexType); |
270 |
}else if(getName().compareTo(CapabilitiesTags.SEQUENCE)==0){ |
271 |
parseSequence(complexType); |
272 |
} |
273 |
break;
|
274 |
case KXmlParser.END_TAG:
|
275 |
if (getName().compareTo(CapabilitiesTags.COMPLEXTYPE) == 0) |
276 |
end = true;
|
277 |
break;
|
278 |
case KXmlParser.TEXT:
|
279 |
break;
|
280 |
} |
281 |
if (!end){
|
282 |
currentTag = next(); |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
private void parseComplexContent(XMLComplexType complexType) throws IOException, XmlPullParserException |
288 |
{ |
289 |
int currentTag;
|
290 |
boolean end = false; |
291 |
|
292 |
require(KXmlParser.START_TAG, null, CapabilitiesTags.COMPLEXCONTENT);
|
293 |
currentTag = next(); |
294 |
|
295 |
while (!end)
|
296 |
{ |
297 |
switch(currentTag)
|
298 |
{ |
299 |
case KXmlParser.START_TAG:
|
300 |
if (getName().compareTo(CapabilitiesTags.EXTENSION )==0) |
301 |
{ |
302 |
parseExtension(complexType); |
303 |
} |
304 |
break;
|
305 |
case KXmlParser.END_TAG:
|
306 |
if (getName().compareTo(CapabilitiesTags.COMPLEXCONTENT) == 0) |
307 |
end = true;
|
308 |
break;
|
309 |
case KXmlParser.TEXT:
|
310 |
break;
|
311 |
} |
312 |
if (!end){
|
313 |
currentTag = next(); |
314 |
} |
315 |
} |
316 |
} |
317 |
|
318 |
private void parseExtension(XMLComplexType complexType) throws IOException, XmlPullParserException |
319 |
{ |
320 |
int currentTag;
|
321 |
boolean end = false; |
322 |
|
323 |
require(KXmlParser.START_TAG, null, CapabilitiesTags.EXTENSION);
|
324 |
currentTag = next(); |
325 |
|
326 |
while (!end)
|
327 |
{ |
328 |
switch(currentTag)
|
329 |
{ |
330 |
case KXmlParser.START_TAG:
|
331 |
if (getName().compareTo(CapabilitiesTags.SEQUENCE)==0) |
332 |
{ |
333 |
parseSequence(complexType); |
334 |
} |
335 |
break;
|
336 |
case KXmlParser.END_TAG:
|
337 |
if (getName().compareTo(CapabilitiesTags.EXTENSION) == 0) |
338 |
end = true;
|
339 |
break;
|
340 |
case KXmlParser.TEXT:
|
341 |
break;
|
342 |
} |
343 |
if (!end){
|
344 |
currentTag = next(); |
345 |
} |
346 |
} |
347 |
} |
348 |
|
349 |
private void parseSequence(XMLComplexType complexType) throws IOException, XmlPullParserException |
350 |
{ |
351 |
int currentTag;
|
352 |
boolean end = false; |
353 |
|
354 |
require(KXmlParser.START_TAG, null, CapabilitiesTags.SEQUENCE);
|
355 |
currentTag = next(); |
356 |
|
357 |
while (!end)
|
358 |
{ |
359 |
switch(currentTag)
|
360 |
{ |
361 |
case KXmlParser.START_TAG:
|
362 |
if (getName().compareTo(CapabilitiesTags.ELEMENT)==0){ |
363 |
XMLElement element = new XMLElement(this); |
364 |
if (element != null){ |
365 |
complexType.addSubtypes(element); |
366 |
} |
367 |
}else if(getName().compareTo(CapabilitiesTags.COMPLEXTYPE)==0){ |
368 |
for (int i=0 ; i<getAttributeCount() ; i++){ |
369 |
if (getAttributeName(i).compareTo(GMLTags.GML_NAME) == 0){ |
370 |
XMLComplexType complexTypeChild = XMLTypesFactory.addComplexType(nameSpace,getAttributeValue(i)); |
371 |
parseComplexType(complexTypeChild); |
372 |
} |
373 |
} |
374 |
} |
375 |
break;
|
376 |
case KXmlParser.END_TAG:
|
377 |
if (getName().compareTo(CapabilitiesTags.SEQUENCE) == 0) |
378 |
end = true;
|
379 |
break;
|
380 |
case KXmlParser.TEXT:
|
381 |
break;
|
382 |
} |
383 |
if (!end){
|
384 |
currentTag = next(); |
385 |
} |
386 |
} |
387 |
} |
388 |
|
389 |
/**
|
390 |
* @return Returns the attributes.
|
391 |
*/
|
392 |
public Hashtable getAttributes() { |
393 |
return attributes;
|
394 |
} |
395 |
|
396 |
public Vector getAttributesList(){ |
397 |
Vector vector = new Vector(); |
398 |
Set keys = attributes.keySet();
|
399 |
Iterator it = keys.iterator();
|
400 |
while(it.hasNext()){
|
401 |
vector.add(attributes.get((String)it.next()));
|
402 |
} |
403 |
return vector;
|
404 |
} |
405 |
|
406 |
|
407 |
} |