Revision 5232

View differences:

tags/Root_MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSLayer.java
1
package org.gvsig.remoteClient.wms;
2

  
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.Hashtable;
6
import java.util.TreeMap;
7
import java.util.Vector;
8

  
9
import org.gvsig.remoteClient.utils.BoundaryBox;
10
import org.gvsig.remoteClient.utils.CapabilitiesTags;
11
import org.kxml2.io.KXmlParser;
12
import org.xmlpull.v1.XmlPullParserException;
13

  
14
/**
15
 * <p>Abstract class that defines an WMSLayer.</p>
16
 * 
17
 */
18
public abstract class WMSLayer implements org.gvsig.remoteClient.ILayer {
19
    
20
    protected ArrayList children;
21
    protected WMSLayer parent;
22
    
23
    /**
24
     * <p>Layer Abstract field in the capabilities document </p> 
25
     */
26
    private String layerAbstract;
27
    
28
    /**
29
     * <p>Themes provided by the WMS for the layer</p> 
30
     */
31
    public ArrayList styles = new ArrayList();
32
    
33
    /**
34
     * <p>Layer name</p>
35
     */
36
    private String name;
37
    
38
    /**
39
     * <p>Layer title</p>
40
     */
41
    private String title;
42
    
43
    private ArrayList keywordList = new ArrayList();
44
    /**
45
     * <p>Layer srs.</p>
46
     */
47
    protected Vector srs = new Vector();
48
    
49
    /**
50
     * <p>extents for each srs the layer can be reproyected to</p>
51
     */
52
    private Hashtable bBoxes  = new Hashtable();
53
    
54
    /**
55
     * <p>extents that defines the bbox for the LatLon projection
56
     * It can be included in the bBoxes vector as well, because it is the most used, we keep it separeted too, according
57
     *  with the OGC WMSCapabilities specifications...
58
     */    
59
    private org.gvsig.remoteClient.utils.BoundaryBox latLonBbox;
60
    
61
    /**
62
     * <p>min scale for the layer to be visible</p>
63
     */
64
    private double scaleMin;
65
    
66
    /**
67
     * <p>max scale for the layer to be visible</p>
68
     */
69
    private double scaleMax;
70
    
71
    /**
72
     * <p>Dimensions defined for the layer in the capabilities doc</p>
73
     */
74
    protected java.util.ArrayList dimensions = new ArrayList();
75
    
76
    /**
77
     * Tells if this layer accepts getFeatureInfo requests.
78
     */
79
    private boolean queryable = false;
80
    
81
    /**
82
     * Tells if this layer is opaque.
83
     */
84
    private boolean opaque = false;
85
    /**
86
     * when set to true, noSubsets indicates that the server is not able to make a map
87
     * of a geographic area other than the layer's bounding box.
88
     */
89
    private boolean m_noSubSets = false;
90
    
91
    /**
92
     * when present and non-zero fixedWidth and fixedHeight indicate that the server is not
93
     * able to produce a map of the layer at a width and height different from the fixed sizes indicated. 
94
     */
95
    private int fixedWidth = 0;
96
    private int fixedHeight = 0;
97
    
98
    /**
99
     * Tells if this layer can be served with transparency.
100
     */
101
    private boolean transparency;
102
    
103
    /**
104
     * <p>Parses the LAYER tag in the WMS capabilities, filling the WMSLayer object
105
     * loading the data in memory to be easily accesed</p>
106
     * 
107
     */
108
    public abstract void parse(KXmlParser parser, TreeMap layerTreeMap)   
109
    throws IOException, XmlPullParserException;    
110
    
111
    //public abstract ArrayList getAllDimensions();
112
    
113
    /**
114
     * add a new keyword to the keywordList.
115
     * @param key
116
     */
117
    protected void addkeyword(String key)
118
    {
119
    	keywordList.add(key);    	
120
    }
121
    public ArrayList getKeywords()
122
    {
123
    	return keywordList;
124
    }
125
    /**
126
     * <p>Adds a style to the styles vector</p>
127
     * @param _style 
128
     */
129
    public void addStyle(org.gvsig.remoteClient.wms.WMSStyle _style) {        
130
        styles.add( _style );    } 
131
    
132
   /**
133
     * <p>Gets the style vector</p> 
134
     * @return 
135
     */
136
    public ArrayList getStyles() {  
137
    	ArrayList list = new ArrayList();
138
    	if (styles != null)
139
    		list.addAll(styles);
140
    	if (this.getParent()!= null)
141
    	{
142
    		//return getAllStyles(this);
143
    		if(this.getParent().getStyles() != null)
144
    			list.addAll(this.getParent().getStyles());
145
    	}
146
        return list;
147
    } 
148
    
149
    public ArrayList getAllStyles(WMSLayer layer)
150
    {
151
    	if (layer.getParent()!= null)
152
    	{
153
    		ArrayList list = getAllStyles(layer.getParent());
154
    		for(int i = 0; i < this.styles.size(); i++)
155
    		{
156
    			list.add(styles.get(i));
157
    		}
158
    		return list;
159
    	}
160
    	else
161
    	{
162
    		return styles;
163
    	}
164
    }
165
    /**
166
     * <p>Adds a bbox to the Bboxes vector</p>
167
     * @param bbox
168
     */
169
    public void addBBox(BoundaryBox bbox) {
170
        bBoxes.put(bbox.getSrs(), bbox);
171
    } 
172
    
173
    /**
174
     * <p>returns the bbox with that id in the Bboxes vector</p> 
175
     * @param id 
176
     */
177
    public BoundaryBox getBbox(String id) {
178
    	if ((id.compareToIgnoreCase( CapabilitiesTags.EPSG_4326 )==0)
179
    		||(id.compareToIgnoreCase( CapabilitiesTags.CRS_84)==0))
180
    	{
181
    		if (latLonBbox != null)
182
    		return (BoundaryBox)latLonBbox; 
183
    	}
184
        BoundaryBox b = (BoundaryBox) bBoxes.get(id);
185
        if (b == null && parent!=null)
186
            return parent.getBbox(id);
187
        return (BoundaryBox)bBoxes.get(id);
188
    } 
189
    
190
    /**
191
     * <p>Gets the bBoxes vector</p> 
192
     * @return 
193
     */
194
    public Hashtable getBboxes() {
195
        return bBoxes;
196
    } 
197
    
198
    
199
    //Methods to manipulate the box that defines the layer extent in LatLon SRS.
200
    public BoundaryBox getLatLonBox()
201
    {
202
        return latLonBbox;
203
    }
204
    public void setLatLonBox(BoundaryBox box)
205
    {
206
        latLonBbox = box;
207
    }   
208
    /**
209
     * <p>adds a new srs to the srs vector</p>  
210
     */    
211
    public void addSrs(String srs)
212
    {
213
    	if (!this.srs.contains(srs))
214
    		this.srs.add(srs);
215
    }
216
    
217
    public Vector getAllSrs()
218
    {   
219
        Vector mySRSs = (Vector) this.srs.clone();
220
        if (parent!=null)
221
            mySRSs.addAll(parent.getAllSrs());
222
        return mySRSs;
223
        
224
//    	if (this.getParent()!= null)
225
//    	{
226
//    		Vector list = this.getParent().getAllSrs();
227
//    		for(int i = 0; i < this.srs.size(); i++)
228
//    		{
229
//    			list.add(srs.get(i));
230
//    		}
231
//    		return list;
232
//    	}
233
//    	else
234
//    	{
235
//    		return srs;
236
//    	}        
237
        
238
    }
239
    /**
240
     * <p>gets the maximum scale for this layer</p>
241
     * @return 
242
     */
243
    public double getScaleMax() {        
244
        return scaleMax;
245
    } 
246
    
247
    /**
248
     * <p>gets the minimum scale for this layer</p>
249
     * @return 
250
     */
251
    public double getScaleMin() {        
252
        return scaleMin;
253
    } 
254
    
255
    /**
256
     * <p>sets the minimum scale for this layer to be visible.</p>
257
     * 
258
     * @param scale 
259
     */
260
    public void setScaleMin(double scale) {        
261
        scaleMin = scale;
262
    } 
263
    
264
    /**
265
     * <p>sets the maximum scale for this layer to be visible</p>
266
     * @param scale 
267
     */
268
    public void setScaleMax(double scale) {        
269
        scaleMax = scale;
270
    } 
271
        
272
    /**
273
     * <p> gets the dimension vector defined in this layer</p>
274
     * @return 
275
     */
276
    public abstract ArrayList getDimensions();
277
//    public ArrayList getDimensions() {        
278
//        return dimensions;
279
//    } 
280
    
281
    public WMSDimension getDimension(String name)
282
    {
283
    	for(int i = 0; i < dimensions.size(); i++ ){
284
    		if(((WMSDimension)dimensions.get(i)).getName().compareTo(name)==0)
285
    		{
286
    			return (WMSDimension)dimensions.get(i);
287
    		}
288
    	}
289
    	return null;
290
    }
291

  
292
//    /**
293
//     * <p>Sets the dimension vector defined for this layer</p>
294
//     * @param v 
295
//     */
296
//    public void setDimensions(ArrayList v) {        
297
//        dimensions = (ArrayList)v.clone(); 
298
//    } 
299
    
300
    /**
301
     * <p>Adds a dimension to the dimension vector </p>
302
     * @param dimension 
303
     */
304
    public void addDimension(org.gvsig.remoteClient.wms.WMSDimension dimension) {        
305
        dimensions.add(dimension);
306
    } 
307
      
308
    /**
309
     * <p>Gets layer name</p>
310
     * @return 
311
     */
312
    public String getName() {        
313
        return this.name;
314
    } 
315
    
316
    /**
317
     * <p>Sets layer name</p>
318
     * @param _name 
319
     */
320
    public void setName(String name) {        
321
        this.name = name;    	
322
    } 
323
    
324
    /**
325
     * <p>Gets layer title</p>
326
     * @return 
327
     */
328
    public String getTitle() {        
329
        return title;
330
    } 
331
    
332
    /**
333
     * <p>Sets the layer title</p>
334
     * @param _title 
335
     */
336
    public void setTitle(String title) {        
337
        this.title = title;
338
    } 
339
    
340
    /**
341
     * <p>Gets the layer abstract</p>
342
     * @return 
343
     */
344
    public String getAbstract() {        
345
        return layerAbstract;
346
    } 
347
    
348
    /**
349
     * <p>Sets the layer abstract</p>
350
     * @param m_abstract 
351
     */
352
    public void setAbstract(String _abstract) {        
353
        layerAbstract = _abstract;
354
    }
355
    
356
    
357
    public ArrayList getChildren() {
358
        return children;
359
    }
360
    
361
    
362
    public void setChildren(ArrayList children) {
363
        this.children = children;
364
    }
365
    
366
    
367
    public WMSLayer getParent() {
368
        return parent;
369
    }
370
    
371
    
372
    public void setParent(WMSLayer parent) {
373
        this.parent = parent;
374
    }
375
    
376
    public String toString(){
377
        return this.getTitle();
378
    }
379
  
380

  
381
    /**
382
     * Tells if this layer accepts getFeatureInfo requests.
383
     */
384
    public boolean isQueryable() {
385
        return queryable;
386
    }
387

  
388

  
389
    /**
390
     * @param queryable The queryable to set.
391
     */
392
    public void setQueryable(boolean queryable) {
393
        this.queryable = queryable;
394
    }
395

  
396
    /**
397
     * Tells if this layer is opaque.
398
     */
399
    public boolean isOpaque() {
400
        return opaque;
401
    }
402
    /**
403
     * @param opaque.
404
     */
405
    public void setOpaque(boolean opaque) {
406
        this.opaque = opaque;
407
    }
408
 
409
    /**
410
     * Tells if this layer is subsettable
411
     */
412
    public boolean noSubSets() {
413
        return this.m_noSubSets;
414
    }
415
    /**
416
     * @param set layer nosubsets attribute.
417
     */
418
    public void setNoSubSets(boolean _noSubSets) {
419
        m_noSubSets = _noSubSets;
420
    }        
421
    
422
    public void setfixedWidth(int w) {
423
        fixedWidth = w;
424
    }
425
 
426
    public int getfixedWidth() {
427
        return fixedWidth;
428
    }
429
    
430
    public void setfixedHeight(int h) {
431
        fixedHeight = h;
432
    }
433
 
434
    public int getfixedHeight() {
435
        return fixedHeight;
436
    }    
437
    
438
    /**
439
     * @return <b>true</b> if this layer can be served with transparency, otherwise <b>false</b>
440
     */
441
    public boolean hasTransparency() {
442
        return transparency;
443
    }
444
    
445
    //Methods to parse tags that are common to several versions of WMS.
446
    //In case there is a version which has different implemantation of one of this tags
447
    // the subclass can overwrite this method
448
    
449
    /**
450
     * Parses the keywordlist from the capabilities and fills this list in the WMSLayer.
451
     * @param parser
452
     */
453
    protected void parseKeywordList(KXmlParser parser)  throws IOException, XmlPullParserException
454
    {
455
    	int currentTag;
456
    	boolean end = false;
457
    	String value;
458
    	
459
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.KEYWORDLIST);
460
    	currentTag = parser.nextTag();
461
        
462
        while (!end) 
463
    	{
464
			 switch(currentTag)
465
			 {
466
				case KXmlParser.START_TAG:
467
					if (parser.getName().compareTo(CapabilitiesTags.KEYWORD)==0)
468
					{						
469
						value = parser.nextText();
470
						if ((value != null) && (value.length() > 0 ))
471
							addkeyword(value);						
472
					}	
473
					break;
474
				case KXmlParser.END_TAG:
475
					if (parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST) == 0)
476
						end = true;
477
					break;
478
				case KXmlParser.TEXT:					
479
					break;
480
			 }
481
			 if (!end)
482
			 {
483
				 currentTag = parser.next();
484
			 }
485
    	}
