Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / gml / bindings / FeatureTypeBinding.java @ 11641

History | View | Annotate | Download (4.99 KB)

1 11604 jorpiell
package org.gvsig.gpe.gml.bindings;
2
3
import java.io.IOException;
4
import java.util.Hashtable;
5
6
import org.gvsig.gpe.gml.GMLTags;
7
import org.gvsig.gpe.gml.GPEGmlParser;
8
import org.gvsig.gpe.gml.bindings.geometries.BoundedByTypeBinding;
9
import org.gvsig.gpe.gml.bindings.geometries.GeometryBinding;
10
import org.gvsig.gpe.gml.types.GMLGeometries;
11
import org.gvsig.gpe.gml.utils.CompareUtils;
12
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
13
import org.xmlpull.v1.XmlPullParser;
14
import org.xmlpull.v1.XmlPullParserException;
15
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id$
59
 * $Log$
60
 * Revision 1.1  2007-05-14 09:30:30  jorpiell
61
 * Add the FeatureMember tag
62
 *
63
 *
64
 */
65
/**
66
 * This class parses the gml objects that has a
67
 * gml:_Feature object type. The structure of the
68
 * properties that this type has is variable and depends
69
 * on its schema. Example:
70
 * <p>
71
 * <pre>
72
 * <code>
73
 * &lt;cit:cities&gt;
74
 * &lt;cit:the_geom&gt;
75
 * &lt;gml:Point srsName='0'&gt;
76
 * &lt;gml:coordinates&gt;-123.06999969482422,49.411192817494346&lt;/gml:coordinates&gt;
77
 * &lt;/gml:Point&gt;
78
 * &lt;/cit:the_geom&gt;
79
 * &lt;cit:NAME&gt;Vancouver&lt;/cit:NAME&gt;
80
 * &lt;cit:CAPITAL&gt;N&lt;/cit:CAPITAL&gt;
81
 * &lt;cit:PROV_NAME&gt;British Columbia&lt;/cit:PROV_NAME&gt;
82
 * &lt;/cit:cities&gt;
83
 * </code>
84
 * </pre>
85
 * </p>
86
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
87
 */
88
public class FeatureTypeBinding {
89
90
        /**
91
         * It parses a feature
92
         * @param parser
93
         * The XML parser
94
         * @param handler
95
         * The GPE parser that contains the content handler and
96
         * the error handler
97
         * @return
98
         * A feature
99
         * @throws XmlPullParserException
100
         * @throws IOException
101
         */
102
        public static Object parse(XmlPullParser parser,GPEGmlParser handler) throws XmlPullParserException, IOException {
103
                boolean endFeature = false;
104
                int currentTag;
105
                Object feature = null;
106
                //Used to finish to parse the current feature member
107
                String featureRootType = parser.getName();
108
109
                Hashtable attributes = GMLUtilsParser.getAttributes(parser);
110
                String fid = FeatureTypeBinding.getID(attributes);
111
112
                feature = handler.getContentHandler().startFeature(fid, null, null);
113
114
                String tag = parser.getName();
115
                currentTag = parser.getEventType();
116
117
                while (!endFeature){
118
                        switch(currentTag){
119
                        case XmlPullParser.START_TAG:
120
                                if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_NAME)){
121
                                        parser.next();
122
                                        handler.getContentHandler().addNameToFeature(parser.getText(), feature);
123
                                }else if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_DESCRIPTION)){
124
                                        parser.next();
125
                                        String description = parser.getText();
126
                                }else if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_BOUNDEDBY)){
127
                                        Object bbox = BoundedByTypeBinding.parse(parser, handler);
128
                                }else{
129
                                        if (!(featureRootType.compareTo(tag) == 0)){
130
                                                if (GMLGeometries.isGML(tag.substring(tag.indexOf(":") + 1, tag.length()))){
131
                                                        Object geometry = GeometryBinding.parse(parser, handler);
132
                                                        handler.getContentHandler().addGeometryToFeature(geometry, feature);
133
                                                }else{
134
                                                //Elements (elements or features)
135
                                                        Object element = ElementTypeBinding.parse(parser, handler, feature, null);
136
                                                        handler.getContentHandler().addElementToFeature(element, feature);
137
                                                }
138
                                        }
139
                                }
140
                                break;
141
                        case XmlPullParser.END_TAG:
142
                                if (tag.compareTo(featureRootType) == 0){
143
                                        endFeature = true;
144
                                        handler.getContentHandler().endFeature(feature);
145
                                }
146
                                break;
147
                        case XmlPullParser.TEXT:
148
149
                                break;
150
                        }
151
                        if (!endFeature){
152
                                currentTag = parser.next();
153
                                tag = parser.getName();
154
                        }
155
                }
156
157
                return feature;
158
        }
159
160
        /**
161
         * It returns a the feaure id attribute
162
         * @param hash
163
         * Hashtable with the XML attributes
164
         * @return
165
         * The id
166
         */
167
        private static String getID(Hashtable hash){
168
                Object obj = hash.get(GMLTags.GML_FID);
169
                if (obj != null){
170
                        return (String)obj;
171
                }
172
                return null;
173
        }
174
}