Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / v2 / GMLFeaturesIterator_v2.java @ 9917

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

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

    
474
}