Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appCatalogAndGazetteerClient / src / es / gva / cit / gazetteer / wfs / drivers / WFSServiceDriver.java @ 18851

History | View | Annotate | Download (9 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
package es.gva.cit.gazetteer.wfs.drivers;
43
import java.io.File;
44
import java.net.MalformedURLException;
45
import java.net.URI;
46
import java.net.URISyntaxException;
47
import java.util.ArrayList;
48
import java.util.Hashtable;
49
import java.util.Iterator;
50
import java.util.Vector;
51

    
52
import org.gvsig.gpe.GPEParser;
53
import org.gvsig.gpe.GPERegister;
54
import org.gvsig.gpe.exceptions.GPEParserCreationException;
55
import org.gvsig.gpe.gml.GPEGmlSFP0Parser;
56
import org.gvsig.gpe.writers.GPEGeometryWithIdTest;
57
import org.gvsig.remoteClient.gml.GMLReader;
58
import org.gvsig.remoteClient.gml.IGMLFeaturesIterator;
59
import org.gvsig.remoteClient.gml.exceptions.GMLException;
60
import org.gvsig.remoteClient.gml.schemas.XMLElement;
61
import org.gvsig.remoteClient.gml.types.XMLComplexType;
62
import org.gvsig.remoteClient.wfs.WFSClient;
63
import org.gvsig.remoteClient.wfs.WFSFeature;
64
import org.gvsig.remoteClient.wfs.WFSStatus;
65
import org.gvsig.remoteClient.wfs.filters.FilterEncoding;
66

    
67
import com.iver.utiles.swing.jcomboServer.ServerData;
68

    
69
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
70
import es.gva.cit.catalog.utils.CatalogConstants;
71
import es.gva.cit.gazetteer.drivers.AbstractGazetteerServiceDriver;
72
import es.gva.cit.gazetteer.drivers.GazetteerCapabilities;
73
import es.gva.cit.gazetteer.querys.Feature;
74
import es.gva.cit.gazetteer.querys.FeatureType;
75
import es.gva.cit.gazetteer.querys.FeatureTypeAttribute;
76
import es.gva.cit.gazetteer.querys.GazetteerQuery;
77
import es.gva.cit.gazetteer.utils.GazetteerFormatter;
78
import es.gva.cit.gazetteer.utils.GazetteerGeometriesFactory;
79

    
80
/**
81
 * Driver for the WFS protocol
82
 * @author Jorge Piera Llodra (piera_jor@gva.es)
83
 */
84
public class WFSServiceDriver extends AbstractGazetteerServiceDriver {
85
        protected WFSClient client = null;
86
        protected WFSStatus status = null;
87

    
88
        /*
89
         * (non-Javadoc)
90
         * @see es.gva.cit.catalogClient.drivers.IDiscoveryServiceDriver#getCapabilities(java.net.URI)
91
         */
92
        public DiscoveryServiceCapabilities getCapabilities(URI uri) {
93
                GazetteerCapabilities capabilities = new GazetteerCapabilities();
94
                String sURL = null;
95
                try {
96
                        sURL = uri.toURL().toString();
97
                } catch (MalformedURLException e) {
98
                        setServerAnswerReady("errorServerNotFound");
99
                        return null;
100
                }
101
                status = new WFSStatus(sURL);
102
                try {
103
                        client = new WFSClient(sURL);
104
                        client.getCapabilities(status, true, null);
105
                } catch (Exception e) {
106
                        capabilities.setServerMessage(e.toString());
107
                        return capabilities;
108
                }
109
                setServerAnswerReady(client.getServiceInformation().name);
110
                Hashtable features = client.getFeatures();
111
                setFeatureTypes(convertFeatureNames(features));
112
                capabilities.setServerMessage(client.getServiceInformation().name +
113
                                client.getServiceInformation().abstr);
114
                capabilities.setFeatureTypes(getFeatureTypes());
115
                return capabilities;
116
        }
117

    
118
        /**
119
         * Convert the features from the remote services format to
120
         * the gazetteer format
121
         * @param features
122
         * @return
123
         */
124
        private FeatureType[] convertFeatureNames(Hashtable features) {
125
                Iterator it = features.keySet().iterator();
126
                FeatureType[] featureTypes = new FeatureType[features.size()];
127
                int i = 0;
128
                while (it.hasNext()){
129
                        String featureName = (String)it.next();
130
                        WFSFeature feature = (WFSFeature)features.get(featureName);
131
                        featureTypes[i] = new FeatureType(featureName);
132
                        featureTypes[i].setTitle(feature.getTitle());
133
                        i++;
134
                }
135
                return featureTypes;
136
        }
137

    
138
        /*
139
         * (non-Javadoc)
140
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#describeFeatureType(java.net.URI, java.lang.String)
141
         */
142
        public FeatureTypeAttribute[] describeFeatureType(URI uri, String feature) {
143
                status.setFeatureName(feature);
144
                try{
145
                        client.describeFeatureType(status, false, null);
146
                }catch (Exception e){
147
                        //Impossible to retrieve the attributes
148
                        return new FeatureTypeAttribute[0];
149
                }
150
                WFSFeature wfsFeature = (WFSFeature)client.getFeatures().get(feature);
151
                if ((wfsFeature.getSrs() != null) && (wfsFeature.getSrs().size() > 0)){
152
                        this.setProjection((String)wfsFeature.getSrs().get(0));
153
                }
154
                return covertFeatuteAttributes(wfsFeature);
155
        }
156

    
157
        /*
158
         * (non-Javadoc)
159
         * @see es.gva.cit.gazetteer.drivers.AsbtractGazetteerServiceDriver#isDescribeFeatureTypeNeeded()
160
         */
161
        public boolean isDescribeFeatureTypeNeeded(){
162
                return true;
163
        }
164

    
165
        /**
166
         * Convert the feature attributes
167
         * @param feature
168
         * a Remote clients feature
169
         * @return
170
         * A list of attributes
171
         */
172
        private FeatureTypeAttribute[] covertFeatuteAttributes(WFSFeature feature) {
173
                XMLElement element = (XMLElement)feature.getFields().get(0);
174
                XMLComplexType type = (XMLComplexType)element.getEntityType();
175
                Vector fields = type.getAttributes();
176
                FeatureTypeAttribute[] attributes = new FeatureTypeAttribute[fields.size()];
177
                for (int i=0 ; i<fields.size(); i++){
178
                        XMLElement child = (XMLElement)fields.get(i);
179
                        attributes[i] = new FeatureTypeAttribute(child.getName(),0,false,child.getEntityType().getName());
180
                }
181
                return attributes;
182
        }
183

    
184
        /*
185
         * (non-Javadoc)
186
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getFeature(java.net.URI, es.gva.cit.gazetteer.querys.Query)
187
         */
188
        public Feature[] getFeature(URI uri, GazetteerQuery query) throws GMLException {
189
                //Set the filter
190
                String sQuery = getFilterSQL(query);
191
                if (sQuery != null){
192
                        status.setFilterQuery(sQuery);
193
                }
194
                status.setFields(new String[0]);
195
                try{
196
                        File file = client.getFeature(status, false, null);
197
                        return parseOutputFile(file,query.getFieldAttribute());
198
                }catch(Exception e){
199
                        e.printStackTrace();
200
                        return new Feature[0];
201
                }
202
        }
203

    
204
        /**
205
         * Parses the GML file
206
         * @param file
207
         * @return
208
         * @throws GPEParserCreationException 
209
         * @throws URISyntaxException 
210
         */
211
        protected Feature[] parseOutputFile(File file, String fieldAttribute) throws URISyntaxException, GPEParserCreationException {
212
                URI uri = file.toURI();
213
                GPEParser parser = new GPEGmlSFP0Parser(null,null);
214
                WFSGPEErrorHandler errorHandler = new WFSGPEErrorHandler();
215
                WFSGPEContentHandler contentHandler = new WFSGPEContentHandler(fieldAttribute);
216
                parser.parse(contentHandler, errorHandler, uri);
217
                ArrayList features = contentHandler.getFeatures();
218
                Feature[] auxFeatures = new Feature[features.size()];
219
                for (int i=0 ; i<features.size() ; i++){
220
                        auxFeatures[i] = (Feature)features.get(i);
221
                }
222
                return auxFeatures;
223
        }
224

    
225
        /**
226
         * Creates a SQL filter to do the search
227
         * @return
228
         * A standard SQL query
229
         */
230
        protected String getFilterSQL(GazetteerQuery query){
231
                StringBuffer buffer = new StringBuffer();
232
                if ((query.getName() == null) || (query.getName().equals(""))){
233
                        return null;
234
                }
235
                if (query.getNameFilter().equals(CatalogConstants.EXACT_WORDS)){
236
                        buffer.append("(" + query.getFieldAttribute() + " = " + query.getName() + ")");
237
                }else{
238
                        String conector = null;
239
                        if (query.getNameFilter().equals(CatalogConstants.ALL_WORDS)){
240
                                conector = "AND";
241
                        }else if (query.getNameFilter().equals(CatalogConstants.ANY_WORD)){
242
                                conector = "OR";
243
                        }
244
                        String[] words = query.getName().split(" ");
245
                        for (int i=0 ; i<words.length ; i++){
246
                                buffer.append("(" + query.getFieldAttribute() + " = " + words[i] + ")");
247
                                if (i  < words.length - 1){
248
                                        buffer.append(" " + conector + " ");
249
                                }
250
                        }
251
                }
252
                FilterEncoding fe = GazetteerFormatter.createFilter();
253
                fe.setQuery(buffer.toString());
254
                return fe.toString();
255
        }
256

    
257
        /*
258
         * (non-Javadoc)
259
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isProtocolSupported(java.net.URI)
260
         */
261
        public boolean isProtocolSupported(URI uri) {
262
                return true;
263
        }
264

    
265
        /*
266
         * (non-Javadoc)
267
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultPort()
268
         */
269
        public int getDefaultPort() {
270
                return 80;
271
        }
272

    
273
        /*
274
         * (non-Javadoc)
275
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultSchema()
276
         */
277
        public String getDefaultSchema() {
278
                return "http";
279
        }
280

    
281
        /*
282
         * (non-Javadoc)
283
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getServiceName()
284
         */
285
        public String getServiceName() {
286
                return ServerData.SERVER_SUBTYPE_GAZETTEER_WFS;
287
        }
288
}