Statistics
| Revision:

root / branches / v1_9_0 / libGPE-GML / src / org / gvsig / gpe / gml / parser / v2 / geometries / GeometryBinding.java @ 53

History | View | Annotate | Download (11.1 KB)

1
package org.gvsig.gpe.gml.parser.v2.geometries;
2

    
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import javax.xml.namespace.QName;
8

    
9
import org.gvsig.gpe.gml.parser.GPEDefaultGmlParser;
10
import org.gvsig.gpe.gml.parser.profiles.IBindingProfile;
11
import org.gvsig.gpe.gml.utils.GMLProjectionFactory;
12
import org.gvsig.gpe.gml.utils.GMLTags;
13
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
14
import org.gvsig.gpe.parser.IGPEErrorHandler;
15
import org.gvsig.gpe.xml.stream.EventType;
16
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
17
import org.gvsig.gpe.xml.stream.XmlStreamException;
18
import org.gvsig.gpe.xml.utils.CompareUtils;
19
import org.gvsig.gpe.xml.utils.XMLAttributesIterator;
20

    
21
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
22
 *
23
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
24
 *
25
 * This program is free software; you can redistribute it and/or
26
 * modify it under the terms of the GNU General Public License
27
 * as published by the Free Software Foundation; either version 2
28
 * of the License, or (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
38
 *
39
 * For more information, contact:
40
 *
41
 *  Generalitat Valenciana
42
 *   Conselleria d'Infraestructures i Transport
43
 *   Av. Blasco Ib??ez, 50
44
 *   46010 VALENCIA
45
 *   SPAIN
46
 *
47
 *      +34 963862235
48
 *   gvsig@gva.es
49
 *      www.gvsig.gva.es
50
 *
51
 *    or
52
 *
53
 *   IVER T.I. S.A
54
 *   Salamanca 50
55
 *   46005 Valencia
56
 *   Spain
57
 *
58
 *   +34 963163400
59
 *   dac@iver.es
60
 */
61
/* CVS MESSAGES:
62
 *
63
 * $Id:GeometryBinding.java 195 2007-11-26 09:02:22Z jpiera $
64
 * $Log$
65
 * Revision 1.7  2007/05/24 07:29:47  csanchez
66
 * A?adidos Alias GML2
67
 *
68
 * Revision 1.6  2007/05/18 10:41:01  csanchez
69
 * Actualizaci?n libGPE-GML eliminaci?n de clases inecesarias
70
 *
71
 * Revision 1.5  2007/05/15 11:55:11  jorpiell
72
 * MultiGeometry is now supported
73
 *
74
 * Revision 1.4  2007/05/15 07:30:38  jorpiell
75
 * Add the geometryProperties tags
76
 *
77
 * Revision 1.3  2007/05/14 11:18:51  jorpiell
78
 * ProjectionFactory updated
79
 *
80
 * Revision 1.2  2007/05/14 09:31:06  jorpiell
81
 * Add the a new class to compare tags
82
 *
83
 * Revision 1.1  2007/05/08 10:24:16  jorpiell
84
 * Add comments to create javadocs
85
 *
86
 * Revision 1.1  2007/05/07 12:58:42  jorpiell
87
 * Add some methods to manage the multigeometries
88
 *
89
 * Revision 1.1  2007/05/07 07:06:46  jorpiell
90
 * Add a constructor with the name and the description fields
91
 *
92
 *
93
 */
94
/**
95
 * This class parses a geometry. It is also used 
96
 * to get the attributes for a GML geometry. 
97
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
98
 */
99
public class GeometryBinding {
100
        protected String srsName = null;
101
        protected String id = null;
102
        
103
        /**
104
         * It parses the gml:Geometry tag
105
         * @param parser
106
         * The XML parser
107
         * @param handler
108
         * The GPE parser that contains the content handler and
109
         * the error handler
110
         * @return
111
         * A geometry
112
         * @throws XmlStreamException
113
         * @throws IOException
114
         */
115
        public Object parse(IXmlStreamReader parser,GPEDefaultGmlParser handler) throws XmlStreamException, IOException {
116

    
117
        parser.require(EventType.START_ELEMENT, null, null);
118
                
119
            Object geometry = parseTag(parser, handler);
120
        
121
            //////parser.require(EventType.END_ELEMENT, null, null);
122
            return geometry;
123
        }
124
        
125
        /**
126
         * It parses an XML tag
127
         * @param parser
128
         * @param handler
129
         * @param tag
130
         * @return
131
         * @throws XmlStreamException
132
         * @throws IOException
133
         */
134
        protected Object parseTag(IXmlStreamReader parser, GPEDefaultGmlParser handler) throws XmlStreamException, IOException{
135
        parser.require(EventType.START_ELEMENT, null, null);
136
        final QName tag = parser.getElementName();
137
        Object geometry = null;
138
        // PROPERTIES
139
        final IBindingProfile profile = handler.getProfile();
140
        if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_GEOMETRYPROPERTY)) {
141
            geometry = profile.getGeometryPropertyTypeBinding().parse(parser, handler);
142
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POINTPROPERTY)) {
143
            geometry = profile.getPointPropertyTypeBinding().parse(parser, handler);
144
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_LINESTRINGPROPERTY)) {
145
            geometry = profile.getLineStringPropertyTypeBinding().parse(parser, handler);
146
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POLYGONPROPERTY)) {
147
            geometry = profile.getPolygonPropertyTypeBinding().parse(parser, handler);
148
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIPOINTPROPERTY)) {
149
            geometry = profile.getMultiPointPropertyTypeBinding().parse(parser, handler);
150
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTILINESTRINGPROPERTY)) {
151
            geometry = profile.getMultiLineStringPropertyTypeBinding().parse(parser, handler);
152
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIPOLYGONPROPERTY)) {
153
            geometry = profile.getMultiPolygonPropertyTypeBinding().parse(parser, handler);
154
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIGEOMETRYPROPERTY)) {
155
            geometry = profile.getMultiGeometryPropertyTypeBinding().parse(parser, handler);
156
        }
