Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / v2 / GMLFeaturesIterator_v2.java @ 9508

History | View | Annotate | Download (14.5 KB)

1
package org.gvsig.remoteClient.gml.v2;
2

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.Hashtable;
6
import java.util.LinkedHashMap;
7
import java.util.Map;
8

    
9
import org.gvsig.remoteClient.gml.GMLException;
10
import org.gvsig.remoteClient.gml.GMLFileParseInfo;
11
import org.gvsig.remoteClient.gml.GMLTags;
12
import org.gvsig.remoteClient.gml.IGMLFeaturesIterator;
13
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
14
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory;
15
import org.gvsig.remoteClient.gml.schemas.XMLElement;
16
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
17
import org.gvsig.remoteClient.gml.types.GMLGeometries;
18
import org.gvsig.remoteClient.gml.types.IXMLType;
19
import org.gvsig.remoteClient.gml.types.XMLComplexType;
20
import org.gvsig.remoteClient.gml.types.XMLSimpleType;
21
import org.gvsig.remoteClient.gml.utils.GMLUtilsParser;
22
import org.kxml2.io.KXmlParser;
23
import org.xmlpull.v1.XmlPullParserException;
24

    
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 *
43
 * For more information, contact:
44
 *
45
 *  Generalitat Valenciana
46
 *   Conselleria d'Infraestructures i Transport
47
 *   Av. Blasco Ib??ez, 50
48
 *   46010 VALENCIA
49
 *   SPAIN
50
 *
51
 *      +34 963862235
52
 *   gvsig@gva.es
53
 *      www.gvsig.gva.es
54
 *
55
 *    or
56
 *
57
 *   IVER T.I. S.A
58
 *   Salamanca 50
59
 *   46005 Valencia
60
 *   Spain
61
 *
62
 *   +34 963163400
63
 *   dac@iver.es
64
 */
65
/* CVS MESSAGES:
66
 *
67
 * $Id: GMLFeaturesIterator_v2.java 9508 2006-12-29 17:22:17Z jorpiell $
68
 * $Log$
69
 * Revision 1.7  2006-12-29 17:22:17  jorpiell
70
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
71
 *
72
 * Revision 1.6  2006/12/22 11:25:44  csanchez
73
 * Nuevo parser GML 2.x para gml's sin esquema
74
 *
75
 * Revision 1.5  2006/11/06 12:14:38  jorpiell
76
 * Cuando geottols no es capaz de parsear el gML se cargaba la capa con el "aspa roja". Ahora esto ya no ocurre, porque si geotools no es capaz de parsear el gML la geometr?a se descarta
77
 *
78
 * Revision 1.4  2006/10/10 12:52:28  jorpiell
79
 * Soporte para features complejas.
80
 *
81
 * Revision 1.3  2006/10/02 08:33:49  jorpiell
82
 * Cambios del 10 copiados al head
83
 *
84
 * Revision 1.1.2.2  2006/09/25 11:35:15  jorpiell
85
 * Se tienen en cuanta tablas a distintos niveles. En caso de anidamiento se cogen los resultados de la primera tabla que aparezca.
86
 *
87
 * Revision 1.1.2.1  2006/09/19 12:23:15  jorpiell
88
 * Ya no se depende de geotools
89
 *
90
 * Revision 1.2  2006/09/18 12:08:55  jorpiell
91
 * Se han hecho algunas modificaciones que necesitaba el WFS
92
 *
93
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
94
 * Primer commit del driver de Gml
95
 *
96
 *
97
 */
98
/**
99
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
100
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
101
 */