486
    	parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.KEYWORDLIST);    	
487
    }
488
    
489
    /**
490
     * Reads and parses the layer attributes
491
     * Maybe this method should be moved to the WMSLayer. Until now the attributes are teh same for all versions.
492
     * @param parser
493
     */
494
    protected void readLayerAttributes(KXmlParser parser)
495
    {
496
    	String value = new String();
497
    	
498
        //First of all set whether the layer is Queryable reading the attribute.
499
        value = parser.getAttributeValue("", CapabilitiesTags.QUERYABLE);
500
        if (value != null)
501
        {
502
            if (value.compareTo("0")==0)
503
                setQueryable(false);
504
            else
505
                setQueryable(true);
506
        }
507
        value = parser.getAttributeValue("", CapabilitiesTags.OPAQUE);
508
        if (value != null)
509
        {
510
            if (value.compareTo("0")==0)
511
                setOpaque(false);
512
            else
513
                setOpaque(true);
514
        }
515
        value = parser.getAttributeValue("", CapabilitiesTags.NOSUBSETS);
516
        if (value != null)
517
        {
518
            if (value.compareTo("0")==0)
519
                setNoSubSets(false);
520
            else
521
            	setNoSubSets(true);
522
        }
523
        value = parser.getAttributeValue("", CapabilitiesTags.FIXEDWIDTH);
524
        if (value != null)
525
        {
526
        	setfixedWidth(Integer.parseInt(value));
527
        }        
528
        value = parser.getAttributeValue("", CapabilitiesTags.FIXEDHEIGHT);
529
        if (value != null)
530
        {
531
        	setfixedHeight(Integer.parseInt(value));
532
        }      
533
    }    
