Revision 33738 branches/v2_0_0_prep/libraries/libRemoteServices/src/org/gvsig/remoteclient/ogc/OGCProtocolHandler.java

View differences:

OGCProtocolHandler.java
41 41
package org.gvsig.remoteclient.ogc;
42 42

  
43 43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.FileNotFoundException;
44 46
import java.io.IOException;
45 47

  
46
import org.gvsig.remoteclient.utils.CapabilitiesTags;
47 48
import org.kxml2.io.KXmlParser;
48 49
import org.xmlpull.v1.XmlPullParserException;
49 50

  
51
import org.gvsig.compat.CompatLocator;
52
import org.gvsig.compat.net.Downloader;
53
import org.gvsig.remoteclient.utils.CapabilitiesTags;
54

  
50 55
public abstract class OGCProtocolHandler {
51
	/**
52
	 * procotol handler name
53
	 */
56
    protected static final Downloader downloader = CompatLocator.getDownloader();
57
    
58
    /**
59
     * procotol handler name
60
     */
54 61
    protected String name;
55 62
    /**
56 63
     * protocol handler version
......
64 71
     *  port number of the comunication channel of the WMS to connect
65 72
     */
66 73
    protected String port;    
67
    
74

  
68 75
    /**
69
	 * @return Returns the host.
70
	 */
71
	public String getHost() {
72
		return host;
73
	}
74
	/**
75
	 * @param host The host to set.
76
	 */
77
	public void setHost(String host) {
78
		this.host = host;
79
	}
80
	/**
81
	 * @return Returns the name.
82
	 */
83
	public String getName() {
84
		return name;
85
	}
86
	/**
87
	 * @param name The name to set.
88
	 */
89
	public void setName(String name) {
90
		this.name = name;
91
	}
92
	/**
93
	 * @return Returns the port.
94
	 */
95
	public String getPort() {
96
		return port;
97
	}
98
	/**
99
	 * @param port The port to set.
100
	 */
101
	public void setPort(String port) {
102
		this.port = port;
103
	}
104
	/**
105
	 * @return Returns the version.
106
	 */
107
	public String getVersion() {
108
		return version;
109
	}
110
	/**
111
	 * @param version The version to set.
112
	 */
113
	public void setVersion(String version) {
114
		this.version = version;
115
	}
116
    
76
     * @return Returns the host.
77
     */
78
    public String getHost() {
79
        return host;
80
    }
117 81
    /**
82
     * @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
    /**
118 125
     * parses the data retrieved by the Capabilities XML document
119 126
     */
120 127
    public abstract boolean parseCapabilities(File f);
121
    
128

  
122 129
    public abstract OGCServiceInformation getServiceInformation();
123
		
124
	/**
125
	 * Just for not repeat code. Gets the correct separator according 
126
	 * to the server URL
127
	 * @param h
128
	 * @return
129
	 */
130
	protected static String getSymbol(String h) {
131
		String symbol;
132
		if (h.indexOf("?")==-1) 
133
			symbol = "?";
134
		else if (h.indexOf("?")!=h.length()-1)
135
			symbol = "&";
136
		else
137
			symbol = "";
138
		return symbol;
139
	}  
140
	
141
	/**
142
	 * Parse an operation into a DcpType tag
143
	 * @param parser
144
	 * The KXMLParser
145
	 * @param operation
146
	 * The WFS operation to parse
147
	 * @throws IOException 
148
	 * @throws XmlPullParserException 
149
	 */
150
	protected void parserDcpType(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
151
		int currentTag;
152
		boolean end = false;
153 130

  
154
		currentTag = parser.next();
131
    /**
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
    }  
155 147

  
156
		while (!end) 
157
		{
158
			switch(currentTag)
159
			{
160
			case KXmlParser.START_TAG:
161
				if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0){
162
					parseHTTPTag(parser, operation);
163
				}	         
164
				break;
165
			case KXmlParser.END_TAG:
166
				if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE) == 0)
167
					end = true;
168
				break;
169
			case KXmlParser.TEXT:                   
170
				break;
171
			}
172
			if (!end){
173
				currentTag = parser.next();
174
			}
175
		}     
176
	}
148
    /**
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;
177 160

  
178
	/**
179
	 * Parse an operation into a HTTP tag
180
	 * @param parser
181
	 * The KXMLParser
182
	 * @param operation
183
	 * The WFS operation to parse
184
	 * @throws IOException 
185
	 * @throws XmlPullParserException 
186
	 */
187
	protected void parseHTTPTag(KXmlParser parser, String operation) throws XmlPullParserException, IOException {        
188
		int currentTag;
189
		boolean end = false;
161
        currentTag = parser.next();
190 162

  
191
		currentTag = parser.next();
192
		int protocol = -1;
193
		
194
		while (!end) 
195
		{
196
			switch(currentTag)
197
			{
198
			case KXmlParser.START_TAG:
199
				String value = null;
200
				if(parser.getName().compareTo(CapabilitiesTags.GET)==0){
201
					protocol = OGCClientOperation.PROTOCOL_GET;
202
					addOperationByAttribute(parser, operation, protocol);
203
				}else if(parser.getName().compareTo(CapabilitiesTags.POST)==0){
204
					protocol = OGCClientOperation.PROTOCOL_POST;
205
					addOperationByAttribute(parser, operation, protocol);
206
				}else if(parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0){
207
					addOperationByAttribute(parser, operation, protocol);
208
				}				
209
				break;
210
			case KXmlParser.END_TAG:
211
				if (parser.getName().compareTo(CapabilitiesTags.HTTP) == 0)
212
					end = true;
213
				break;
214
			case KXmlParser.TEXT:                   
215
				break;
216
			}
217
			if (!end){
218
				currentTag = parser.next();
219
			}
220
		}     
221
	}
222
	
223
	/**
224
	 * Add an operation and the online resource 
225
	 * @param parser
226
	 * The parser
227
	 * @param operation
228
	 * The operation to add
229
	 * @param protocol
230
	 * The parser to add
231
	 */
232
	protected void addOperationByAttribute(KXmlParser parser, String operation, int protocol){
233
		String value = null;
234
		if (protocol > -1){
235
			for (int i=0 ; i<parser.getAttributeCount() ; i++){
236
				if ((parser.getAttributeName(i).toUpperCase().compareTo(CapabilitiesTags.ONLINERESOURCE.toUpperCase()) == 0) ||
237
						(parser.getAttributeName(i).equals(CapabilitiesTags.XLINK_HREF))){					
238
					value = parser.getAttributeValue(i);
239
				}
240
			}								
241
			if (value == null){
242
				getServiceInformation().addOperation(operation, protocol);
243
			}else{
244
				getServiceInformation().addOperation(operation, protocol, value);
245
			}
246
		}  
247
	}
163
        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
    }
248 288
}

Also available in: Unified diff