Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / v2 / GMLFeaturesIterator_v2.java @ 9729

History | View | Annotate | Download (14.9 KB)

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

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

    
8
import org.gvsig.exceptions.BaseException;
9
import org.gvsig.remoteClient.gml.GMLTags;
10
import org.gvsig.remoteClient.gml.IGMLFeaturesIterator;
11
import org.gvsig.remoteClient.gml.exceptions.GMLException;
12
import org.gvsig.remoteClient.gml.exceptions.GMLFileReadException;
13
import org.gvsig.remoteClient.gml.exceptions.GMLParserException;
14
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
15
import org.gvsig.remoteClient.gml.factories.XMLElementsFactory;
16
import org.gvsig.remoteClient.gml.schemas.XMLElement;
17
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
18
import org.gvsig.remoteClient.gml.types.GMLGeometries;
19
import org.gvsig.remoteClient.gml.types.IXMLType;
20
import org.gvsig.remoteClient.gml.types.XMLComplexType;
21
import org.gvsig.remoteClient.gml.types.XMLSimpleType;
22
import org.gvsig.remoteClient.gml.utils.GMLUtilsParser;
23
import org.gvsig.remoteClient.gml.warnings.GMLWarningInfo;
24
import org.gvsig.remoteClient.gml.warnings.GMLWarningWrongNamespace;
25
import org.kxml2.io.KXmlParser;
26
import org.xmlpull.v1.XmlPullParserException;
27

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

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

    
471
}