534
    
535
    
536
    /**
537
     * <p>Inner class describing the MetadataURL tag in OGC specifications in WMS</p>
538
     * 
539
     */
540
    protected class MetadataURL
541
    {    	
542
    	public MetadataURL()
543
    	{
544
    		type = new String();
545
    		format = new String();
546
    		onlineResource_xlink = new String();
547
    		onlineResource_type = new String();
548
    		onlineResource_href = new String();
549
    	}
550
        public String type;
551
        public String format;
552
        public String onlineResource_xlink;
553
        public String onlineResource_type;
554
        public String onlineResource_href;
555
     } 
556

  
557
    /**
558
     * <p>Inner class describing the DataURL tag in OGC specifications in WMS</p>
559
     * 
560
     */
561
    protected class DataURL
562
    {    	
563
    	public DataURL()
564
    	{
565
    		type = new String();
566
    		format = new String();
567
    		onlineResource_xlink = new String();
568
    		onlineResource_type = new String();
569
    		onlineResource_href = new String();
570
    	}
571
        public String type;
572
        public String format;
573
        public String onlineResource_xlink;
574
        public String onlineResource_type;
575
        public String onlineResource_href;
576
     }     
577
}
0 578

  
tags/Root_MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSClient.java
1

  
2
package org.gvsig.remoteClient.wms;
3

  
4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6
import java.io.IOException;
7
import java.net.ConnectException;
8
import java.util.TreeMap;
9
import java.util.Vector;
10

  
11
import org.gvsig.remoteClient.exceptions.ServerErrorException;
12
import org.gvsig.remoteClient.exceptions.WMSException;
13
import org.gvsig.remoteClient.utils.BoundaryBox;
14

  
15

  
16
/**
17
 * <p>Represents the class the with the necessary logic to connect to a OGCWMS and interpretate the data </p>
18
 * 
19
 */