157
        // GEOMETRIES
158
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POINTMEMBER)) {
159
            geometry = profile.getPointMemberTypeBinding().parse(parser, handler);
160
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POINT)) {
161
            geometry = profile.getPointTypeBinding().parse(parser, handler);
162
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_LINESTRINGMEMBER)) {
163
            geometry = profile.getLineStringMemberTypeBinding().parse(parser, handler);
164
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_LINESTRING)) {
165
            geometry = profile.getLineStringTypeBinding().parse(parser, handler);
166
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_LINEARRING)) {
167
            geometry = profile.getLinearRingTypeBinding().parse(parser, handler);
168
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POLYGONMEMBER)) {
169
            geometry = profile.getPointMemberTypeBinding().parse(parser, handler);
170
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_POLYGON)) {
171
            geometry = profile.getPolygonTypeBinding().parse(parser, handler);
172
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_GEOMETRYMEMBER)) {
173
            geometry = profile.getGeometryMemberTypeBinding().parse(parser, handler);
174
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIPOINT)) {
175
            geometry = profile.getMultiPointTypeBinding().parse(parser, handler);
176
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTILINESTRING)) {
177
            geometry = profile.getMultiLineStringTypeBinding().parse(parser, handler);
178
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIPOLYGON)) {
179
            geometry = profile.getMultiPolygonTypeBinding().parse(parser, handler);
180
        } else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIGEOMETRY)) {
181
            geometry = profile.getMultiGeometryTypeBinding().parse(parser, handler);
182
        }
183
        // POINT PROPERTY ALIASES
184
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_LOCATION)
185
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_CENTEROF)
186
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_POSITION)) {
187
            geometry = profile.getPointTypeBinding().parse(parser, handler);
188
        }
189
        // LINESTRING PROPERTY ALIASES
190
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_EDGEOF)
191
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_CENTERLINEOF)) {
192
            geometry = profile.getLineStringPropertyTypeBinding().parse(parser, handler);
193
        }
194
        // POLYGON PROPERTY ALIASES
195
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_EXTENTOF)
196
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_COVERAGE)) {
197
            geometry = profile.getPolygonPropertyTypeBinding().parse(parser, handler);
198
        }
199
        // MULTIPOINT PROPERTY ALIASES
200
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTILOCATION)
201
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIPOSITION)) {
202
            geometry = profile.getMultiPointPropertyTypeBinding().parse(parser, handler);
203
        }
204
        // MULTILINESTRING PROPERTY ALIASES
205
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIEDGEOF)
206
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTICENTERLINEOF)) {
207
            geometry = profile.getMultiLineStringPropertyTypeBinding().parse(parser, handler);
208
        }
209
        // MULTIPOLYGON PROPERTY ALIASES
210
        else if (CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTIEXTENTOF)
211
                || CompareUtils.compareWithNamespace(tag, GMLTags.GML_MULTICOVERAGE)) {
212
            geometry = profile.getMultiPolygonTypeBinding().parse(parser, handler);
213
        } else {
214
            throw new IllegalStateException("Found no binding for element " + tag);
215
        }
216

    
217
        // make sure the parse method consumed the element fully
218
        ///////parser.require(EventType.END_ELEMENT, tag.getNamespaceURI(), tag.getLocalPart());
219
        return geometry;
220
        }
221
        
222
    /**
223
     * Save about 20% the geometry parsing time by cacheing the gml to GPE CRS names
224
     */
225
    private Map gmlToGPESrsNames = new HashMap();
226
        
227
        /**
228
         * This method has to be invoked by all the geometries
229
         * before to make the parse process. It retrieves
230
         * the common geometry attributes
231
         * @param parser
232
         * The root geometry tag
233
         * @param errorHandler
234
         * To add the errors
235
         * @throws XmlStreamException 
236
         */
237
        protected void setAtributtes(IXmlStreamReader parser, IGPEErrorHandler errorHandler) throws XmlStreamException{
238
                XMLAttributesIterator attributesIteartor = new XMLAttributesIterator(parser);
239
                Map attributes = attributesIteartor.getAttributes();
240
                Object objId = attributes.get(GMLTags.GML_GID);
241
                if (objId != null){
242
                        id = (String)objId;
243
                }else{
244
                        id = null;
245
                }
246
                Object objSrs = attributes.get(GMLTags.GML_SRS_NAME);
247
                if(objSrs == null){
248
                    objSrs = attributes.get(new QName(GMLTags.GML_SRS_NAME.getLocalPart()));
249
                }
250
                if (objSrs != null){
251
                    srsName = (String) gmlToGPESrsNames.get(objSrs);
252
                    if(srsName == null){
253
                        srsName = GMLProjectionFactory.fromGMLToGPE((String)objSrs, errorHandler);
254
                        gmlToGPESrsNames.put(objSrs, srsName);
255
                    }
256
                }else{
257
                        srsName = null;
258
                }
259
        }
260
        
261
        /**
262
         * It returns a the geometry id attribute
263
         * @return
264
         * The id
265
         */
266
        public String getID(){
267
                return id;
268
        }        
269

    
270
        /**
271
         * It returns a the geometry srs attribute
272
         * @return
273
         * The srs
274
         */
275
        public String getSrs(){
276
                return srsName;
277
        }
278

    
279
}