Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / ogc / OGCProtocolHandler.java @ 33910

History | View | Annotate | Download (8.01 KB)

1 29669 jpiera
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.remoteclient.ogc;
42
43
import java.io.File;
44 33738 jpiera
import java.io.FileInputStream;
45
import java.io.FileNotFoundException;
46 29669 jpiera
import java.io.IOException;
47
48
import org.kxml2.io.KXmlParser;
49
import org.xmlpull.v1.XmlPullParserException;
50
51 33738 jpiera
import org.gvsig.compat.CompatLocator;
52
import org.gvsig.compat.net.Downloader;
53
import org.gvsig.remoteclient.utils.CapabilitiesTags;
54
55 29669 jpiera
public abstract class OGCProtocolHandler {
56 33738 jpiera
    protected static final Downloader downloader = CompatLocator.getDownloader();
57
58
    /**
59
     * procotol handler name
60
     */
61 29669 jpiera
    protected String name;
62
    /**
63
     * protocol handler version
64
     */
65
    protected String version;
66
    /**
67
     * host of the WMS to connect
68
     */
69
    protected String host;
70
    /**
71
     *  port number of the comunication channel of the WMS to connect
72
     */
73
    protected String port;
74 33738 jpiera
75 29669 jpiera
    /**
76 33738 jpiera
     * @return Returns the host.
77
     */
78
    public String getHost() {
79
        return host;
80
    }
81 29669 jpiera
    /**
82 33738 jpiera
     * @param host The host to set.
83
     */
84
    public void setHost(String host) {
85
        this.host = host;
86
    }
87
    /**
88
     * @return Returns the name.
89
     */
90
    public String getName() {
91
        return name;
92
    }
93
    /**
94
     * @param name The name to set.
95
     */
96
    public void setName(String name) {
97
        this.name = name;
98
    }
99
    /**
100
     * @return Returns the port.
101
     */
102
    public String getPort() {
103
        return port;
104
    }
105
    /**
106
     * @param port The port to set.
107
     */
108
    public void setPort(String port) {
109
        this.port = port;
110
    }
111
    /**
112
     * @return Returns the version.
113
     */
114
    public String getVersion() {
115
        return version;
116
    }
117
    /**
118
     * @param version The version to set.
119
     */
120
    public void setVersion(String version) {
121
        this.version = version;
122
    }
123
124
    /**
125 29669 jpiera
     * parses the data retrieved by the Capabilities XML document
126
     */
127
    public abstract boolean parseCapabilities(File f);
128 33738 jpiera
129 29669 jpiera
    public abstract OGCServiceInformation getServiceInformation();
130
131 33738 jpiera
    /**
132
     * Just for not repeat code. Gets the correct separator according
133
     * to the server URL
134
     * @param h
135
     * @return
136
     */
137
    protected static String getSymbol(String h) {
138
        String symbol;
139
        if (h.indexOf("?")==-1)
140
            symbol = "?";
141
        else if (h.indexOf("?")!=h.length()-1)
142
            symbol = "&";
143
        else
144
            symbol = "";
145
        return symbol;
146
    }
147 29669 jpiera
148 33738 jpiera
    /**
149
     * Parse an operation into a DcpType tag
150
     * @param parser
151
     * The KXMLParser
152
     * @param operation
153
     * The WFS operation to parse
154
     * @throws IOException
155
     * @throws XmlPullParserException
156
     */
157
    protected void parserDcpType(KXmlParser parser, String operation) throws XmlPullParserException, IOException {
158
        int currentTag;
159
        boolean end = false;
160 29669 jpiera
161 33738 jpiera
        currentTag = parser.next();
162 29669 jpiera
163 33738 jpiera
        while (!end)
164
        {
165
            switch(currentTag)
166
            {
167
            case KXmlParser.START_TAG:
168
                if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0){
169
                    parseHTTPTag(parser, operation);
170
                }
171
                break;
172
            case KXmlParser.END_TAG:
173
                if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE) == 0)
174
                    end = true;
175
                break;
176
            case KXmlParser.TEXT:
177
                break;
178
            }
179
            if (!end){
180
                currentTag = parser.next();
181
            }
182
        }
183
    }
184
185
    /**
186
     * Parse an operation into a HTTP tag
187
     * @param parser
188
     * The KXMLParser
189
     * @param operation
190
     * The WFS operation to parse
191
     * @throws IOException
192
     * @throws XmlPullParserException
193
     */
194
    protected void parseHTTPTag(KXmlParser parser, String operation) throws XmlPullParserException, IOException {
195
        int currentTag;
196
        boolean end = false;
197
198
        currentTag = parser.next();
199
        int protocol = -1;
200
201
        while (!end)
202
        {
203
            switch(currentTag)
204
            {
205
            case KXmlParser.START_TAG:
206
                String value = null;
207
                if(parser.getName().compareTo(CapabilitiesTags.GET)==0){
208
                    protocol = OGCClientOperation.PROTOCOL_GET;
209
                    addOperationByAttribute(parser, operation, protocol);
210
                }else if(parser.getName().compareTo(CapabilitiesTags.POST)==0){
211
                    protocol = OGCClientOperation.PROTOCOL_POST;
212
                    addOperationByAttribute(parser, operation, protocol);
213
                }else if(parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0){
214
                    addOperationByAttribute(parser, operation, protocol);
215
                }
216
                break;
217
            case KXmlParser.END_TAG:
218
                if (parser.getName().compareTo(CapabilitiesTags.HTTP) == 0)
219
                    end = true;
220
                break;
221
            case KXmlParser.TEXT:
222
                break;
223
            }
224
            if (!end){
225
                currentTag = parser.next();
226
            }
227
        }
228
    }
229
230
    /**
231
     * Add an operation and the online resource
232
     * @param parser
233
     * The parser
234
     * @param operation
235
     * The operation to add
236
     * @param protocol
237
     * The parser to add
238
     */
239
    protected void addOperationByAttribute(KXmlParser parser, String operation, int protocol){
240
        String value = null;
241
        if (protocol > -1){
242
            for (int i=0 ; i<parser.getAttributeCount() ; i++){
243
                if ((parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0) ||
244
                    (parser.getAttributeName(i).equals(CapabilitiesTags.XLINK_HREF))){
245
                    value = parser.getAttributeValue(i);
246
                }
247
            }
248
            if (value == null){
249
                getServiceInformation().addOperation(operation, protocol);
250
            }else{
251
                getServiceInformation().addOperation(operation, protocol, value);
252
            }
253
        }
254
    }
255
256
    /**
257
     * Copy the file in a byte array
258
     * @param file
259
     * The file to copy
260
     * @return
261
     * An array of bytes
262
     * @throws IOException
263
     */
264
    protected byte[] fileToBytes(File file) throws IOException{
265
        FileInputStream fis = null;
266
        byte[] bytes = null;
267
        try{
268
            fis = new FileInputStream(file);
269
270
            long length = file.length();
271
            bytes = new byte[(int)file.length()];
272
273
            int offset = 0;
274
            int numRead = 0;
275
            while (offset < bytes.length && (numRead=fis.read(bytes, offset, bytes.length-offset)) >= 0)
276
            {
277
                offset += numRead;
278
            }
279
        }catch (IOException e) {
280
            throw e;
281
        }finally{
282
            if (fis != null){
283
                fis.close();
284
            }
285
        }
286
        return bytes;
287
    }
288 29669 jpiera
}