20
public class WMSClient extends org.gvsig.remoteClient.RasterClient {
21
    private org.gvsig.remoteClient.wms.WMSProtocolHandler handler;
22
    private TreeMap layers = new TreeMap();
23
    private WMSLayer rootLayer;
24
    
25
    /**
26
     * @return Returns the rootLayer.
27
     */
28
    public WMSLayer getRootLayer() {
29
        return rootLayer;
30
    }
31

  
32
    /**
33
     * @param rootLayer The rootLayer to set.
34
     */
35
    public void setRootLayer(WMSLayer rootLayer) {
36
        this.rootLayer = rootLayer;
37
    }
38

  
39
    
40
    /**
41
     * Constructor.
42
     * the parameter host, indicates the WMS host to connect.
43
     * */
44
    public WMSClient(String host, WMSEventListener listener) throws ConnectException, IOException 
45
    {
46
    	setHost(host);
47
        try {        	
48
        	handler = WMSProtocolHandlerFactory.negotiate(host, listener);
49
        	handler.setHost(host);        
50
        	
51
        } catch(ConnectException conE) {
52
        	conE.printStackTrace();
53
        	throw conE; 
54
        } catch(IOException ioE) {
55
        	ioE.printStackTrace();
56
        	throw ioE; 
57
        } catch(Exception e) {
58
        	e.printStackTrace();       	
59
        }
60
    }
61
    
62
    /**
63
     * Constructor.
64
     * the parameter host, indicates the WMS host to connect.
65
     * @deprecated (l'hauria de llevar)
66
     * */
67
    public WMSClient(String host) throws ConnectException, IOException 
68
    {
69
    	setHost(host);
70
        try {        	
71
        	handler = WMSProtocolHandlerFactory.negotiate(host, null);
72
        	handler.setHost(host);        
73
        	
74
        } catch(ConnectException conE) {
75
        	conE.printStackTrace();
76
        	throw conE; 
77
        } catch(IOException ioE) {
78
        	ioE.printStackTrace();
79
        	throw ioE; 
80
        } catch(Exception e) {
81
        	e.printStackTrace();       	
82
        }
83
    }
84
    
85
    public String getVersion()
86
    {
87
        return handler.getVersion();
88
    }
89
    /**
90
     * <p>One of the three interfaces that OGC WMS defines. Request a map.</p> 
91
     * @throws ServerErrorException 
92
     */
93
    public File getMap(WMSStatus status) throws WMSException, ServerErrorException{   
94
        return handler.getMap(status);
95
    } 
96
    
97
    /**
98
     * <p>One of the three interfaces defined by OGC WMS, it gets the service capabilities</p>
99
     * @param override, if true the previous downloaded data will be overridden
100
     */
101
    public void getCapabilities(WMSStatus status, boolean override) {        
102
        handler.getCapabilities(status, override);
103
        layers = handler.layers;
104
        rootLayer = handler.rootLayer;
105
    } 
106
    
107
    /**
108
     * <p>One of the three interfaces defined by the OGC WMS, it gets the information about a feature requested</p>
109
     * @return 
110
     */
111
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount) throws WMSException{        
112
        return handler.getFeatureInfo(status, x, y, featureCount);
113
    } 
114
    
115
    /**
116
     * <p> Reads from the WMS Capabilities, the layers available in the service</p>
117
     * @return a TreeMap with the available layers in the WMS 
118
     */
119
    public TreeMap getLayers() {        
120
        return layers;
121
    } 
122
    
123
    /**
124
     * <p>Reads from the WMS Capabilities the number if layers available in the service</p>
125
     * @return, number of layers available
126
     */
127
    public int getNumberOfLayers() {        
128
        if (layers != null)
129
        {
130
            return layers.size();
131
        }
132
        return 0;
133
    } 
134
    
135
    /**
136
     * <p>Gets the WMSLayer with this name</p>
137
     * 
138
     * @param _name, layer name
139
     * @return the layer with this name
140
     */
141
    public WMSLayer getLayer(String _name) {        
142
        if (layers.get(_name) != null)
143
        {
144
            return (WMSLayer)layers.get(_name);
145
        }
146
        
147
        return null;
148
    } 
149
    
150
    public String[] getLayerNames()
151
    {    	
152
        WMSLayer[] lyrs;
153
        
154
        lyrs = (WMSLayer[])layers.values().toArray(new WMSLayer[0]);
155
        
156
        String[] names = new String[lyrs.length];
157
        
158
        for(int i = 0; i<lyrs.length; i++)
159
        {
160
            names[i] = ((WMSLayer)lyrs[i]).getName();
161
        }
162
        return names;
163
    }
164
    
165
    /**
166
     * <p>Gets the image formats available in the Service to retrieve the maps</p>
167
     * @return a vector with all the available formats
168
     */
169
    public Vector getFormats() {        
170
        return handler.getServiceInformation().formats;         
171
    } 
172
    
173
    public boolean isQueryable()
