Statistics
| Revision:

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

History | View | Annotate | Download (8.76 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.exceptions.GPEParserCreationException;
54
import org.gvsig.gpe.gml.GPEGmlSFP0Parser;
55
import org.gvsig.remoteClient.gml.exceptions.GMLException;
56
import org.gvsig.remoteClient.gml.schemas.XMLElement;
57
import org.gvsig.remoteClient.gml.types.XMLComplexType;
58
import org.gvsig.remoteClient.wfs.WFSClient;
59
import org.gvsig.remoteClient.wfs.WFSFeature;
60
import org.gvsig.remoteClient.wfs.WFSStatus;
61
import org.gvsig.remoteClient.wfs.filters.FilterEncoding;
62

    
63
import com.iver.utiles.swing.jcomboServer.ServerData;
64

    
65
import es.gva.cit.catalog.drivers.DiscoveryServiceCapabilities;
66
import es.gva.cit.catalog.utils.CatalogConstants;
67
import es.gva.cit.gazetteer.drivers.AbstractGazetteerServiceDriver;
68
import es.gva.cit.gazetteer.drivers.GazetteerCapabilities;
69
import es.gva.cit.gazetteer.querys.Feature;
70
import es.gva.cit.gazetteer.querys.FeatureType;
71
import es.gva.cit.gazetteer.querys.FeatureTypeAttribute;
72
import es.gva.cit.gazetteer.querys.GazetteerQuery;
73
import es.gva.cit.gazetteer.utils.GazetteerFormatter;
74

    
75
/**
76
 * Driver for the WFS protocol
77
 * @author Jorge Piera Llodra (piera_jor@gva.es)
78
 */
79
public class WFSServiceDriver extends AbstractGazetteerServiceDriver {
80
        protected WFSClient client = null;
81
        protected WFSStatus status = null;
82

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

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

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

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

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

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

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

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

    
252
        /*
253
         * (non-Javadoc)
254
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isProtocolSupported(java.net.URI)
255
         */
256
        public boolean isProtocolSupported(URI uri) {
257
                return true;
258
        }
259

    
260
        /*
261
         * (non-Javadoc)
262
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultPort()
263
         */
264
        public int getDefaultPort() {
265
                return 80;
266
        }
267

    
268
        /*
269
         * (non-Javadoc)
270
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getDefaultSchema()
271
         */
272
        public String getDefaultSchema() {
273
                return "http";
274
        }
275

    
276
        /*
277
         * (non-Javadoc)
278
         * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getServiceName()
279
         */
280
        public String getServiceName() {
281
                return ServerData.SERVER_SUBTYPE_GAZETTEER_WFS;
282
        }
283
}