102
public class GMLFeaturesIterator_v2 extends IGMLFeaturesIterator {
103
        public GMLFileParseInfo warnings = null;
104
        private boolean goNextTag= true;
105
        
106
        public GMLFeaturesIterator_v2(XMLSchemaParser parser, IGeometriesFactory factory) {
107
                super(parser, factory);        
108
                //It creates the warnings structure
109
                warnings = new GMLFileParseInfo();
110
        }
111
        
112
        /**
113
         * hasNext()
114
         * @return boolean
115
         *                         -true: if has more features to parse
116
         *                         -false: in other case.  
117
         **/
118
        public boolean hasNext() throws GMLException {
119
                try {
120
                        if ((getParser().getName().compareTo(getFeatureRoot()) == 0) &&
121
                                (getParser().getEventType() == KXmlParser.START_TAG)){
122
                                return true;
123
                        }
124
                } catch (XmlPullParserException e) {
125
                        // TODO Auto-generated catch block
126
                        e.printStackTrace();
127
                        throw new GMLException(GMLException.EXC_PARSE);
128
                }                
129
                return false;
130
        }
131
        
132
        /**
133
         * It implements the iterator function to parse one feature
134
         * @return object (feature)
135
         **/
136
        public Object next() throws GMLException {
137
                boolean endFeature = false;
138
                Object feature = null;
139
                int currentTag;                
140
                
141
                try {        
142
                        // It returns what kind of tag is 
143
                        currentTag = getParser().getEventType();        
144
                        while (!endFeature){
145
                                switch(currentTag){
146
                                case KXmlParser.START_TAG:
147
                                        // It gets the name of the feature <FeatureMember>
148
                                        feature = parseFeature(getParser().getName());
149
                                        break;
150
                                case KXmlParser.END_TAG:
151
                                        //It compares with the name of tag captured before
152
                                        //If is equal then is the end of the fature </FeatureMember>
153
                                        if (getParser().getName().compareTo(getFeatureRoot()) == 0){
154
                                                endFeature = true;        
155
                                                currentTag = getParser().nextTag();
156
                                        }
157
                                        break;
158
                                case KXmlParser.TEXT:
159
                                        //Text Tags are ignored
160
                                        break;
161
                                }
162
                                if (!endFeature){                                        
163
                                        if (getParser().getName().compareTo(getFeatureRoot()) != 0){
164
                                                currentTag = getParser().next();
165
                                        }
166
                                        else
167
                                        {
168
                                                endFeature=true;
169
                                                currentTag = getParser().nextTag();
170
                                        }
171
                                }
172
                        }
173
                }catch (XmlPullParserException e) {
174
                        // TODO Auto-generated catch block
175
                        e.printStackTrace();
176
                        throw new GMLException(GMLException.EXC_PARSE);
177
                } catch (IOException e) {
178
                        // TODO Auto-generated catch block
179
                        e.printStackTrace();
180
                        throw new GMLException(GMLException.EXC_READ_FILE);
181
                }                 
182
                return feature;
183
        }
184
        
185
        /**
186
         * parseFeature(Element):
187
         * Parse an specific feature by its name, if this feature isn't declared in the schema 
188
         * or it doesn't exist, function tries to parse it without schema.
189
         * 
190
         * @param String elementName
191
         **/
192
        private Object parseFeature(String elementName) throws GMLException {
193
                int currentTag;
194
                boolean end = false;
195
                Hashtable values = new Hashtable();                
196
                Object geometry = null;
197
                Object feature = null;
198
                XMLElement entity = null;
199
                
200
                try{                
201
                        currentTag = getParser().next();
202
                        String entityTag = getParser().getName();
203
                        while (!end){                        
204
                                switch(currentTag){
205
                                case KXmlParser.START_TAG:
206
                                        entity = XMLElementsFactory.getElement(getParser().getName());
207
                                        if (entity != null){
208
                                                geometry = parseComplexFeature(entity,values,geometry);                        
209
                                                //When the feature is parsed, it's time to create it
210
                                                if (geometry != null){
211
                                                        feature = getFactory().createSimpleFeature(entity.getName(),entity.getEntityType(),values,geometry);
212
                                                }
213
                                                end = true;
214
                                        }
215
                                        //if the element doesn't exist, it tries to parse it without schema.
216
                                        else{
217
                                                geometry = parseComplexFeatureNoSchema(entityTag,values);                        
218
                                                //The feature is created without schema
219
                                                if (geometry != null){
220
                                                        feature = getFactory().createSimpleFeature("GMLFeature",null,values,geometry);
221
                                                }
222
                                                end = true;
223
                                        }
224
                                        break;
225
                                case KXmlParser.END_TAG:
226
                                        if ((getParser().getName().compareTo(elementName) == 0)){
227
                                                end = true;
228
                                        }
229
                                        break;
230
                                case KXmlParser.TEXT:                   
231
                                        break;
232
                                }
233
                                if (!end){
234
                                        currentTag = getParser().next();
235
                                }        
236
                        }
237
                }catch (XmlPullParserException e) {
238
                        // TODO Auto-generated catch block
239
                        e.printStackTrace();
240
                        throw new GMLException(GMLException.EXC_PARSE);
241
                } catch (IOException e) {
242
                        // TODO Auto-generated catch block
243
                        e.printStackTrace();
244
                        throw new GMLException(GMLException.EXC_READ_FILE);
245
                } 
246
                return feature;
247
        }
248
        
249
        /**
250
         * parseComplexFeature(entity,parameters,values):
251
         * Parses a complex type
252
         * 
253
         * @param complexTypeName
254
         * @param params
255
         * @param values
256
         * @return geometry
257
         * @throws GMLException 
258
         */
259
        private Object parseComplexFeature(XMLElement entity,Map values, Object geometry) throws GMLException{
260
                int currentTag;
261
                boolean end = false;                
262
                
263
                XMLComplexType entityType = (XMLComplexType)entity.getEntityType();
264
                try{                
265
                        currentTag = getParser().nextTag();                                        
266
                        while (!end){                        
267
                                switch(currentTag){
268
                                case KXmlParser.START_TAG:
269
                                        if (getParser().getName().compareTo(GMLTags.GML_BOUNDEDBY)==0){
270
                                                GMLUtilsParser.parseBoundedBy(getParser());
271
                                        }else{
272
                                                String attName = getParser().getName();
273
                                                //The atributte get the entity Type (SIMPLE, COMPLEX or GEOMETRY)
274
                                                XMLElement attribute = entityType.getAttribute(attName);
275
                                                //It compares the tag with the GML Standard
276
                                                GMLGeometries gmlGeometry = new GMLGeometries(attName);
277
                                                if ((attribute != null)||(gmlGeometry.isGML()==true)){
278
                                                        if ((gmlGeometry.isGML()==true)||(attribute.getEntityType() != null)){
279
                                                                if ((gmlGeometry.isGML()==false)&&(attribute.getEntityType().getType() == IXMLType.SIMPLE)){
280
                                                                        if (attribute.isMultiple()){
281
                                                                                ArrayList multiple = null;
282
                                                                                if (values.get(attribute.getName()) == null){
283
                                                                                        multiple = new ArrayList();
284
                                                                                }else{
285
                                                                                        multiple = (ArrayList)values.get(attribute.getName());
286
                                                                                }
287
                                                                                getParser().next();
288
                                                                                multiple.add(((XMLSimpleType)attribute.getEntityType()).getJavaType(getParser().getText()));
289
                                                                                values.put(attName,multiple);
290
                                                                        }else{
291
                                                                                getParser().next();
292
                                                                                values.put(attName,((XMLSimpleType)attribute.getEntityType()).getJavaType(getParser().getText()));
293
                                                                        }
294
                                                                }else if ((gmlGeometry.isGML()==true)||(attribute.getEntityType().getType() == IXMLType.GML_GEOMETRY)){
295
                                                                        //if is GML geometry we try to parse it
296
                                                                        if (gmlGeometry.isGeometryGML()==true){
297
                                                                                geometry = GMLUtilsParser.parseGeometry(getParser(),attName,getFactory());
298
                                                                        }
299
                                                                        else{
300
                                                                                if (gmlGeometry.isFeatureGML()==false){
301
                                                                                        //If isn't a known GML geometry, then is a geometry specified in the schema
302
                                                                                        //we know that this is special tag of geometry and the real geometry is after it
303
                                                                                        currentTag = getParser().next();
304
                                                                                        geometry = GMLUtilsParser.parseGeometry(getParser(),attName,getFactory());
305
                                                                                }
306
                                                                                //Else, it don't do nothing                                                                        
307
                                                                        }
308
                                                                }else if ((gmlGeometry.isGML()==false)&&(attribute.getEntityType().getType() == IXMLType.COMPLEX)){
309
                                                                        if (attribute.isMultiple()){
310
                                                                                ArrayList multiple = null;
311
                                                                                if (values.get(attName) == null){
312
                                                                                        multiple = new ArrayList();
313
                                                                                }else{
314
                                                                                        multiple = (ArrayList)values.get(attribute.getName());
315
                                                                                }
316
                                                                                LinkedHashMap myValues = new LinkedHashMap();         
317
                                                                                geometry=parseComplexFeature(attribute,myValues,geometry);
318
                                                                                multiple.add(myValues);
319
                                                                                values.put(attName,multiple);
320
                                                                        }else{
321
                                                                                LinkedHashMap myValues = new LinkedHashMap();         
322
                                                                                geometry=parseComplexFeature(attribute,myValues,geometry);
323
                                                                                values.put(attName,myValues);
324
                                                                        }
325
                                                                }
326
                                                        }
327
                                                }        
328
                                        }
329
                                        break;
330
                                case KXmlParser.END_TAG:
331
                                        if ((getParser().getName().compareTo(entity.getName()) == 0)){
332
                                                end = true;
333
                                        }
334
                                        break;
335
                                case KXmlParser.TEXT:                   
336
                                        break;
337
                                }
338
                                if (!end){
339
                                        currentTag = getParser().next();
340
                                }        
341
                        }
342
                }catch (XmlPullParserException e) {
343
                        // TODO Auto-generated catch block
344
                        e.printStackTrace();
345
                        throw new GMLException(GMLException.EXC_PARSE);
346
                } catch (IOException e) {
347
                        // TODO Auto-generated catch block
348
                        e.printStackTrace();
349
                        throw new GMLException(GMLException.EXC_READ_FILE);
350
                }         
351
                return geometry;
352
        }
353
        
354
        /**
355
         * Parses a Complex type without Schema
356
         * @param params
357
         * @param values
358
         * @return
359
         * The geom
360
         * @throws GMLException 
361
         */
362
        private Object parseComplexFeatureNoSchema(String entityTag,Map values) throws GMLException{
363
                int currentTag;
364
                boolean end = false;
365
                Object geometry = null;
366

    
367
                try{                
368
                        currentTag = getParser().nextTag();
369
                        while (!end){        
370
                                switch(currentTag){
371
                                case KXmlParser.START_TAG:
372
                                        if (getParser().getName().compareTo(GMLTags.GML_BOUNDEDBY)==0){
373
                                                GMLUtilsParser.parseBoundedBy(getParser());
374
                                        }else{
375
                                                String namespace = getParser().getNameSpace();
376
                                                String tagName = getParser().getName();
377
                                                GMLGeometries gmlGeometry = new GMLGeometries(tagName);
378
                                                //If the tag hasn't namesapce it's local, maybe is an object declaration (or table component)
379
                                                
380
                                                /**************
381
                                                 * <GEOMETRY> *
382
                                                 **************/
383
                                                if ((namespace.compareTo("gml") == 0)||(gmlGeometry.isGML())){
384
                                                        //if is GML geometry we try to parse it
385
                                                        if (gmlGeometry.isGeometryGML()==true){
386
                                                                geometry = GMLUtilsParser.parseGeometry(getParser(),tagName,getFactory());
387
                                                        }
388
                                                        else{
389
                                                                if (gmlGeometry.isFeatureGML()==false){
390
                                                                        warnings.setElement(GMLFileParseInfo.WAR_WRONG_NAMESPACE);
391
                                                                        //MAYBE IT HAD SCHEMA AND IS A GEOM DECLARED THERE, WE CAN TAKE IT LIKE THE IDENTIFIER OF THE GEOM
392
                                                                }
393
                                                                //Else it do nothing
394
                                                        }
395
                                                }
396
                                                /**********
397
                                                 * <DATA> *
398
                                                 **********/
399
                                                else{
400
                                                        //If another namespace it should be tables data  
401
                                                        //it checks that it isn't geometry hide in other namespace
402
                                                        if (gmlGeometry.isGML()==false){
403
                                                                currentTag = getParser().next();
404
                                                                String nextTagName = getParser().getName();
405
                                                                gmlGeometry = new GMLGeometries(nextTagName);
406
                                                                /****************
407
                                                                 *  <SIMPLE>        *
408
                                                                 ****************/
409
                                                                if (currentTag==KXmlParser.TEXT){
410
                                                                        //it gets the value of the param, 
411
                                                                        //it doesn't know what type is, it takes string to default.
412
                                                                        String texto = getParser().getText();
413
                                                                        values.put(tagName,texto);
414
                                                                        
415
                                                                }
416
                                                                /****************
417
                                                                 *  <COMPLEX>        *
418
                                                                 ****************/
419
                                                                else 
420
                                                                {
421
                                                                        goNextTag = false;
422
                                                                        LinkedHashMap myValues = new LinkedHashMap();         
423
                                                                        parseComplexFeatureNoSchema("COMPLEX FEATURE",myValues);
424
                                                                        values.put(tagName,myValues);
425
                                                                }
426
                                                        }
427
                                                }        
428
                                        }
429
                                        break;
430
                                case KXmlParser.END_TAG:
431
                                        if ((getParser().getName().compareTo(entityTag) == 0)||(getParser().getName().compareTo(getFeatureRoot()) == 0)){
432
                                                end = true;
433
                                        }
434
                                        break;
435
                                case KXmlParser.TEXT:                   
436
                                        break;
437
                                }
438
                                if ((!end)&&(goNextTag==true)){                                
439
                                        currentTag = getParser().next();
440
                                }        
441
                                else{
442
                                        goNextTag = true;
443
                                }
444
                        }
445
                }catch (XmlPullParserException e) {
446
                        // TODO Auto-generated catch block
447
                        e.printStackTrace();
448
                        throw new GMLException(GMLException.EXC_PARSE);
449
                } catch (IOException e) {
450
                        // TODO Auto-generated catch block
451
                        e.printStackTrace();
452
                        throw new GMLException(GMLException.EXC_READ_FILE);
453
                }         
454
                return geometry;        
455
        }
456

    
457
}