174
    {
175
    	return handler.getServiceInformation().isQueryable();  
176
    }
177
    
178
    public void close() {        
179
        // your code here
180
    } 
181
    
182
    
183
    /**
184
     * Returns the max extent that envolves the requested layers
185
     * */
186
    public Rectangle2D getLayersExtent(String[]layerNames, String srs)
187
    {
188
        try
189
        {
190
            BoundaryBox bbox;
191
            WMSLayer layer = getLayer(layerNames[0]);
192
            
193
            bbox = layer.getBbox(srs);
194
            if (bbox == null) return null;
195
            double xmin = bbox.getXmin();
196
            double xmax = bbox.getXmax();
197
            double ymin = bbox.getYmin();
198
            double ymax = bbox.getYmax();
199
            
200
            for(int i=1; i<layerNames.length; i++)
201
            {
202
                layer = getLayer(layerNames[i]);
203
                bbox = layer.getBbox(srs);
204
                if (bbox == null) return null;
205
                if (bbox.getXmin() < xmin)
206
                {
207
                    xmin = bbox.getXmin();
208
                }
209
                if (bbox.getYmin() < ymin)
210
                {
211
                    ymin = bbox.getYmin();
212
                }
213
                if (bbox.getXmax() > xmax)
214
                {
215
                    xmax = bbox.getXmax();
216
                }
217
                if (bbox.getYmax() > ymax)
218
                {
219
                    ymax = bbox.getYmax();
220
                }
221
            }	
222
            
223
            Rectangle2D extent = new Rectangle2D.Double(xmin,ymin,Math.abs(xmax-xmin),Math.abs(ymax-ymin));
224
            return extent;
225
        }
226
        catch(Exception e)
227
        {
228
            e.printStackTrace();
229
            return null;
230
        }
231
    }
232
    
233
    
234
    /**
235
     * Gets the Service information included in the Capabilities
236
     * */
237
    
238
    public WMSProtocolHandler.ServiceInformation getServiceInformation()
239
    {
240
        return handler.getServiceInformation();  
241
    }
242
    
243
    
244
    /**
245
     * <p>Checks the connection to de remote WMS and requests its capabilities.</p>
246
     * @param override, if true the previous downloaded data will be overridden
247
     */
248
    public boolean connect(boolean override) 
249
    {
250
        try {            
251
            if (handler == null)
252
            {
253
                if (getHost().trim().length() > 0)
254
                {					
255
                    //TODO: Implement correctly the negotiate algorithm
256
                    handler = WMSProtocolHandlerFactory.negotiate(getHost(), null);
257
                    //handler = new WMSProtocolHandler1_1_1();
258
                    handler.setHost(getHost());
259
                }
260
                else
261
                {
262
                    //must to specify host first!!!!
263
                    return false;
264
                }                
265
            }
266
            getCapabilities(null, override);
267
            return true;
268
            
269
        } catch (Exception e) {
270
            e.printStackTrace();
271
            return false;
272
        }
273
    }
274
    
275
    //TODO Check this out: Always 1 layer at first level...
276
    public WMSLayer getLayersRoot() {
277
        return rootLayer;
278
    }
279

  
280
	public boolean connect() {
281
		return connect(false);
282
	}
283
}
0 284

  
tags/Root_MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_1_0/WMSLayer1_1_0.java
1

  
2
package org.gvsig.remoteClient.wms.wms_1_1_0;
3

  
4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.TreeMap;
7

  
8
import org.gvsig.remoteClient.utils.BoundaryBox;
9
import org.gvsig.remoteClient.utils.CapabilitiesTags;
10
import org.gvsig.remoteClient.utils.Utilities;
11
import org.gvsig.remoteClient.wms.WMSDimension;
12
import org.gvsig.remoteClient.wms.WMSExtent;
13
import org.kxml2.io.KXmlParser;
14
import org.xmlpull.v1.XmlPullParserException;
15

  
16

  
17
/**
18
 * <p>WMS Layer for WMS 1.1.0</p>
19
 * 
20
 */
