Statistics
| Revision:

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

History | View | Annotate | Download (6.1 KB)

1
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.utils.CompareUtils;
11
import org.gvsig.gpe.gml.utils.GMLGeometries;
12
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
13
import org.gvsig.gpe.schema.exceptions.TypeNotFoundException;
14
import org.gvsig.gpe.schema.som.IXSElementDeclaration;
15
import org.xmlpull.v1.XmlPullParser;
16
import org.xmlpull.v1.XmlPullParserException;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
 *
20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
 *
22
 * This program is free software; you can redistribute it and/or
23
 * modify it under the terms of the GNU General Public License
24
 * as published by the Free Software Foundation; either version 2
25
 * of the License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
 *
36
 * For more information, contact:
37
 *
38
 *  Generalitat Valenciana
39
 *   Conselleria d'Infraestructures i Transport
40
 *   Av. Blasco Ib??ez, 50
41
 *   46010 VALENCIA
42
 *   SPAIN
43
 *
44
 *      +34 963862235
45
 *   gvsig@gva.es
46
 *      www.gvsig.gva.es
47
 *
48
 *    or
49
 *
50
 *   IVER T.I. S.A
51
 *   Salamanca 50
52
 *   46005 Valencia
53
 *   Spain
54
 *
55
 *   +34 963163400
56
 *   dac@iver.es
57
 */
58
/* CVS MESSAGES:
59
 *
60
 * $Id: FeatureTypeBinding.java 12071 2007-06-07 14:53:59Z jorpiell $
61
 * $Log$
62
 * Revision 1.6  2007-06-07 14:53:30  jorpiell
63
 * Add the schema support
64
 *
65
 * Revision 1.5  2007/05/18 10:41:01  csanchez
66
 * Actualizaci?n libGPE-GML eliminaci?n de clases inecesarias
67
 *
68
 * Revision 1.4  2007/05/15 12:10:01  jorpiell
69
 * The bbox is linked to the feature
70
 *
71
 * Revision 1.3  2007/05/15 09:43:48  jorpiell
72
 * The namespace is deleted from the feature name
73
 *
74
 * Revision 1.2  2007/05/15 09:35:09  jorpiell
75
 * the tag names cant have blanc spaces
76
 *
77
 * Revision 1.1  2007/05/14 09:30:30  jorpiell
78
 * Add the FeatureMember tag
79
 *
80
 *
81
 */
82
/**
83
 * This class parses the gml objects that has a 
84
 * gml:_Feature object type. The structure of the
85
 * properties that this type has is variable and depends
86
 * on its schema. Example:
87
 * <p>
88
 * <pre>
89
 * <code>
90
 * &lt;cit:cities&gt;
91
 * &lt;cit:the_geom&gt;
92
 * &lt;gml:Point srsName='0'&gt;
93
 * &lt;gml:coordinates&gt;-123.06999969482422,49.411192817494346&lt;/gml:coordinates&gt;
94
 * &lt;/gml:Point&gt;
95
 * &lt;/cit:the_geom&gt;
96
 * &lt;cit:NAME&gt;Vancouver&lt;/cit:NAME&gt;
97
 * &lt;cit:CAPITAL&gt;N&lt;/cit:CAPITAL&gt;
98
 * &lt;cit:PROV_NAME&gt;British Columbia&lt;/cit:PROV_NAME&gt;
99
 * &lt;/cit:cities&gt;
100
 * </code>
101
 * </pre>
102
 * </p>
103
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
104
 */
105
public class FeatureTypeBinding {        
106
        
107
        /**
108
         * It parses a feature
109
         * @param parser
110
         * The XML parser
111
         * @param handler
112
         * The GPE parser that contains the content handler and
113
         * the error handler
114
         * @return
115
         * A feature
116
         * @throws XmlPullParserException
117
         * @throws IOException
118
         */
119
        public static Object parse(XmlPullParser parser,GPEGmlParser handler) throws XmlPullParserException, IOException {
120
                boolean endFeature = false;
121
                int currentTag;                
122
                Object feature = null;        
123
                //Used to finish to parse the current feature member
124
                String featureRootType = parser.getName();
125

    
126
                Hashtable attributes = GMLUtilsParser.getAttributes(parser);                
127
                String fid = FeatureTypeBinding.getID(attributes);
128
                
129
                String featureName = featureRootType.substring(featureRootType.indexOf(":") + 1,featureRootType.length());                
130
                feature = handler.getContentHandler().startFeature(fid, GMLUtilsParser.removeBlancSymbol(featureName), null, null);
131
                
132
                //If the element doesn't has the type it register the warning
133
                IXSElementDeclaration featureElement = null;
134
                try {
135
                        featureElement = handler.getContentHandler().getSchemaMap().getElementDeclarationByName(featureRootType);
136
                } catch (TypeNotFoundException e) {
137
                        handler.getErrorHandler().addWarning(e);
138
                }
139
                
140
                String tag = parser.getName();
141
                currentTag = parser.getEventType();
142

    
143
                while (!endFeature){
144
                        switch(currentTag){
145
                        case XmlPullParser.START_TAG:
146
                                if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_NAME)){
147
                                        parser.next();
148
                                        handler.getContentHandler().addNameToFeature(parser.getText(), feature);
149
                                }else if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_DESCRIPTION)){
150
                                        parser.next();
151
                                        String description = parser.getText();
152
                                }else if (CompareUtils.compareWithOutNamespace(tag,GMLTags.GML_BOUNDEDBY)){
153
                                        Object bbox = BoundedByTypeBinding.parse(parser, handler);
154
                                        handler.getContentHandler().addBboxToFeature(bbox, feature);
155
                                }else{
156
                                        if (!(featureRootType.compareTo(tag) == 0)){                                                
157
                                                if (GMLGeometries.isGML(tag.substring(tag.indexOf(":") + 1, tag.length()))){
158
                                                        Object geometry = GeometryBinding.parse(parser, handler);
159
                                                        handler.getContentHandler().addGeometryToFeature(geometry, feature);
160
                                                }else{
161
                                                //Elements (elements or features)
162
                                                        Object element = ElementTypeBinding.parse(parser, handler, feature, null, featureElement);
163
                                                        handler.getContentHandler().addElementToFeature(element, feature);
164
                                                }
165
                                        }
166
                                }
167
                                break;
168
                        case XmlPullParser.END_TAG:
169
                                if (tag.compareTo(featureRootType) == 0){                                                
170
                                        endFeature = true;
171
                                        handler.getContentHandler().endFeature(feature);
172
                                }
173
                                break;
174
                        case XmlPullParser.TEXT:                                        
175

    
176
                                break;
177
                        }
178
                        if (!endFeature){                                        
179
                                currentTag = parser.next();
180
                                tag = parser.getName();
181
                        }
182
                }                        
183
                return feature;                
184
        }        
185
        
186
        /**
187
         * It returns a the feaure id attribute
188
         * @param hash
189
         * Hashtable with the XML attributes
190
         * @return
191
         * The id
192
         */
193
        public static String getID(Hashtable hash){
194
                Object obj = hash.get(GMLTags.GML_FID);
195
                if (obj != null){
196
                        return (String)obj;
197
                }
198
                return null;
199
        }        
200
}