Statistics
| Revision:

root / branches / v10 / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / v2 / GMLFeaturesIterator_v2.java @ 10876

History | View | Annotate | Download (15.3 KB)

1 6722 jorpiell
package org.gvsig.remoteClient.gml.v2;
2
3
import java.io.IOException;
4
import java.util.ArrayList;
5 9917 jorpiell
import java.util.LinkedHashMap;
6
import java.util.Map;
7 6722 jorpiell
8 8765 jjdelcerro
import org.gvsig.remoteClient.gml.GMLTags;
9 6722 jorpiell
import org.gvsig.remoteClient.gml.IGMLFeaturesIterator;
10 9917 jorpiell
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 6722 jorpiell
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 9917 jorpiell
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 6722 jorpiell
import org.gvsig.remoteClient.gml.utils.GMLUtilsParser;
23 9917 jorpiell
import org.gvsig.remoteClient.gml.warnings.GMLWarningInfo;
24
import org.gvsig.remoteClient.gml.warnings.GMLWarningWrongNamespace;
25 6722 jorpiell
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$
71
 * $Log$
72 10876 jorpiell
 * Revision 1.1.2.6  2007-03-23 10:42:30  jorpiell
73
 * A?adido soporte para elementos anidadas con el mismo nombre que el elemento padre
74
 *
75
 * Revision 1.1.2.5  2007/01/25 16:12:59  jorpiell
76 9917 jorpiell
 * Se han sustituido las clases por las que hay en el nuevo driver de GML.
77 8765 jjdelcerro
 *
78 9917 jorpiell
 * Revision 1.9  2007/01/15 13:11:17  csanchez
79
 * Sistema de Warnings y Excepciones adaptado a BasicException
80
 *
81
 * Revision 1.8  2006/12/29 18:05:20  jorpiell
82
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
83
 *
84
 * Revision 1.6  2006/12/22 11:25:44  csanchez
85
 * Nuevo parser GML 2.x para gml's sin esquema
86
 *
87 8765 jjdelcerro
 * Revision 1.5  2006/11/06 12:14:38  jorpiell
88
 * 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
89
 *
90
 * Revision 1.4  2006/10/10 12:52:28  jorpiell
91
 * Soporte para features complejas.
92
 *
93
 * Revision 1.3  2006/10/02 08:33:49  jorpiell
94
 * Cambios del 10 copiados al head
95
 *
96
 * Revision 1.1.2.2  2006/09/25 11:35:15  jorpiell
97 7557 jorpiell
 * Se tienen en cuanta tablas a distintos niveles. En caso de anidamiento se cogen los resultados de la primera tabla que aparezca.
98
 *
99
 * Revision 1.1.2.1  2006/09/19 12:23:15  jorpiell
100 7355 jorpiell
 * Ya no se depende de geotools
101
 *
102
 * Revision 1.2  2006/09/18 12:08:55  jorpiell
103
 * Se han hecho algunas modificaciones que necesitaba el WFS
104
 *
105
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
106 6722 jorpiell
 * Primer commit del driver de Gml
107
 *
108
 *
109
 */
110
/**
111
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
112 9917 jorpiell
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
113 6722 jorpiell
 */