21
public class WMSLayer1_1_0 extends org.gvsig.remoteClient.wms.WMSLayer {
22
    
23
    /**
24
     * <p>Extents defined for the layer in the capabilities doc</p>
25
     */
26
    private java.util.ArrayList extents = new ArrayList();
27
    
28
    /**
29
     * <p> gets the extent vector defined in this layer</p>
30
     * @return 
31
     */
32
    public ArrayList getExtents() {        
33
        return extents;
34
    } 
35
    
36
    public WMSExtent getExtent(String name)
37
    {
38
    	for(int i = 0; i < extents.size(); i++ ){
39
    		if(((WMSExtent)extents.get(i)).getName().compareTo(name)==0)
40
    		{
41
    			return (WMSExtent)extents.get(i);
42
    		}
43
    	}
44
    	return null;
45
    }
46
    
47
    /**
48
     * <p>Adds an extent to the extent vector </p>
49
     * @param extent 
50
     */
51
    public void addExtent(org.gvsig.remoteClient.wms.WMSExtent extent) {        
52
        extents.add(extent);
53
    }   
54
    
55
    public ArrayList getDimensions()
56
    {   
57
        WMSDimension dimension;
58
        WMSExtent extent;
59
    	for(int i = 0; i < dimensions.size(); i++)
60
    	{
61
    		dimension = (WMSDimension)dimensions.get(i);
62
    		extent = getExtent(dimension.getName()); 
63
    		if(extent != null)
64
    		{    			
65
    			((WMSDimension)dimensions.get(i)).setDimensionExpression( extent.getExtentExpression());    			
66
    		}
67
    	}    	
68
    	
69
        WMSDimension pDimension;
70
        WMSDimension myDimension;    
71
        ArrayList myDimensions = (ArrayList) this.dimensions.clone();        
72
        ArrayList pDimensions;        
73
        if (parent!=null)
74
        {
75
        	pDimensions = parent.getDimensions();
76
        	for (int i= 0; i < pDimensions.size(); i++){
77
        		pDimension = (WMSDimension)pDimensions.get(i);
78
        		myDimension = getDimension(pDimension.getName());
79
        		if (myDimension != null){
80
        			pDimensions.remove(pDimension);
81
        		}
82
        	}
83
        	myDimensions.addAll(pDimensions);
84
        }
85
        return myDimensions;
86
    }
87
    
88
    public WMSLayer1_1_0()
89
    {
90
        children = new ArrayList();
91
    }
92
    /**
93
     * <p>Parses the contents of the parser(WMSCapabilities)
94
     * to extract the information about an WMSLayer</p>
95
     * 
96
     */
97
    public void parse(KXmlParser parser, TreeMap layerTreeMap)
98
    throws IOException, XmlPullParserException
99
    {
100
        int currentTag;
101
        boolean end = false;
102
        String value;
103
        BoundaryBox bbox;
104
        parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.LAYER);
105
        
106
        //First of all set whether the layer is Queryable reading the attribute.
107
        value = parser.getAttributeValue("", CapabilitiesTags.QUERYABLE);
108
        if (value != null)
109
        {
110
            if (value.compareTo("0")==0)
111
                setQueryable(false);
112
            else
113
                setQueryable(true);
114
        }
115
        
116
        currentTag = parser.nextTag();
117
        
118
        while (!end) 
119
        {
120
            switch(currentTag)
121
            {
122
                case KXmlParser.START_TAG:
123
                    if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
124
                    {	
125
                        WMSLayer1_1_0 lyr = new WMSLayer1_1_0();						
126
                        //parser.next(); 
127
                        lyr.parse(parser, layerTreeMap);
128
                        lyr.setParent(this);
129
                        this.children.add(lyr);
130
                        // Jaume
131
                        if (lyr.getName()!=null)
132
                            layerTreeMap.put(lyr.getName(), lyr);
133
                    }
134
                    else if (parser.getName().compareTo(CapabilitiesTags.ATTRIBUTION)==0){
135
                        // TODO comprobar que esto se necesite o se deseche
136
                        parser.skipSubTree();
137
                    }
138
                    else if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
139
                    {		
140
                        value = parser.nextText();
141
                        if (value != null) setName(value);						
142
                    }	
143
                    else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
144
                    {
145
                        value = parser.nextText();
146
                        if (value != null) setTitle(value);
147
                    }
148
                    else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
149
                    {
150
                        value = parser.nextText();
151
                        if (value != null) setAbstract(value);
152
                    }
153
                    else if (parser.getName().compareTo(CapabilitiesTags.SRS)==0)
154
                    {
155
                        value = parser.nextText();
156
                        if (value != null){
157
                            String[] mySRSs = value.split(" ");
158
                            for (int i = 0; i < mySRSs.length; i++) {
159
                                addSrs(mySRSs[i]);    
160
                            }
161
                            
162
                        }
163
                    }					
164
                    else if (parser.getName().compareTo(CapabilitiesTags.BOUNDINGBOX)==0)
165
                    {
166
                        bbox = new BoundaryBox();
167
                        value = parser.getAttributeValue("",CapabilitiesTags.SRS);
168
                        if (value != null)
169
                            bbox.setSrs(value);
170
                        value = parser.getAttributeValue("",CapabilitiesTags.MINX);
171
                        if ((value != null) && (Utilities.isNumber(value)))
172
                            bbox.setXmin(Double.parseDouble(value));	
173
                        value = parser.getAttributeValue("",CapabilitiesTags.MINY);
174
                        if ((value != null) && (Utilities.isNumber(value)))
175
                            bbox.setYmin(Double.parseDouble(value));	
176
                        value = parser.getAttributeValue("",CapabilitiesTags.MAXX);
177
                        if ((value != null) && (Utilities.isNumber(value)))
178
                            bbox.setXmax(Double.parseDouble(value));	
179
                        value = parser.getAttributeValue("",CapabilitiesTags.MAXY);
180
                        if ((value != null) && (Utilities.isNumber(value)))
181
                            bbox.setYmax(Double.parseDouble(value));	
182
                        addBBox(bbox);
183
                        addSrs(bbox.getSrs());
184
                    }	
185
                    else if (parser.getName().compareTo(CapabilitiesTags.LATLONBOUNDINGBOX)==0)
186
                    {
187
                        bbox = new BoundaryBox();
188
                        bbox.setSrs(CapabilitiesTags.EPSG_4326);
189
                        value = parser.getAttributeValue("",CapabilitiesTags.MINX);
190
                        if ((value != null) && (Utilities.isNumber(value)))
191
                            bbox.setXmin(Double.parseDouble(value));	
192
                        value = parser.getAttributeValue("",CapabilitiesTags.MINY);
193
                        if ((value != null) && (Utilities.isNumber(value)))
194
                            bbox.setYmin(Double.parseDouble(value));	
195
                        value = parser.getAttributeValue("",CapabilitiesTags.MAXX);
196
                        if ((value != null) && (Utilities.isNumber(value)))
197
                            bbox.setXmax(Double.parseDouble(value));	
198
                        value = parser.getAttributeValue("",CapabilitiesTags.MAXY);
199
                        if ((value != null) && (Utilities.isNumber(value)))
200
                            bbox.setYmax(Double.parseDouble(value));	
201
                        addBBox(bbox);
202
                        setLatLonBox(bbox);
203
                        addSrs(bbox.getSrs());
204
                    }						
205
                    else if (parser.getName().compareTo(CapabilitiesTags.SCALEHINT)==0)
206
                    {
207
                        value = parser.getAttributeValue("",CapabilitiesTags.MIN);
208
                        if ((value != null) && (Utilities.isNumber(value)))
209
                            setScaleMin(Double.parseDouble(value));
210
                        value = parser.getAttributeValue("",CapabilitiesTags.MAX);
211
                        if ((value != null) && (Utilities.isNumber(value)))
212
                            setScaleMax(Double.parseDouble(value));																	
213
                    }						
214
                    else if (parser.getName().compareTo(CapabilitiesTags.STYLE)==0)
215
                    {
216
                        WMSStyle1_1_0 style = new WMSStyle1_1_0();
217
                        style.parse(parser);
218
                        if ((style != null) && (style.getName() != null))
219
                        {
220
                            styles.add(style);
221
                        }
222
                    }
223
                    else if (parser.getName().compareTo(CapabilitiesTags.DIMENSION)==0)
224
                    {
225
                        WMSDimension dim = new WMSDimension();
226
                        dim.parse(parser);
227
                        if ((dim != null) && (dim.getName() != null))
228
                        {
229
                            addDimension(dim);
230
                        }
231
                    }
232
                    else if (parser.getName().compareTo(CapabilitiesTags.EXTENT)==0)
233
                    {                    	
234
                        WMSExtent extent = new WMSExtent();
235
                        extent.parse(parser);
236
                        if ((extent != null) && (extent.getName() != null))
237
                        {
238
                            addExtent(extent);
239
                            
240
                        }
241
                    }                      
242
                    break;
243
                case KXmlParser.END_TAG:
244
                    if (parser.getName().compareTo(CapabilitiesTags.LAYER) == 0)
245
                        end = true;
246
                    break;
247
                case KXmlParser.TEXT:					
248
                    break;
249
            }
250
            if (!end)
251
            	currentTag = parser.next();
252
        }
253
        parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.LAYER);
