Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appGazetteer / src / org / gvsig / gazetteer / wfs / drivers / WFSServiceDriver.java @ 34026

History | View | Annotate | Download (8.5 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 org.gvsig.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

    
51
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
52
import org.gvsig.catalog.utils.CatalogConstants;
53
import org.gvsig.gazetteer.drivers.AbstractGazetteerServiceDriver;
54
import org.gvsig.gazetteer.drivers.GazetteerCapabilities;
55
import org.gvsig.gazetteer.querys.Feature;
56
import org.gvsig.gazetteer.querys.FeatureType;
57
import org.gvsig.gazetteer.querys.FeatureTypeAttribute;
58
import org.gvsig.gazetteer.querys.GazetteerQuery;
59
import org.gvsig.gazetteer.utils.GazetteerFormatter;
60
import org.gvsig.gpe.lib.api.GPELocator;
61
import org.gvsig.gpe.lib.api.GPEManager;
62
import org.gvsig.i18n.Messages;
63
import org.gvsig.remoteclient.wfs.WFSClient;
64
import org.gvsig.remoteclient.wfs.WFSFeature;
65
import org.gvsig.remoteclient.wfs.WFSFeatureField;
66
import org.gvsig.remoteclient.wfs.WFSStatus;
67
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding;
68
import org.gvsig.utils.swing.jcomboServer.ServerData;
69

    
70

    
71

    
72
/**
73
 * Driver for the WFS protocol
74
 * @author Jorge Piera Llodra (piera_jor@gva.es)
75
 */
76
public class WFSServiceDriver extends AbstractGazetteerServiceDriver {
77
        protected WFSClient client = null;
78
        protected WFSStatus status = null;
79
        private static GPEManager gpeManager = GPELocator.getGPEManager();
80

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

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

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

    
151
        /*
152
         * (non-Javadoc)
153
         * @see es.gva.cit.gazetteer.drivers.AsbtractGazetteerServiceDriver#isDescribeFeatureTypeNeeded()
154
         */
155
        public boolean isDescribeFeatureTypeNeeded(){
156
                return true;
157
        }
158

    
159
        /**
160
         * Convert the feature attributes
161
         * @param feature
162
         * a Remote clients feature
163
         * @return
164
         * A list of attributes
165
         */
166
        private FeatureTypeAttribute[] covertFeatuteAttributes(WFSFeature feature) {
167
                FeatureTypeAttribute[] attributes = new FeatureTypeAttribute[feature.getFieldSize()];
168
                for (int i=0 ; i<feature.getFieldSize(); i++){
169
                        WFSFeatureField child = feature.getFieldAt(i);
170
                        attributes[i] = new FeatureTypeAttribute(child.getName(),0,false,child.getType());
171
                }
172
                return attributes;
173
        }
174

    
175
        /*
176
         * (non-Javadoc)
177
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getFeature(java.net.URI, es.gva.cit.gazetteer.querys.Query)
178
         */
179
        public Feature[] getFeature(URI uri, GazetteerQuery query) throws Exception {
180
                //Set the filter
181
                String sQuery = getFilterSQL(query);
182
                if (sQuery != null){
183
                        status.setFilterByAttribute(sQuery);
184
                }
185
                status.setFields(new String[0]);
186
                try{
187
                        File file = client.getFeature(status, false, null);
188
                        return parseOutputFile(file,query.getFieldAttribute());
189
                }catch(Exception e){
190
                        e.printStackTrace();
191
                        return new Feature[0];
192
                }
193
        }
194

    
195
        /**
196
         * Parses the GML file
197
         * @param file
198
         * @return
199
         * @throws GPEParserCreationException 
200
         * @throws URISyntaxException 
201
         */
202
        protected Feature[] parseOutputFile(File file, String fieldAttribute) throws Exception {
203
                URI uri = file.toURI();        
204
                WFSGPEErrorHandler errorHandler = new WFSGPEErrorHandler();
205
                WFSGPEContentHandler contentHandler = new WFSGPEContentHandler(fieldAttribute);
206
                gpeManager.parse(contentHandler, errorHandler, uri);
207
                ArrayList features = contentHandler.getFeatures();
208
                Feature[] auxFeatures = new Feature[features.size()];
209
                for (int i=0 ; i<features.size() ; i++){
210
                        auxFeatures[i] = (Feature)features.get(i);
211
                }
212
                return auxFeatures;
213
        }
214

    
215
        /**
216
         * Creates a SQL filter to do the search
217
         * @return
218
         * A standard SQL query
219
         */
220
        protected String getFilterSQL(GazetteerQuery query){
221
                StringBuffer buffer = new StringBuffer();
222
                if ((query.getName() == null) || (query.getName().equals(""))){
223
                        return null;
224
                }
225
                if (query.getNameFilter().equals(CatalogConstants.EXACT_WORDS)){
226
                        buffer.append("(" + query.getFieldAttribute() + " = " + query.getName() + ")");
227
                }else{
228
                        String conector = null;
229
                        if (query.getNameFilter().equals(CatalogConstants.ALL_WORDS)){
230
                                conector = "AND";
231
                        }else if (query.getNameFilter().equals(CatalogConstants.ANY_WORD)){
232
                                conector = "OR";
233
                        }
234
                        String[] words = query.getName().split(" ");
235
                        for (int i=0 ; i<words.length ; i++){
236
                                buffer.append("(" + query.getFieldAttribute() + " = " + words[i] + ")");
237
                                if (i  < words.length - 1){
238
                                        buffer.append(" " + conector + " ");
239
                                }
240
                        }
241
                }
242
                FilterEncoding fe = GazetteerFormatter.createFilter();
243
                fe.setQueryByAttribute(buffer.toString());
244
                return fe.toString();
245
        }
246

    
247
        /*
248
         * (non-Javadoc)
249
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isProtocolSupported(java.net.URI)
250
         */
251
        public boolean isProtocolSupported(URI uri) {
252
                return true;
253
        }
254

    
255
        /*
256
         * (non-Javadoc)
257
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultPort()
258
         */
259
        public int getDefaultPort() {
260
                return 80;
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultSchema()
266
         */
267
        public String getDefaultSchema() {
268
                return "http";
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getServiceName()
274
         */
275
        public String getServiceName() {
276
                return ServerData.SERVER_SUBTYPE_GAZETTEER_WFS;
277
        }
278
}