Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / v2 / GMLFeaturesIterator_v2.java @ 8745

History | View | Annotate | Download (6.77 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.Vector;
7

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

    
22
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
23
 *
24
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
25
 *
26
 * This program is free software; you can redistribute it and/or
27
 * modify it under the terms of the GNU General Public License
28
 * as published by the Free Software Foundation; either version 2
29
 * of the License, or (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
39
 *
40
 * For more information, contact:
41
 *
42
 *  Generalitat Valenciana
43
 *   Conselleria d'Infraestructures i Transport
44
 *   Av. Blasco Ib??ez, 50
45
 *   46010 VALENCIA
46
 *   SPAIN
47
 *
48
 *      +34 963862235
49
 *   gvsig@gva.es
50
 *      www.gvsig.gva.es
51
 *
52
 *    or
53
 *
54
 *   IVER T.I. S.A
55
 *   Salamanca 50
56
 *   46005 Valencia
57
 *   Spain
58
 *
59
 *   +34 963163400
60
 *   dac@iver.es
61
 */
62
/* CVS MESSAGES:
63
 *
64
 * $Id: GMLFeaturesIterator_v2.java 8745 2006-11-14 13:14:23Z  $
65
 * $Log$
66
 * Revision 1.1.2.2  2006-09-25 11:35:15  jorpiell
67
 * Se tienen en cuanta tablas a distintos niveles. En caso de anidamiento se cogen los resultados de la primera tabla que aparezca.
68
 *
69
 * Revision 1.1.2.1  2006/09/19 12:23:15  jorpiell
70
 * Ya no se depende de geotools
71
 *
72
 * Revision 1.2  2006/09/18 12:08:55  jorpiell
73
 * Se han hecho algunas modificaciones que necesitaba el WFS
74
 *
75
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
76
 * Primer commit del driver de Gml
77
 *
78
 *
79
 */
80
/**
81
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
82
 */
83
public class GMLFeaturesIterator_v2 extends IGMLFeaturesIterator {
84
        
85
        public GMLFeaturesIterator_v2(XMLSchemaParser parser, IGeometriesFactory factory) {
86
                super(parser, factory);                
87
        }
88
        
89
        /*
90
         *  (non-Javadoc)
91
         * @see org.gvsig.remoteClient.gml.IGMLFeaturesIterator#hasNext()
92
         */
93
        public boolean hasNext() throws GMLException {
94
                try {
95
                        if ((getParser().getName().compareTo(getFeatureRoot()) == 0) &&
96
                                (getParser().getEventType() == KXmlParser.START_TAG)){
97
                                return true;
98
                        }
99
                } catch (XmlPullParserException e) {
100
                        // TODO Auto-generated catch block
101
                        e.printStackTrace();
102
                        throw new GMLException(GMLException.EXC_PARSE);
103
                }                
104
                return false;
105
        }
106
        
107
        /*
108
         *  (non-Javadoc)
109
         * @see org.gvsig.remoteClient.gml.IGMLFeaturesIterator#next()
110
         */
111
        public Object next() throws GMLException {
112
                boolean endFeature = false;
113
                Object feature = null;
114
                int currentTag;                
115
                
116
                try {        
117
                        currentTag = getParser().getEventType();        
118
                        while (!endFeature){
119
                                switch(currentTag){
120
                                case KXmlParser.START_TAG:
121
                                        feature = parseFeature();
122
                                        break;
123
                                case KXmlParser.END_TAG:                                
124
                                        if (getParser().getName().compareTo(getFeatureRoot()) == 0){
125
                                                endFeature = true;        
126
                                                currentTag = getParser().nextTag();
127
                                        }
128
                                        break;
129
                                case KXmlParser.TEXT:                   
130
                                        break;
131
                                }
132
                                if (!endFeature){
133
                                        currentTag = getParser().next();
134
                                }
135
                        }
136
                }catch (XmlPullParserException e) {
137
                        // TODO Auto-generated catch block
138
                        e.printStackTrace();
139
                        throw new GMLException(GMLException.EXC_PARSE);
140
                } catch (IOException e) {
141
                        // TODO Auto-generated catch block
142
                        e.printStackTrace();
143
                        throw new GMLException(GMLException.EXC_READ_FILE);
144
                }                 
145
                return feature;
146
        }
147
        
148
        /**
149
         * It parses a Feature
150
         * @throws IOException 
151
         * @throws XmlPullParserException 
152
         *
153
         */
154
        private Object parseFeature() throws GMLException {
155
                int currentTag;
156
                boolean end = false;
157
                                
158
                try {
159
                        currentTag = getParser().getEventType();
160
                        String featureType = null;
161
                        
162
                        if ((getParser().getName() != null) && (featureType == null)){
163
                                featureType = getParser().getName();
164
                                return parseSpecificFeature(featureType);
165
                        }
166
                }catch (XmlPullParserException e) {
167
                        e.printStackTrace();
168
                        throw new GMLException(GMLException.EXC_PARSE);
169
                }
170
                return null;
171
                                
172
        }
173
        
174
        private Object parseSpecificFeature(String elementName) throws GMLException {
175
                int currentTag;
176
                boolean end = false;
177
                ArrayList params = new ArrayList();
178
                ArrayList values = new ArrayList();
179
                Hashtable duplicates = new Hashtable();
180
                Object geom = null;
181
                
182
                XMLElement element = XMLElementsFactory.getElement(elementName);
183
                if (element != null){
184
                        XMLComplexType elementType = (XMLComplexType)element.getType();
185
                        try{                
186
                                currentTag = getParser().next();                                        
187
                                while (!end){                        
188
                                        switch(currentTag){
189
                                        case KXmlParser.START_TAG:
190
                                                String attName = getParser().getName();
191
                                                XMLElement attribute = elementType.getElement(attName);
192
                                                if (attribute != null){
193
                                                        if (attribute.getType() != null){
194
                                                                if (attribute.getType().getType() == IXMLType.SIMPLE){
195
                                                                        if (duplicates.get(attName) == null){
196
                                                                                params.add(attName);
197
                                                                                duplicates.put(attName,attName);
198
                                                                                getParser().next();
199
                                                                                values.add(((XMLSimpleType)attribute.getType()).getObject(getParser().getText()));
200
                                                                        }
201
                                                                }else if (attribute.getType().getType() == IXMLType.GML_GEOMETRY){
202
                                                                        geom = GMLUtilsParser.parseGeometry(getParser(),attName,getFactory());
203
                                                                }else if (attribute.getType().getType() == IXMLType.COMPLEX){
204
                                                                        //Not in GML v2
205
                                                                }
206
                                                        }
207
                                                }
208
                                                break;
209
                                        case KXmlParser.END_TAG:
210
                                                if ((getParser().getName().compareTo(elementName) == 0))
211
                                                        end = true;
212
                                                break;
213
                                        case KXmlParser.TEXT:                   
214
                                                break;
215
                                        }
216
                                        if (!end){
217
                                                currentTag = getParser().next();
218
                                        }        
219
                                }
220
                        }catch (XmlPullParserException e) {
221
                                // TODO Auto-generated catch block
222
                                e.printStackTrace();
223
                                throw new GMLException(GMLException.EXC_PARSE);
224
                        } catch (IOException e) {
225
                                // TODO Auto-generated catch block
226
                                e.printStackTrace();
227
                                throw new GMLException(GMLException.EXC_READ_FILE);
228
                        }                 
229
                }
230
                return getFactory().createSimpleFeature(elementName,geom,params,values);
231
        }
232
        
233
}