254
    }      
255
    
256
    public String toString(){
257
        return super.toString();
258
    }
259
}
0 260

  
tags/Root_MULTITHREADING_DEVELOPMENT/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_1_0/WMSProtocolHandler1_1_0.java
1

  
2
package org.gvsig.remoteClient.wms.wms_1_1_0;
3

  
4
import java.io.BufferedReader;
5
import java.io.ByteArrayInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.IOException;
11
import java.util.ArrayList;
12
import java.util.TreeMap;
13

  
14
import org.gvsig.remoteClient.utils.CapabilitiesTags;
15
import org.gvsig.remoteClient.utils.ExceptionTags;
16
import org.kxml2.io.KXmlParser;
17
import org.xmlpull.v1.XmlPullParserException;
18

  
19
/**
20
 * <p>
21
 * Describes the handler to comunicate to a WMS 1.1.0
22
 * </p>
23
 */
24
public class WMSProtocolHandler1_1_0 extends org.gvsig.remoteClient.wms.WMSProtocolHandler
25
{
26
	private WMSLayer1_1_0 fakeRootLayer;
27
    
28
	public WMSProtocolHandler1_1_0()
29
	{
30
		this.version = "1.1.0";
31
		this.name = "WMS1.1.0";
32
		this.serviceInfo = new ServiceInformation(); 
33
		this.layers = new TreeMap();
34
	}
35
    
36
//------------------------------------------------------------------------------
37
// Parsing methods....    
38
//------------------------------------------------------------------------------    
39
/**
40
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
41
 * 
42
 */
43
    public void parse(File f)
44
    {       
45
    	FileReader reader = null;  	
46
    	try
47
    	{
48
    		reader = new FileReader(f);
49
    		BufferedReader br = new BufferedReader(reader);
50
    		char[] buffer = new char[100];
51
    		br.read(buffer);
52
    		StringBuffer st = new StringBuffer(new String(buffer));
53
    		String searchText = "encoding=\"";
54
    		int index = st.indexOf(searchText);
55
    		if (index>-1) {
56
    			st.delete(0, index+searchText.length());
57
    			encoding = st.substring(0, st.indexOf("\""));
58
    		}
59
    	}
60
    	catch(FileNotFoundException ex)	{
61
    		ex.printStackTrace();
62
    	} catch (IOException e) {
63
			e.printStackTrace();
64
		}
65
    	
66
    	int tag;
67
    	KXmlParser kxmlParser = null;
68
    	kxmlParser = new KXmlParser();
69
    	try
70
    	{
71
    		kxmlParser.setInput(new FileInputStream(f), encoding);		
72
			kxmlParser.nextTag();
73
    		if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
74
    		{    		
75
    			kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);    			
76
    			tag = kxmlParser.nextTag();
77
				 while(tag != KXmlParser.END_DOCUMENT)
78
				 {
79
                     switch(tag)
80
					 {
81
                         
82
						case KXmlParser.START_TAG:
83
							if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
84
							{
85
								parseServiceTag(kxmlParser);
86
							}	
87
							else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
88
							{
89
								parseCapabilityTag(kxmlParser);
90
							}
91
							break;
92
						case KXmlParser.END_TAG:							
93
							break;
94
						case KXmlParser.TEXT:
95
												
96
						break;
97
					 }
98
    				 tag = kxmlParser.next();
99
    			 }
100

  
101
    			kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
102
    		}
103
    	}
104
    	catch(XmlPullParserException parser_ex){
105
    		parser_ex.printStackTrace();
106
    	}
107
   		catch (IOException ioe) {			
108
   			ioe.printStackTrace();
109
		} finally {
110
            
111
        }
112
    } 