114
public class GMLFeaturesIterator_v2 extends IGMLFeaturesIterator {
115 9917 jorpiell
        public GMLWarningInfo warnings = null;
116
        private boolean goNextTag= true;
117 6722 jorpiell
118
        public GMLFeaturesIterator_v2(XMLSchemaParser parser, IGeometriesFactory factory) {
119 9917 jorpiell
                super(parser, factory);
120
                //It creates the warnings structure
121
                warnings = new GMLWarningInfo();
122 6722 jorpiell
        }
123
124 9917 jorpiell
        /**
125
         * hasNext()
126
         * @return boolean
127
         *                         -true: if has more features to parse
128
         *                         -false: in other case.
129
         **/
130 6722 jorpiell
        public boolean hasNext() throws GMLException {
131 7355 jorpiell
                try {
132
                        if ((getParser().getName().compareTo(getFeatureRoot()) == 0) &&
133
                                (getParser().getEventType() == KXmlParser.START_TAG)){
134
                                return true;
135
                        }
136
                } catch (XmlPullParserException e) {
137 9917 jorpiell
                        //Captured KXML parsing Exception
138
                        throw new GMLException(new GMLParserException(e));
139 6722 jorpiell
                }
140
                return false;
141
        }
142
143 9917 jorpiell
        /**
144
         * It implements the iterator function to parse one feature
145
         * @return object (feature)
146
         **/
147
        public Object next() throws GMLException{
148 6722 jorpiell
                boolean endFeature = false;
149
                Object feature = null;
150
                int currentTag;
151
152
                try {
153 9917 jorpiell
                        // It returns what kind of tag is
154 7355 jorpiell
                        currentTag = getParser().getEventType();
155 6722 jorpiell
                        while (!endFeature){
156
                                switch(currentTag){
157
                                case KXmlParser.START_TAG:
158 9917 jorpiell
                                        // It gets the name of the feature <FeatureMember>
159
                                        feature = parseFeature(getParser().getName());
160 6722 jorpiell
                                        break;
161 9917 jorpiell
                                case KXmlParser.END_TAG:
162
                                        //It compares with the name of tag captured before
163
                                        //If is equal then is the end of the fature </FeatureMember>
164 7355 jorpiell
                                        if (getParser().getName().compareTo(getFeatureRoot()) == 0){
165
                                                endFeature = true;
166
                                                currentTag = getParser().nextTag();
167
                                        }
168 6722 jorpiell
                                        break;
169 9917 jorpiell
                                case KXmlParser.TEXT:
170
                                        //Text Tags are ignored
171 6722 jorpiell
                                        break;
172
                                }
173 9917 jorpiell
                                if (!endFeature){
174
                                        if (getParser().getName().compareTo(getFeatureRoot()) != 0){
175
                                                currentTag = getParser().next();
176
                                        }
177
                                        else
178
                                        {
179
                                                endFeature=true;
180
                                                currentTag = getParser().nextTag();
181
                                        }
182 7355 jorpiell
                                }
183 6722 jorpiell
                        }
184
                }catch (XmlPullParserException e) {
185 9917 jorpiell
                        //Captured KXML parsing Exception
186
                        throw new GMLException(new GMLParserException(e));
187 6722 jorpiell
                } catch (IOException e) {
188
                        // TODO Auto-generated catch block
189 9917 jorpiell
                        throw new GMLException(new GMLFileReadException(e));
190
                } catch (BaseException e) {
191
                        throw new GMLException(e);
192 6722 jorpiell
                }
193
                return feature;
194
        }
195
196
        /**
197 9917 jorpiell
         * parseFeature(Element):
198
         * Parse an specific feature by its name, if this feature isn't declared in the schema
199
         * or it doesn't exist, function tries to parse it without schema.
200
         *
201
         * @param String elementName
202
         **/
203
        private Object parseFeature(String elementName) throws BaseException{
204 6722 jorpiell
                int currentTag;
205
                boolean end = false;
206 9917 jorpiell
                LinkedHashMap values = new LinkedHashMap();
207
                Object geometry = null;
208
                Object feature = null;
209 8765 jjdelcerro
                XMLElement entity = null;
210 6722 jorpiell
211 8765 jjdelcerro
                try{
212 9917 jorpiell
                        currentTag = getParser().next();
213
                        String entityTag = getParser().getName();
214 8765 jjdelcerro
                        while (!end){
215
                                switch(currentTag){
216
                                case KXmlParser.START_TAG:
217
                                        entity = XMLElementsFactory.getElement(getParser().getName());
218
                                        if (entity != null){
219 9917 jorpiell
                                                geometry = parseComplexFeature(entity,values,geometry);
220
                                                //When the feature is parsed, it's time to create it
221
                                                if (geometry != null){
222
                                                        feature = getFactory().createSimpleFeature(entity.getName(),entity.getEntityType(),values,geometry);
223
                                                }
224 8765 jjdelcerro
                                                end = true;
225
                                        }
226 9917 jorpiell
                                        //if the element doesn't exist, it tries to parse it without schema.
227
                                        else{
228
                                                geometry = parseComplexFeatureNoSchema(entityTag,values);
229
                                                //The feature is created without schema
230
                                                if (geometry != null){
231
                                                        feature = getFactory().createSimpleFeature("GMLFeature",null,values,geometry);
232
                                                }
233
                                                end = true;
234
                                        }
235 8765 jjdelcerro
                                        break;
236
                                case KXmlParser.END_TAG:
237 9917 jorpiell
                                        if ((getParser().getName().compareTo(elementName) == 0)){
238 8765 jjdelcerro
                                                end = true;
239 9917 jorpiell
                                        }
240 8765 jjdelcerro
                                        break;
241
                                case KXmlParser.TEXT:
242
                                        break;
243
                                }
244
                                if (!end){
245
                                        currentTag = getParser().next();
246
                                }
247
                        }
248
                }catch (XmlPullParserException e) {
249 9917 jorpiell
                        //Captured KXML parsing Exception
250
                        throw new GMLParserException(e);
251
                } catch (IOException e) {
252 8765 jjdelcerro
                        // TODO Auto-generated catch block
253 9917 jorpiell
                        throw new GMLFileReadException(e);
254
                }
255
                return feature;
256
        }
257
258
        /**
259
         * parseComplexFeature(entity,parameters,values):
260
         * Parses a complex type
261
         *
262
         * @param complexTypeName
263
         * @param params
264
         * @param values
265
         * @return geometry
266
         * @throws GMLException
267
         */
268
        private Object parseComplexFeature(XMLElement entity,Map values, Object geometry) throws BaseException{
269
                int currentTag = KXmlParser.START_TAG;
270
                boolean end = false;
271
272
                XMLComplexType entityType = (XMLComplexType)entity.getEntityType();
273
                try{
274
                        if(goNextTag)
275
                                currentTag = getParser().nextTag();
276
                        while (!end){
277
                                switch(currentTag){
278
                                case KXmlParser.START_TAG:
279
                                        if (getParser().getName().compareTo(GMLTags.GML_BOUNDEDBY)==0){
280
                                                GMLUtilsParser.parseBoundedBy(getParser());
281
                                        }else{
282
                                                String attName = getParser().getName();
283
                                                //The atributte get the entity Type (SIMPLE, COMPLEX or GEOMETRY)
284
                                                XMLElement attribute = entityType.getAttribute(attName);
285
                                                //It compares the tag with the GML Standard
286
                                                GMLGeometries gmlGeometry = new GMLGeometries(attName);
287
                                                if ((attribute != null)||(gmlGeometry.isGML()==true)){
288
                                                        if ((gmlGeometry.isGML()==true)||(attribute.getEntityType() != null)){
289
                                                                /************
290
                                                                 * <SIMPLE> *
291
                                                                 ************/
292
                                                                if ((gmlGeometry.isGML()==false)&&(attribute.getEntityType().getType() == IXMLType.SIMPLE)){
293
                                                                        if (attribute.isMultiple()){
294
                                                                                ArrayList multiple = null;
295
                                                                                if (values.get(attribute.getName()) == null){
296
                                                                                        multiple = new ArrayList();
297
                                                                                }else{
298
                                                                                        multiple = (ArrayList)values.get(attribute.getName());
299
                                                                                }
300
                                                                                getParser().next();
301
                                                                                multiple.add(((XMLSimpleType)attribute.getEntityType()).getJavaType(getParser().getText()));
302
                                                                                values.put(attName,multiple);
303
                                                                        }else{
304
                                                                                getParser().next();
305
                                                                                values.put(attName,((XMLSimpleType)attribute.getEntityType()).getJavaType(getParser().getText()));
306
                                                                        }
307 10876 jorpiell
                                                                        //Jump the end tag
308
                                                                        getParser().next();
309 9917 jorpiell
                                                                }
310
                                                                /**************
311
                                                                 * <GEOMETRY> *
312
                                                                 **************/
313
                                                                else if ((gmlGeometry.isGML()==true)||(attribute.getEntityType().getType() == IXMLType.GML_GEOMETRY)){
314
                                                                        //if is GML geometry we try to parse it
315
                                                                        if (gmlGeometry.isGeometryGML()==true){
316
                                                                                geometry = GMLUtilsParser.parseGeometry(getParser(),attName,getFactory());
317
                                                                        }
318
                                                                        else{
319
                                                                                if (gmlGeometry.isFeatureGML()==false){
320
                                                                                        //If isn't a known GML geometry, then is a geometry specified in the schema
321
                                                                                        //we know that this is special tag of geometry and the real geometry is after it
322
                                                                                        currentTag = getParser().next();
323
                                                                                        geometry = GMLUtilsParser.parseGeometry(getParser(),attName,getFactory());
324
                                                                                }
325
                                                                                //Else, it does nothing
326
                                                                        }
327
                                                                }
328
                                                                /****************
329
                                                                 *  <COMPLEX>        *
330
                                                                 ****************/
331
                                                                else if ((gmlGeometry.isGML()==false)&&(attribute.getEntityType().getType() == IXMLType.COMPLEX)){
332
                                                                        if (attribute.isMultiple()){
333
                                                                                ArrayList multiple = null;
334
                                                                                if (values.get(attName) == null){
335
                                                                                        multiple = new ArrayList();
336
                                                                                }else{
337
                                                                                        multiple = (ArrayList)values.get(attribute.getName());
338
                                                                                }
339
                                                                                LinkedHashMap myValues = new LinkedHashMap();
340
                                                                                geometry=parseComplexFeature(attribute,myValues,geometry);
341
                                                                                multiple.add(myValues);
342
                                                                                values.put(attName,multiple);
343
                                                                        }else{
344
                                                                                LinkedHashMap myValues = new LinkedHashMap();
345
                                                                                geometry=parseComplexFeature(attribute,myValues,geometry);
346
                                                                                values.put(attName,myValues);
347
                                                                        }
348
                                                                }
349
                                                        }
350
                                                }
351
                                        }
352
                                        break;
353
                                case KXmlParser.END_TAG:
354
                                        if ((getParser().getName().compareTo(entity.getName()) == 0)){
355
                                                end = true;
356
                                        }
357
                                        break;
358
                                case KXmlParser.TEXT:
359
                                        break;
360
                                }
361
                                if ((!end)&&(goNextTag==true)){
362
                                        currentTag = getParser().next();
363
                                }
364
                                else{
365
                                        goNextTag = true;
366
                                }
367
                        }
368
                }catch (XmlPullParserException e) {
369
                        //Captured KXML parsing Exception
370
                        throw new GMLParserException(e);
371 8765 jjdelcerro
                } catch (IOException e) {
372
                        // TODO Auto-generated catch block
373 9917 jorpiell
                        throw new GMLFileReadException(e);
374 8765 jjdelcerro
                }
375 9917 jorpiell
                return geometry;
376 8765 jjdelcerro
        }
377
378
        /**
379 9917 jorpiell
         * Parses a Complex type without Schema
380 8765 jjdelcerro
         * @param params
381
         * @param values
382
         * @return
383
         * The geom
384
         * @throws GMLException
385
         */
386 9917 jorpiell
        private Object parseComplexFeatureNoSchema(String entityTag,Map values) throws BaseException{
387 8765 jjdelcerro
                int currentTag;
388
                boolean end = false;
389 9917 jorpiell
                Object geometry = null;
390
391 8765 jjdelcerro
                try{
392 9917 jorpiell
                        currentTag = getParser().nextTag();
393
                        while (!end){
394 8765 jjdelcerro
                                switch(currentTag){
395
                                case KXmlParser.START_TAG:
396
                                        if (getParser().getName().compareTo(GMLTags.GML_BOUNDEDBY)==0){
397
                                                GMLUtilsParser.parseBoundedBy(getParser());
398
                                        }else{
399 9917 jorpiell
                                                String namespace = getParser().getNameSpace();
400
                                                String tagName = getParser().getName();
401
                                                GMLGeometries gmlGeometry = new GMLGeometries(tagName);
402
                                                //If the tag hasn't namesapce it's local, maybe is an object declaration (or table component)
403
404
                                                /**************
405
                                                 * <GEOMETRY> *
406
                                                 **************/
407
                                                if ((namespace.compareTo("gml") == 0)||(gmlGeometry.isGML())){
408
                                                        //if is GML geometry we try to parse it
409
                                                        if (gmlGeometry.isGeometryGML()==true){
410
                                                                geometry = GMLUtilsParser.parseGeometry(getParser(),tagName,getFactory());
411
                                                        }
412
                                                        else{
413
                                                                if (gmlGeometry.isFeatureGML()==false){
414
                                                                        warnings.setElement(new GMLWarningWrongNamespace("gml"));
415
                                                                        //MAYBE IT HAD SCHEMA AND IS A GEOM DECLARED THERE, WE CAN TAKE IT LIKE THE IDENTIFIER OF THE GEOM
416 7557 jorpiell
                                                                }
417 9917 jorpiell
                                                                //Else it do nothing
418 6722 jorpiell
                                                        }
419
                                                }
420 9917 jorpiell
                                                /**********
421
                                                 * <DATA> *
422
                                                 **********/
423
                                                else{
424
                                                        //If another namespace it should be tables data
425
                                                        //it checks that it isn't geometry hide in other namespace
426
                                                        if (gmlGeometry.isGML()==false){
427
                                                                currentTag = getParser().next();
428
                                                                String nextTagName = getParser().getName();
429
                                                                gmlGeometry = new GMLGeometries(nextTagName);
430
                                                                /****************
431
                                                                 *  <SIMPLE>        *
432
                                                                 ****************/
433
                                                                if (currentTag==KXmlParser.TEXT){
434
                                                                        //it gets the value of the param,
435
                                                                        //it doesn't know what type is, it takes string to default.
436
                                                                        String texto = getParser().getText();
437
                                                                        values.put(tagName,texto);
438
439
                                                                }
440
                                                                /****************
441
                                                                 *  <COMPLEX>        *
442
                                                                 ****************/
443
                                                                else
444
                                                                {
445
                                                                        goNextTag = false;
446
                                                                        LinkedHashMap myValues = new LinkedHashMap();
447
                                                                        parseComplexFeatureNoSchema("COMPLEX FEATURE",myValues);
448
                                                                        values.put(tagName,myValues);
449
                                                                }
450
                                                        }
451
                                                }
452 6722 jorpiell
                                        }
453 8765 jjdelcerro
                                        break;
454
                                case KXmlParser.END_TAG:
455 9917 jorpiell
                                        if ((getParser().getName().compareTo(entityTag) == 0)||(getParser().getName().compareTo(getFeatureRoot()) == 0)){
456 8765 jjdelcerro
                                                end = true;
457 9917 jorpiell
                                        }
458 8765 jjdelcerro
                                        break;
459
                                case KXmlParser.TEXT:
460
                                        break;
461 6722 jorpiell
                                }
462 9917 jorpiell
                                if ((!end)&&(goNextTag==true)){
463 8765 jjdelcerro
                                        currentTag = getParser().next();
464
                                }
465 9917 jorpiell
                                else{
466
                                        goNextTag = true;
467
                                }
468 8765 jjdelcerro
                        }
469
                }catch (XmlPullParserException e) {
470 9917 jorpiell
                        //Captured KXML parsing Exception
471
                        throw new GMLParserException(e);
472 8765 jjdelcerro
                } catch (IOException e) {
473
                        // TODO Auto-generated catch block
474 9917 jorpiell
                        throw new GMLFileReadException(e);
475 8765 jjdelcerro
                }
476 9917 jorpiell
                return geometry;
477
        }
478 8765 jjdelcerro
479 6722 jorpiell
}