113
    
114
    /**
115
     * <p>Parses the Service Information </p>
116
     */    
117
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
118
    {
119
    	int currentTag;
120
    	boolean end = false;
121
    	
122
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
123
    	currentTag = parser.next();
124
    	
125
    	while (!end) 
126
    	{
127
			 switch(currentTag)
128
			 {
129
				case KXmlParser.START_TAG:
130
					if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
131
					{
132
						serviceInfo.name = parser.nextText(); 
133
					}	
134
					else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
135
					{
136
						serviceInfo.title = parser.nextText(); 
137
					}
138
					else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
139
					{
140
						serviceInfo.abstr = parser.nextText(); 
141
					}
142
					else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
143
					{
144
				    	String value = null;
145
				        value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
146
				        if (value != null){
147
				        	serviceInfo.online_resource = value;
148
				        }
149
					}					
150
					else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
151
							(parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
152
					{
153
						parser.skipSubTree();
154
					}					
155
					break;
156
				case KXmlParser.END_TAG:
157
					if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
158
						end = true;
159
					break;
160
				case KXmlParser.TEXT:					
161
				break;
162
			 }
163
             if (!end)
164
                 currentTag = parser.next();
165
    	}
166
    	parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
167
    }
168
    
169
    /**
170
     * <p>Parses the Capability Tag </p>
171
     */    
172
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
173
    { 	
174
    	int currentTag;
175
    	boolean end = false;
176
    	
177
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
178
    	currentTag = parser.next();
179
    	
180
    	while (!end) 
181
    	{
182
			 switch(currentTag)
183
			 {
184
				case KXmlParser.START_TAG:
185
					if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
186
					{
187
						parseRequestTag(parser); 
188
					}	
189
					else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
190
					{
191
						// TODO:
192
						// add the exception formats supported.
193
					}
194
					else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
195
					{
196
						WMSLayer1_1_0 lyr = new WMSLayer1_1_0();
197
                        if (rootLayer == null)
198
                            rootLayer = lyr;
199
                        else {
200
                            // Handles when there is no general root layer, will use
201
                            // a fake non-queryable one.
202
                            if (!rootLayer.equals(getFakeRootLayer())){
203
                                WMSLayer1_1_0 aux = (WMSLayer1_1_0) rootLayer;
204
                                rootLayer  = getFakeRootLayer();
205
                                rootLayer.getChildren().add(aux);
206
                            }
207
                            rootLayer.getChildren().add(lyr);
208
                        }
209
						lyr.parse(parser, layers);
210
						
211
                        if (lyr.getName()!=null)
212
						    layers.put(lyr.getName(), lyr); 													
213
					}
214
					else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
215
							(parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
216
                            
217
					{
218
						parser.skipSubTree();
219
					}					
220
					break;
221
				case KXmlParser.END_TAG:
222
					if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
223
						end = true;
224
					break;
225
				case KXmlParser.TEXT:					
226
				break;
227
			 }
228
			 if (!end)
229
			 currentTag = parser.next();
230
    	}
231
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);    	
232
    }
233
    
234
    /**
235
     * <p>Parses the Request tag </p>
236
     */ 
237
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
238
    {	
239
    	int currentTag;
240
    	boolean end = false;
241
    	
242
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
243
    	currentTag = parser.next();
244
    	
245
    	while (!end) 
246
    	{
247
			 switch(currentTag)
248
			 {
249
				case KXmlParser.START_TAG:
250
					if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
251
					{
252
						serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES , null); 
253
					}	
254
					else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
255
					{	
256
						serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
257
						parseGetMapTag(parser);						
258
					}
259
					else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
260
					{
261
						serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null); 
262
						parseGetFeatureInfoTag(parser);
263
					}		
264
					else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
265
					{
266
						serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
267
					}					
268
					break;
269
				case KXmlParser.END_TAG:
270
					if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
271
						end = true;
272
					break;
273
				case KXmlParser.TEXT:					
274
				break;
275
			 }
276
			 if(!end)
277
				 currentTag = parser.next();
278
    	}
279
    	// TODO: does not get such a tag when arrives here!!!!!!
280
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);    	
281
    }
282

  
283
    /**
284
     * <p>Parses the GetMap tag </p>
285
     */ 
286
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
287
    {	
288
    	int currentTag;
289
    	boolean end = false;
290
    	
291
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
292
    	currentTag = parser.next();
293
    	
294
    	while (!end) 
295
    	{
296
			 switch(currentTag)
297
			 {
298
				case KXmlParser.START_TAG:
299
					if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
300
					{
301
						serviceInfo.formats.add(parser.nextText());
302
					}	
303
					else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
304
					{			
305
						currentTag = parser.nextTag();
306
						if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
307
						{
308
							currentTag = parser.nextTag();
309
							if(parser.getName().compareTo(CapabilitiesTags.GET)==0)
310
							{
311
								currentTag = parser.nextTag();
312
								if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
313
								{
314
									String value = new String();
315
									value = parser.getAttributeValue("", CapabilitiesTags.XLINK_HREF);
316
									if (value != null){
317
										serviceInfo.operations.put(CapabilitiesTags.GETMAP, value);
318
									}
319
								}
320
							}
321
						}
322
					}			
323
					break;
324
				case KXmlParser.END_TAG:
325
					if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
326
						end = true;
327
					break;
328
				case KXmlParser.TEXT:					
329
				break;
330
			 }
331
			 if(!end)
332
				 currentTag = parser.next();
333
    	}	
334
    }    
335
    
336
    /**
337
     * <p>Parses the GetMap tag </p>
338
     */ 
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff