Revision 11381

View differences:

trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSProtocolHandler.java
26 26
/**
27 27
 * <p> Abstract class that represents handlers to comunicate via WMS protocol.
28 28
 * </p>
29
 * 
29
 *
30 30
 */
31 31
public abstract class WMSProtocolHandler {
32 32
	/**
......
48 48
    /**
49 49
     * port number of the comunication channel of the WMS to connect
50 50
     */
51
    protected String port;    
51
    protected String port;
52 52
    /**
53 53
     * WMS metadata
54 54
     */
......
56 56
    public TreeMap layers;
57 57
    public WMSLayer rootLayer;
58 58
//    public Vector srs;
59
    
59

  
60 60
    /**
61
     * parses the data retrieved by the WMS in XML format. 
61
     * parses the data retrieved by the WMS in XML format.
62 62
     * It will be mostly the WMS Capabilities, but the implementation
63 63
     * will be placed in the handler implementing certain version of the protocol.
64
     * 
64
     *
65 65
     */
66 66
    public abstract void parse(File f) ;
67
    
67

  
68 68
    /**
69 69
     * returns the alfanumeric information of the layers at the specified point.
70 70
     * the diference between the other getfeatureInfo method is that this will
71 71
     * be implemented by each specific version because the XML from the server will be
72 72
     * parsed and presented by a well known structure.
73
     */    
73
     */
74 74

  
75
    public String getName() {        
75
    public String getName() {
76 76
    	return name;
77
    } 
77
    }
78 78

  
79
    public String getVersion() {        
79
    public String getVersion() {
80 80
    	return version;
81
    }    
82
    
83
    public ServiceInformation getServiceInformation() {        
81
    }
82

  
83
    public ServiceInformation getServiceInformation() {
84 84
        return serviceInfo;
85
    }  
85
    }
86 86
    public String getHost ()
87 87
    {
88 88
    	return host;
......
99 99
    {
100 100
    	port = _port;
101 101
    }
102
    
103 102

  
103

  
104 104
    /**
105 105
	 * <p>Builds a GetCapabilities request that is sent to the WMS
106 106
	 * the response will be parse to extract the data needed by the
......
108 108
	 * @param override, if true the previous downloaded data will be overridden
109 109
	 */
110 110
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel)
111
    {		
111
    {
112 112
    	URL request = null;
113 113
		try
114 114
		{
......
120 120
		}
121 121
		try
122 122
		{
123
			if (override) 
123
			if (override)
124 124
				Utilities.removeURL(request);
125 125
			File f = Utilities.downloadFile(request,"wms_capabilities.xml", cancel);
126 126
			if (f == null)
......
146 146
     */
147 147
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel)
148 148
    {
149
		
149

  
150 150
    	URL request = null;
151 151
    	StringBuffer output = new StringBuffer();
152 152
    	String outputFormat = new String();
153
    	String ServiceException = "ServiceExceptionReport";    	    	
153
    	String ServiceException = "ServiceExceptionReport";
154 154
    	StringBuffer sb = new StringBuffer();
155 155
    	sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
156 156
		try
......
160 160
	    	File f = Utilities.downloadFile(request,"wms_feature_info.xml", cancel);
161 161
			if (f == null)
162 162
				return "";
163
			
164
//	    	byte[] buffer = new byte[1024*256];	    	
165
//	    	DataInputStream is = new DataInputStream();	    	
163

  
164
//	    	byte[] buffer = new byte[1024*256];
165
//	    	DataInputStream is = new DataInputStream();
166 166
//	    	outputFormat = request.openConnection().getContentType();
167 167
//	    	for (int i = f.read(buffer); i>0; i = is.read(buffer))
168 168
//	    	{
169 169
//	    		String str = new String(buffer,0,i);
170
//	    		output.append(str);	    	
170
//	    		output.append(str);
171 171
//	    	}
172
//	    		    
172
//
173 173
//	    	is.close();
174 174
			FileReader fReader = new FileReader(f);
175 175
			char[] buffer = new char[1024*256];
176 176
			for (int i = fReader.read(buffer); i>0; i = fReader.read(buffer))
177 177
		    {
178 178
		    	String str = new String(buffer,0,i);
179
		    	output.append(str);	    	
179
		    	output.append(str);
180 180
		    }
181 181
		    if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1)
182 182
		    		||output.toString().toLowerCase().startsWith("<?xml")
......
184 184
		    {
185 185
		    	int tag;
186 186
		    	KXmlParser kxmlParser = null;
187
		    	kxmlParser = new KXmlParser();	    
188
		    	//kxmlParser.setInput(new StringReader(output.toString()));		    	
187
		    	kxmlParser = new KXmlParser();
188
		    	//kxmlParser.setInput(new StringReader(output.toString()));
189 189
		    	kxmlParser.setInput(new FileReader(f));
190
		    	
191
		    	tag = kxmlParser.nextTag();   	
190

  
191
		    	tag = kxmlParser.nextTag();
192 192
		    	if (kxmlParser.getName().compareTo(ServiceException)==0)
193 193
				{
194 194
		    		sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>");
195
		    		return sb.toString();									
195
		    		return sb.toString();
196 196
				}
197 197
				else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0)
198 198
				{
199 199
					return output.toString();
200 200
				}
201
				else		
201
				else
202 202
				{
203
					return output.toString();		
203
					return output.toString();
204 204
				}
205
					    		
205

  
206 206
// 				 while(tag != KXmlParser.END_DOCUMENT)
207 207
//				 {
208 208
//					 switch(tag)
209 209
//					 {
210
//						case KXmlParser.START_TAG:		
210
//						case KXmlParser.START_TAG:
211 211
//							if (kxmlParser.getName().compareTo(ServiceException)==0)
212 212
//							{
213 213
//					    		sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>");
214
//					    		return sb.toString();									
214
//					    		return sb.toString();
215 215
//							}
216 216
//							else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0)
217 217
//								return output.toString();
218
//							else								
219
//								sb.append("<" + kxmlParser.getName() + ">\n");							
218
//							else
219
//								sb.append("<" + kxmlParser.getName() + ">\n");
220 220
//							break;
221
//						case KXmlParser.END_TAG:	
221
//						case KXmlParser.END_TAG:
222 222
//							sb.append("</" + kxmlParser.getName() + ">\n");
223 223
//							break;
224 224
//						case KXmlParser.TEXT:
225
//							sb.append(kxmlParser.getText());						
225
//							sb.append(kxmlParser.getText());
226 226
//						break;
227 227
//					 }
228 228
//    				 tag = kxmlParser.next();
229
//    			 }		    		
229
//    			 }
230 230
	    		//return sb.toString();
231 231
	    	}
232 232
	    	else
......
261 261
     * <p>Builds a GetMap request that is sent to the WMS
262 262
     * the response (image) will be redirect to the
263 263
     * WMS client</p>
264
     */   
264
     */
265 265
    public byte[] _getMap(WMSStatus status) throws ServerErrorException, WMSException
266
    {        
266
    {
267 267
    	URL request = null;
268 268
		try
269 269
		{
......
273 273
			URLConnection conn = request.openConnection();
274 274
			System.out.println(request.toString());
275 275
            String type = conn.getContentType();
276
            
277
                        	
276

  
277

  
278 278
	    	byte[] imageBytes = null;
279 279
	    	byte[] buffer = new byte[1024*256];
280 280
            InputStream is = conn.getInputStream();
281 281
	    	int readed = 0;
282
	    	
282

  
283 283
	    	for (int i = is.read(buffer); i>0; i = is.read(buffer)){
284 284
                // Creates a new buffer to contain the previous readed bytes and the next bunch of bytes
285 285
	    		byte[] buffered = new byte[readed+i];
......
294 294
	    			}
295 295
				}
296 296
	    		imageBytes = (byte[]) buffered.clone();
297
	    		readed += i;	    		
297
	    		readed += i;
298 298
	    	}
299
	    	
300
	    	if ((type !=null && !type.subSequence(0,5).equals("image")) 
299

  
300
	    	if ((type !=null && !type.subSequence(0,5).equals("image"))
301 301
	    		||(Utilities.isTextData(imageBytes)))
302
	    	{	    		
302
	    	{
303 303
               	WMSException wmsEx = null;
304
               	
304

  
305 305
            	String exceptionMessage = parseException(imageBytes);
306 306
                if (exceptionMessage==null)
307 307
                {
......
319 319
            	wmsEx.setWMSMessage(new String(imageBytes));
320 320
                throw wmsEx;
321 321
            }
322
			return imageBytes;	    	
322
			return imageBytes;
323 323
		}
324 324
		catch(IOException e)
325 325
		{
326 326
			e.printStackTrace();
327 327
            throw new ServerErrorException();
328 328
		}
329
    } 
330
    
329
    }
330

  
331 331
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException
332 332
    {
333 333
    	URL request = null;
334 334
		try
335 335
		{
336
			request = new URL(buildGetLegendGraphicRequest(status, layerName));     
336
			request = new URL(buildGetLegendGraphicRequest(status, layerName));
337 337
			System.out.println(request);
338
            File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel);            		    	
338
            File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel);
339 339
	    	if (f== null)
340 340
	    		return null;
341 341
            if (Utilities.isTextFile(f)) {
......
344 344
	    		byte[] data = new byte[(int)fc.size()];
345 345
	    		ByteBuffer bb = ByteBuffer.wrap(data);
346 346
	    		fc.read(bb);
347
	    			    		
347

  
348 348
	    		WMSException wmsEx = null;
349
               	
349

  
350 350
            	String exceptionMessage = parseException(data);
351 351
                if (exceptionMessage==null)
352 352
                {
......
356 356
                	{
357 357
                		String xml = error.substring(pos,error.length());
358 358
                		exceptionMessage = parseException(xml.getBytes());
359
                	}               
359
                	}
360 360
                    if (exceptionMessage == null)
361 361
                    	exceptionMessage = new String(data);
362
                	
362

  
363 363
                }
364 364
             	wmsEx = new WMSException(exceptionMessage);
365 365
            	wmsEx.setWMSMessage(new String(data));
366 366
            	Utilities.removeURL(request);
367 367
                throw wmsEx;
368 368
            }
369
			return f;	    	
369
			return f;
370 370
		}
371 371
		catch(IOException e)
372 372
		{
373 373
			e.printStackTrace();
374 374
            throw new ServerErrorException();
375 375
		}
376
    } 
377
    
376
    }
377

  
378 378
    public File getMap(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException
379
    {        
379
    {
380 380
    	URL request = null;
381 381
		try
382 382
		{
383 383
			//TODO:
384 384
			//pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
385 385
			request = new URL(buildMapRequest(status));
386
            
387
            File f = Utilities.downloadFile(request, "wmsGetMap", cancel);            		    	
386

  
387
            File f = Utilities.downloadFile(request, "wmsGetMap", cancel);
388 388
	    	if (f== null)
389 389
	    		return null;
390 390
            if (Utilities.isTextFile(f)) {
......
393 393
	    		byte[] data = new byte[(int)fc.size()];   // fc.size returns the size of the file which backs the channel
394 394
	    		ByteBuffer bb = ByteBuffer.wrap(data);
395 395
	    		fc.read(bb);
396
	    			    		
396

  
397 397
	    		WMSException wmsEx = null;
398
               	
398

  
399 399
            	String exceptionMessage = parseException(data);
400 400
                if (exceptionMessage==null)
401 401
                {
......
407 407
                		exceptionMessage = parseException(xml.getBytes());
408 408
//                        if (exceptionMessage == null)
409 409
//                        	exceptionMessage = new String(data);
410
                	}               
410
                	}
411 411
                    if (exceptionMessage == null)
412 412
                    	exceptionMessage = new String(data);
413
                	
413

  
414 414
                }
415 415
             	wmsEx = new WMSException(exceptionMessage);
416 416
            	wmsEx.setWMSMessage(new String(data));
417
            	
417

  
418 418
            	// Since it is an error file, It must be deleted from the cache
419 419
            	Utilities.removeURL(request);
420 420
                throw wmsEx;
421 421
            }
422
			return f;	    	
422
			return f;
423 423
		}
424 424
		catch(IOException e)
425 425
		{
426 426
			e.printStackTrace();
427 427
            throw new ServerErrorException();
428 428
		}
429
    } 
430
    
431
    
429
    }
430

  
431

  
432 432
    /* (non-Javadoc)
433 433
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
434 434
     */
......
437 437
        KXmlParser kxmlParser = new KXmlParser();
438 438
        try
439 439
        {
440
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
440
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
441 441
            kxmlParser.nextTag();
442 442
            int tag;
443
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
444
            { 
445
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);             
443
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
444
            {
445
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
446 446
                tag = kxmlParser.nextTag();
447 447
                 while(tag != KXmlParser.END_DOCUMENT)
448 448
                 {
......
456 456
                                errors.add(errorCode+errorMessage);
457 457
                            }
458 458
                            break;
459
                        case KXmlParser.END_TAG:                            
459
                        case KXmlParser.END_TAG:
460 460
                            break;
461
                        
461

  
462 462
                     }
463 463
                     tag = kxmlParser.nextTag();
464 464
                 }
465 465
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
466 466
            }
467 467
        }
468
        catch(XmlPullParserException parser_ex){ 
468
        catch(XmlPullParserException parser_ex){
469 469
            parser_ex.printStackTrace();
470 470
        }
471
        catch (IOException ioe) {           
472
            ioe.printStackTrace();            
471
        catch (IOException ioe) {
472
            ioe.printStackTrace();
473 473
        }
474 474
        String message = errors.size()>0? "" : null;
475 475
        for (int i = 0; i < errors.size(); i++) {
......
483 483
     */
484 484
    public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version)
485 485
    {
486
		String req = new String();		
486
		String req = new String();
487 487
        String symbol = getSymbol(_host);
488
        req = req + _host + symbol + "REQUEST=GetCapabilities&SERVICE=WMS&";		
488
        req = req + _host + symbol + "SERVICE=WMS&REQUEST=GetCapabilities";
489 489
        if((_version != null) && (_version.length()>0 ))
490 490
        {
491 491
        	req += ("&VERSION=" + _version);
492 492
        }
493 493
		req += ("&EXCEPTIONS=XML");
494
		return req;   	
494
		return req;
495 495
    }
496
    
496

  
497 497
    /**
498 498
     * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
499 499
     * @param WMSStatus
......
502 502
    {
503 503
		StringBuffer req = new StringBuffer();
504 504
		String symbol = null;
505
		
505

  
506 506
		String onlineResource;
507 507
		if (status == null || status.getOnlineResource() == null)
508 508
			onlineResource = getHost();
509
		else 
509
		else
510 510
			onlineResource = status.getOnlineResource();
511 511
		symbol = getSymbol(onlineResource);
512
		
512

  
513 513
		req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WMS&");
514 514
		req.append("VERSION=").append(getVersion()).append("&EXCEPTIONS=XML");
515 515
		return req.toString();
516 516
    }
517
    
517

  
518 518
    /**
519 519
     * Builds the GetFeatureInfoRequest according to the OGC WMS Specifications
520 520
     */
......
527 527
		String onlineResource;
528 528
		if (status.getOnlineResource() == null)
529 529
			onlineResource = getHost();
530
		else 
530
		else
531 531
			onlineResource = status.getOnlineResource();
532 532
		symbol = getSymbol(onlineResource);
533
        
533

  
534 534
		req.append(onlineResource).append(symbol).append("REQUEST=GetFeatureInfo&SERVICE=WMS&");
535
		req.append("QUERY_LAYERS=").append(Utilities.Vector2CS(status.getLayerNames())); 
535
		req.append("QUERY_LAYERS=").append(Utilities.Vector2CS(status.getLayerNames()));
536 536
		req.append("&VERSION=").append(getVersion()).append("&INFO_FORMAT=application/vnd.ogc.gml&");
537 537
		req.append(getPartialQuery(status)).append("&x="+x + "&y="+y);
538 538
		//this parameter sets the max number of features per layer to be returned.
......
544 544
            req.append("&EXCEPTIONS=XML");
545 545
        }
546 546
		return req.toString().replaceAll(" ", "%20");
547
    }    
547
    }
548 548

  
549 549
    /**
550 550
     * Builds the GetMapRequest according to the OGC WMS Specifications
551 551
     */
552 552
    private String buildGetLegendGraphicRequest(WMSStatus status, String layerName)
553
    { 
553
    {
554 554
    	//TODO: pass by parameter the legend output format?
555 555
		StringBuffer req = new StringBuffer();
556 556
		String symbol = null;
557 557
		String onlineResource = null;
558
		
558

  
559 559
		if (status.getOnlineResource() == null)
560 560
			onlineResource = getHost();
561
		else 
561
		else
562 562
			onlineResource = status.getOnlineResource();
563 563
		symbol = getSymbol(onlineResource);
564
        
564

  
565 565
		req.append(onlineResource + symbol + "REQUEST=GetLegendGraphic&SERVICE=WMS&VERSION=").append(getVersion());
566 566
        req.append("&LAYER=" + layerName).append("&TRANSPARENT=TRUE").append("&FORMAT=image/png");
567 567
		return req.toString().replaceAll(" ", "%20");
......
571 571
     * Builds the GetMapRequest according to the OGC WMS Specifications
572 572
     */
573 573
    private String buildMapRequest(WMSStatus status)
574
    { 
574
    {
575 575
		StringBuffer req = new StringBuffer();
576 576
		String symbol = null;
577 577
		String onlineResource = null;
578
		
578

  
579 579
		if (status.getOnlineResource() == null)
580 580
			onlineResource = getHost();
581
		else 
581
		else
582 582
			onlineResource = status.getOnlineResource();
583 583
		symbol = getSymbol(onlineResource);
584
        
584

  
585 585
		req.append(onlineResource + symbol + "REQUEST=GetMap&SERVICE=WMS&VERSION=").append(getVersion()).append("&");
586 586
		req.append(getPartialQuery(status));
587 587
//        if (status.getExceptionFormat() != null) {
......
591 591
//        }
592 592
		return req.toString().replaceAll(" ", "%20");
593 593
    }
594
    
594

  
595 595
    /**
596 596
     * Just for not repeat code. Gets the correct separator according to the server URL
597 597
     * @param h
......
599 599
     */
600 600
    protected static String getSymbol(String h) {
601 601
        String symbol;
602
        if (h.indexOf("?")==-1) 
602
        if (h.indexOf("?")==-1)
603 603
            symbol = "?";
604 604
        else if (h.indexOf("?")!=h.length()-1)
605 605
            symbol = "&";
......
613 613
     * @return String request
614 614
     */
615 615
    public String getPartialQuery(WMSStatus status)
616
    {    	
616
    {
617 617
        StringBuffer req = new StringBuffer();
618 618
        req.append("LAYERS=" + Utilities.Vector2CS(status.getLayerNames()))
619 619
           .append("&SRS=" + status.getSrs())
......
627 627
           .append("&STYLES=");
628 628
        Vector v = status.getStyles();
629 629
        if (v!=null && v.size()>0)
630
        	req.append(Utilities.Vector2CS(v)); 
630
        	req.append(Utilities.Vector2CS(v));
631 631
        v = status.getDimensions();
632 632
        if (v!=null && v.size()>0)
633 633
            req.append("&" + Utilities.Vector2URLParamString(v));
......
637 637
        return req.toString();
638 638
    }
639 639

  
640
    
641
    
642
    public void close() {        
640

  
641

  
642
    public void close() {
643 643
        // your code here
644
    } 
645
    
644
    }
645

  
646 646
    /**
647 647
     * Inner class that represents the description of the WMS metadata.
648 648
     * The first part of the capabilities will return the service information
649
     * from the WMS, this class will hold this information. 
650
     * 
649
     * from the WMS, this class will hold this information.
650
     *
651 651
     */
652 652
    public class ServiceInformation {
653 653

  
......
676 676
        public String email;
677 677
        public Vector formats;
678 678
        public HashMap operations; // operations that WMS supports
679
        
679

  
680 680
        public ServiceInformation()
681
        {  	
681
        {
682 682
            version = new String();
683 683
            name = new String();
684 684
            scope = new String();
......
699 699
            phone = new String();
700 700
            fax = new String();
701 701
            email = new String();
702
            formats = new Vector();       	
703
            operations = new HashMap();            
702
            formats = new Vector();
703
            operations = new HashMap();
704 704
        }
705 705
        public boolean isQueryable()
706 706
        {
......
715 715
        		return true;
716 716
        	else
717 717
        		return false;
718
        }        
718
        }
719 719
        public void clear() {
720 720
        	version = new String();
721 721
            name = new String();
......
737 737
            phone = new String();
738 738
            fax = new String();
739 739
            email = new String();
740
            formats = new Vector();       	
741
            operations = new HashMap();            
740
            formats = new Vector();
741
            operations = new HashMap();
742 742
        }
743
     }   
743
     }
744 744
 }
trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/wms_1_1_1/WMSProtocolHandler1_1_1.java
8 8
import java.io.FileNotFoundException;
9 9
import java.io.FileReader;
10 10
import java.io.IOException;
11
import java.io.StringReader;
11 12
import java.util.ArrayList;
12 13
import java.util.TreeMap;
13 14

  
......
23 24
 */
24 25
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
25 26
	private WMSLayer1_1_1 fakeRootLayer;
26
    
27

  
27 28
	public WMSProtocolHandler1_1_1()
28 29
	{
29 30
		this.version = "1.1.1";
30 31
		this.name = "WMS1.1.1";
31
		this.serviceInfo = new ServiceInformation(); 
32
		this.serviceInfo = new ServiceInformation();
32 33
		this.layers = new TreeMap();
33 34
	}
34
    
35

  
35 36
//------------------------------------------------------------------------------
36
// Parsing methods....    
37
//------------------------------------------------------------------------------    
37
// Parsing methods....
38
//------------------------------------------------------------------------------
38 39
/**
39 40
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
40
 * 
41
 *
41 42
 */
42 43
    public void parse(File f)
43
    {      
44
    {
44 45
    	rootLayer = null;
45
    	FileReader reader = null;    	
46
    	FileReader reader = null;
46 47
    	try
47 48
    	{
48 49
    		reader = new FileReader(f);
49 50
    		BufferedReader br = new BufferedReader(reader);
50 51
    		char[] buffer = new char[100];
51 52
    		br.read(buffer);
52
    		StringBuffer st = new StringBuffer(new String(buffer));
53
    		String string = new String(buffer);
54

  
55
    		 // patch for ArcIMS + WMS connector > 9.0 bug
56
            int a = string.toLowerCase().indexOf("<?xml");
57
            if (a !=-1)
58
            	string = string.substring(a, string.length());
59
            // end patch
60

  
61
    		StringBuffer st = new StringBuffer(string);
53 62
    		String searchText = "encoding=\"";
54 63
    		int index = st.indexOf(searchText);
55 64
    		if (index>-1) {
......
61 70
    	} catch (IOException e) {
62 71
			e.printStackTrace();
63 72
		}
64
    	
73

  
65 74
    	int tag;
66 75
    	KXmlParser kxmlParser = null;
67 76
    	kxmlParser = new KXmlParser();
68 77
    	try
69 78
    	{
70
    		kxmlParser.setInput(new FileInputStream(f), encoding);
79
    		reader = new FileReader(f);
80
    		BufferedReader br = new BufferedReader(reader);
81

  
82
    		 // patch for ArcIMS + WMS connector > 9.0 bug
83
    		char[] buffer = new char[(int) f.length()];
84
    		br.read(buffer);
85
    		String string = new String(buffer);
86
    		int a = string.toLowerCase().indexOf("<?xml");
87
            if (a !=-1) {
88
            	string = string.substring(a, string.length());
89
            	kxmlParser.setInput(new StringReader(string));
90
            } else
91
            	// end patch
92
            	kxmlParser.setInput(new FileInputStream(f), encoding);
93

  
71 94
			kxmlParser.nextTag();
72
    		if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
73
    		{    		
74
    			kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);    			
95
    		if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
96
    		{
97
    			kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT1_1_1);
75 98
    			tag = kxmlParser.nextTag();
76 99
				 while(tag != KXmlParser.END_DOCUMENT)
77 100
				 {
78 101
                     switch(tag)
79 102
					 {
80
                         
103

  
81 104
						case KXmlParser.START_TAG:
82 105
							if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
83 106
							{
84 107
								parseServiceTag(kxmlParser);
85
							}	
108
							}
86 109
							else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
87 110
							{
88 111
								parseCapabilityTag(kxmlParser);
89 112
							}
90 113
							break;
91
						case KXmlParser.END_TAG:							
114
						case KXmlParser.END_TAG:
92 115
							break;
93 116
						case KXmlParser.TEXT:
94
							//System.out.println("[TEXT]["+kxmlParser.getText()+"]");							
117
							//System.out.println("[TEXT]["+kxmlParser.getText()+"]");
95 118
						break;
96 119
					 }
97 120
    				 tag = kxmlParser.next();
......
102 125
    	catch(XmlPullParserException parser_ex){
103 126
    		parser_ex.printStackTrace();
104 127
    	}
105
   		catch (IOException ioe) {			
128
   		catch (IOException ioe) {
106 129
   			ioe.printStackTrace();
107 130
		} finally {
108
            
131

  
109 132
        }
110
   		// In the parsing process the layer has been filled  		
111
    } 
112
    
133
   		// In the parsing process the layer has been filled
134
    }
135

  
113 136
    /**
114 137
     * <p>Parses the Service Information </p>
115
     */    
116
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
138
     */
139
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException
117 140
    {
118 141
    	int currentTag;
119 142
    	boolean end = false;
120
    	
143

  
121 144
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
122 145
    	currentTag = parser.next();
123
    	
124
    	while (!end) 
146

  
147
    	while (!end)
125 148
    	{
126 149
			 switch(currentTag)
127 150
			 {
128 151
				case KXmlParser.START_TAG:
129 152
					if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
130 153
					{
131
						serviceInfo.name = parser.nextText(); 
132
					}	
154
						serviceInfo.name = parser.nextText();
155
					}
133 156
					else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
134 157
					{
135
						serviceInfo.title = parser.nextText(); 
158
						serviceInfo.title = parser.nextText();
136 159
					}
137 160
					else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
138 161
					{
139
						serviceInfo.abstr = parser.nextText(); 
162
						serviceInfo.abstr = parser.nextText();
140 163
					}
141 164
					else if (parser.getName().compareTo(CapabilitiesTags.ONLINERESOURCE)==0)
142 165
					{
......
150 173
							(parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
151 174
					{
152 175
						parser.skipSubTree();
153
					}					
176
					}
154 177
					break;
155 178
				case KXmlParser.END_TAG:
156 179
					if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
157 180
						end = true;
158 181
					break;
159
				case KXmlParser.TEXT:					
182
				case KXmlParser.TEXT:
160 183
				break;
161 184
			 }
162 185
             if (!end)
......
166 189
    }
167 190
    /**
168 191
     * <p>Parses the Capability Tag </p>
169
     */    
192
     */
170 193
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
171
    { 	
194
    {
172 195
    	int currentTag;
173 196
    	boolean end = false;
174
 
197

  
175 198
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
176 199
    	currentTag = parser.next();
177
    	
178
    	while (!end) 
200

  
201
    	while (!end)
179 202
    	{
180 203
			 switch(currentTag)
181 204
			 {
182 205
				case KXmlParser.START_TAG:
183 206
					if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
184 207
					{
185
						parseRequestTag(parser); 
186
					}	
208
						parseRequestTag(parser);
209
					}
187 210
					else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
188 211
					{
189 212
						//TODO:
......
193 216
					{
194 217
						WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
195 218
						lyr.parse(parser, layers);
196
						
219

  
197 220
                        if (rootLayer == null)
198 221
                            rootLayer = lyr;
199 222
                        else {
......
206 229
                            }
207 230
                            rootLayer.getChildren().add(lyr);
208 231
                        }
209
			
232

  
210 233
                        if (lyr.getName()!=null)
211
						    layers.put(lyr.getName(), lyr); 
212
                                                
213
//                        Collection layerCollection = layers.values(); 
234
						    layers.put(lyr.getName(), lyr);
235

  
236
//                        Collection layerCollection = layers.values();
214 237
//                        Iterator iter = layerCollection.iterator();
215 238
//                        while (iter.hasNext())
216 239
//                        {
217
//                        	WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next(); 
240
//                        	WMSLayer1_1_1 layer = (WMSLayer1_1_1)iter.next();
218 241
//    						//Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
219 242
//    						for (i=0;i<layer.getAllSrs().size();i++)
220
//    						{   							
243
//    						{
221 244
////    						    if (!layer.srs.contains(layer.getAllSrs().elementAt(i)))
222 245
////    						    {
223 246
////    						        this.srs.add(layer.getAllSrs().elementAt(i));
224 247
////    						    }
225
//    						}                        	
226
//                        }						
248
//    						}
249
//                        }
227 250
					}
228 251
					else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
229 252
							(parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
230
                            
253

  
231 254
					{
232 255
						parser.skipSubTree();
233
					}					
256
					}
234 257
					break;
235 258
				case KXmlParser.END_TAG:
236 259
					if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
237 260
						end = true;
238 261
					break;
239
				case KXmlParser.TEXT:					
262
				case KXmlParser.TEXT:
240 263
				break;
241 264
			 }
242 265
			 if (!end)
243 266
				 currentTag = parser.next();
244 267
    	}
245
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);    	
268
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);
246 269
    }
247
    
270

  
248 271
    /**
249 272
     * <p>Parses the Request tag </p>
250
     */ 
273
     */
251 274
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
252
    {	
275
    {
253 276
    	int currentTag;
254 277
    	boolean end = false;
255
    	
278

  
256 279
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
257 280
    	currentTag = parser.next();
258
    	
259
    	while (!end) 
281

  
282
    	while (!end)
260 283
    	{
261 284
			 switch(currentTag)
262 285
			 {
263 286
				case KXmlParser.START_TAG:
264 287
					if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
265 288
					{
266
						serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null); 
267
					}	
289
						serviceInfo.operations.put(CapabilitiesTags.GETCAPABILITIES, null);
290
					}
268 291
					else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
269 292
					{
270 293
						// put a null to this key?
271 294
						// or leave it like it was with a CODE in a vector specifying this operation.
272
						// WMSProtocolHandler.GETMAP_OPERATION 
273
						serviceInfo.operations.put(CapabilitiesTags.GETMAP, null); 
295
						// WMSProtocolHandler.GETMAP_OPERATION
296
						serviceInfo.operations.put(CapabilitiesTags.GETMAP, null);
274 297
						parseGetMapTag(parser);
275 298
					}
276 299
					else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
......
278 301
						//serviceInfo.operations.put(WMSProtocolHandler.GETFEATUREINFO_OPERATION)
279 302
						serviceInfo.operations.put(CapabilitiesTags.GETFEATUREINFO, null);
280 303
						parseGetFeatureInfoTag(parser);
281
					}		
304
					}
282 305
					else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
283 306
					{
284 307
						//serviceInfo.operations.put(WMSProtocolHandler.DESCRIBELAYER_OPERATION)
285
						serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null); 
286
					}		
308
						serviceInfo.operations.put(CapabilitiesTags.DESCRIBELAYER, null);
309
					}
287 310
					else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
288 311
					{
289 312
						serviceInfo.operations.put(CapabilitiesTags.GETLEGENDGRAPHIC, null);
......
294 317
					if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
295 318
						end = true;
296 319
					break;
297
				case KXmlParser.TEXT:					
320
				case KXmlParser.TEXT:
298 321
				break;
299 322
			 }
300 323
			 if(!end)
301 324
				 currentTag = parser.next();
302 325
    	}
303 326
    	// TODO: does not get such a tag when arrives here!!!!!!
304
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);    	
327
    	//parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);
305 328
    }
306
    
329

  
307 330
    /**
308 331
     * <p>Parses the GetMap tag </p>
309
     */ 
332
     */
310 333
    private void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
311
    {	
334
    {
312 335
    	int currentTag;
313 336
    	boolean end = false;
314
    	
337

  
315 338
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
316 339
    	currentTag = parser.next();
317
    	
318
    	while (!end) 
340

  
341
    	while (!end)
319 342
    	{
320 343
			 switch(currentTag)
321 344
			 {
......
323 346
					if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
324 347
					{
325 348
						serviceInfo.formats.add(parser.nextText());
326
					}	
349
					}
327 350
					else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
328
					{			
351
					{
329 352
						currentTag = parser.nextTag();
330 353
						if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
331 354
						{
......
343 366
								}
344 367
							}
345 368
						}
346
					}			
369
					}
347 370
					break;
348 371
				case KXmlParser.END_TAG:
349 372
					if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
350 373
						end = true;
351 374
					break;
352
				case KXmlParser.TEXT:					
375
				case KXmlParser.TEXT:
353 376
				break;
354 377
			 }
355 378
			 if(!end)
356 379
				 currentTag = parser.next();
357
    	}	
358
    }    
359
    
380
    	}
381
    }
382

  
360 383
    /**
361 384
     * <p>Parses the GetFeatureInfoTag tag </p>
362
     */ 
385
     */
363 386
    private void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
364
    {	
387
    {
365 388
    	int currentTag;
366 389
    	boolean end = false;
367
    	
390

  
368 391
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
369 392
    	currentTag = parser.next();
370
    	
371
    	while (!end) 
393

  
394
    	while (!end)
372 395
    	{
373 396
			 switch(currentTag)
374 397
			 {
......
378 401
						//TODO:
379 402
						// add the supported formats by the GetFeatureInfo request
380 403
						//serviceInfo.formats.add(parser.nextText());
381
					}	
404
					}
382 405
					else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
383
					{			
406
					{
384 407
						currentTag = parser.nextTag();
385 408
						if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
386 409
						{
......
398 421
								}
399 422
							}
400 423
						}
401
					}			
424
					}
402 425
					break;
403 426
				case KXmlParser.END_TAG:
404 427
					if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
405 428
						end = true;
406 429
					break;
407
				case KXmlParser.TEXT:					
430
				case KXmlParser.TEXT:
408 431
				break;
409 432
			 }
410 433
			 if(!end)
411 434
				 currentTag = parser.next();
412
    	}	
413
    }         
435
    	}
436
    }
414 437

  
415 438
    /**
416 439
     * <p>Parses the GetFeatureInfoTag tag </p>
417
     */ 
440
     */
418 441
    private void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
419
    {	
442
    {
420 443
    	int currentTag;
421 444
    	boolean end = false;
422
    	
445

  
423 446
    	parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
424 447
    	currentTag = parser.next();
425
    	
426
    	while (!end) 
448

  
449
    	while (!end)
427 450
    	{
428 451
			 switch(currentTag)
429 452
			 {
......
433 456
						//TODO:
434 457
						// add the supported formats by the GetLegendGraphic request
435 458
						//serviceInfo.formats.add(parser.nextText());
436
					}	
459
					}
437 460
					else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
438
					{			
461
					{
439 462
						currentTag = parser.nextTag();
440 463
						if(parser.getName().compareTo(CapabilitiesTags.HTTP)==0)
441 464
						{
......
453 476
								}
454 477
							}
455 478
						}
456
					}			
479
					}
457 480
					break;
458 481
				case KXmlParser.END_TAG:
459 482
					if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
460 483
						end = true;
461 484
					break;
462
				case KXmlParser.TEXT:					
485
				case KXmlParser.TEXT:
463 486
				break;
464 487
			 }
465 488
			 if(!end)
466 489
				 currentTag = parser.next();
467
    	}	
468
    }    
469
    
470
    
490
    	}
491
    }
492

  
493

  
471 494
    private WMSLayer1_1_1 getFakeRootLayer(){
472 495
        if (fakeRootLayer == null){
473 496
            fakeRootLayer = new WMSLayer1_1_1();
......
485 508
        KXmlParser kxmlParser = new KXmlParser();
486 509
        try
487 510
        {
488
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);        
511
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
489 512
            int tag;
490
            
513

  
491 514
            boolean end = false;
492 515
            tag = kxmlParser.nextTag();
493
            
516

  
494 517
            //Comentar temporalmente para subsanar el hecho de que SimonCit me devuelve las capabilities en un GetLegendGraphic!!!
495 518
            kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
496
            
497
            while (!end) 
519

  
520
            while (!end)
498 521
            {
499 522
                switch(tag)
500 523
                {
......
506 529
                                errors.add(errorCode+errorMessage);
507 530
                            }
508 531
                            break;
509
                    case KXmlParser.END_TAG:   
532
                    case KXmlParser.END_TAG:
510 533
                            if (kxmlParser.getName().compareTo(ExceptionTags.EXCEPTION_ROOT) == 0)
511 534
                                end = true;
512
                            break; 
535
                            break;
513 536
                     }
514 537
                	if (!end)
515 538
                	{
......
521 544
            System.out.println(parser_ex.getMessage());
522 545
            parser_ex.printStackTrace();
523 546
        }
524
        catch (IOException ioe) {           
525
            ioe.printStackTrace();            
547
        catch (IOException ioe) {
548
            ioe.printStackTrace();
526 549
        }
527 550
        String message = errors.size()>0? "" : null;
528 551
        for (int i = 0; i < errors.size(); i++) {
......
530 553
        }
531 554
        return message;
532 555
    }
533
    
556

  
534 557
  }
trunk/libraries/libRemoteServices/src/org/gvsig/remoteClient/wms/WMSProtocolHandlerFactory.java
15 15

  
16 16
/**
17 17
 * <p></p>
18
 * 
18
 *
19 19
 */
20 20
public class WMSProtocolHandlerFactory {
21 21
/**
22 22
 * <p></p>
23
 * 
23
 *
24 24
 */
25 25
    public org.gvsig.remoteClient.wms.WMSProtocolHandler wMSProtocolHandler;
26 26

  
......
49 49
     * @return cliente cuya version es igual o inmediatamente inferior
50 50
     * @throws IllegalAccessException
51 51
     * @throws InstantiationException
52
     * 
52
     *
53 53
     */
54 54
    private static String getDriverVersion(String version, Iterator clients) throws InstantiationException, IllegalAccessException {
55 55
        while (clients.hasNext()) {
......
74 74
     */
75 75
     public static WMSProtocolHandler negotiate(String host) throws ConnectException, IOException {
76 76

  
77
        if (supportedVersions.size() == 0) 
77
        if (supportedVersions.size() == 0)
78 78
        {
79 79
        	return null;
80 80
        }
81 81

  
82 82
        try
83
        {        	
83
        {
84 84
        	String highestVersionSupportedByServer  = getSuitableWMSVersion(host,"");
85 85
        	if (supportedVersions.contains(highestVersionSupportedByServer))
86 86
        	{
87 87
        		//we support the highest version supported by the server
88
        		// this is the best case 
88
        		// this is the best case
89 89
        		return createVersionDriver(highestVersionSupportedByServer);
90 90
        	}
91
        	else
91

  
92

  
93
        else
92 94
        	{
93 95
        		// in case we dont support the highest version from the server
94 96
        		// we start the negotiation process in which we have to get the higest version
......
96 98
        		Iterator iVersion = supportedVersions.iterator();
97 99
        		String wmsVersion;
98 100
        		String gvSIGVersion;
99
	        
100
        		while (iVersion.hasNext()) { 
101

  
102
        		while (iVersion.hasNext()) {
101 103
		                gvSIGVersion = (String)iVersion.next();
102 104
		                wmsVersion = getSuitableWMSVersion(host,gvSIGVersion);
103 105
		                //TODO:
104 106
		                //compare with the version returned by the WMS!!!!!
105 107
		                // send GetCapabilities and read the version to compare.
106 108
		                int res = wmsVersion.compareTo(gvSIGVersion);
107
		
108
		                if (res == 0) { //Si es la misma que nuestra version                
109

  
110
		                if (res == 0) { //Si es la misma que nuestra version
109 111
		                    return createVersionDriver(gvSIGVersion);
110 112
		                } else if (res > 0) { //Si es mayor que nuestra version
111 113
		                    throw new Exception("Server Version too high: " + wmsVersion);
112 114
		                } else { //Si es menor que nuestra version
113 115
		                         //Obtenemos la primera version menor o igual que tengamos
114 116
		                    String lowerVersion = WMSProtocolHandlerFactory.getDriverVersion(wmsVersion, iVersion);
115
		
117

  
116 118
		                    if (lowerVersion == null) { //Si no hay ninguna
117 119
		                        throw new Exception("Lowest server version is " + wmsVersion);
118 120
		                    } else {
119
		                        if (lowerVersion.equals(wmsVersion)) { 
121
		                        if (lowerVersion.equals(wmsVersion)) {
120 122
		                            return createVersionDriver(lowerVersion);
121 123
		                        } else { //Si hay una version menor que la que retorno el servidor
122 124
		                            //iV = lower;
......
125 127
		                }
126 128
        		}
127 129
        	}//case we had to start the negotiation process.
128
	        return null; // if it did not find any suitable version.        
130
	        return null; // if it did not find any suitable version.
129 131
        }
130 132
        catch(ConnectException conEx)
131 133
        {
......
134 136
        catch(IOException ioEx)
135 137
        {
136 138
        	throw ioEx;
137
        }        
139
        }
138 140
        catch(Exception e)
139 141
        {
140 142
        	e.printStackTrace();
141 143
        	return null;
142 144
        }
143 145
    }
144
    
146

  
145 147
     /**
146 148
      * Sends a GetCapabilities to the WMS server to get the version
147 149
      * if the version parameter is null, the WMS will return the highest version supported
148 150
      * if not it will return the lower highest version than the one requested.
149 151
      * @param host
150 152
      * @param version
151
      * @return suitable version supported by the server 
153
      * @return suitable version supported by the server
152 154
      */
153
     private static String getSuitableWMSVersion(String host, String _version) throws ConnectException, IOException 
154
     {    	 
155
     private static String getSuitableWMSVersion(String host, String _version) throws ConnectException, IOException
156
     {
155 157
    	String request = WMSProtocolHandler.buildCapabilitiesSuitableVersionRequest(host, _version);
156
    	String version = new String(); 
158
    	String version = new String();
157 159
    	StringReader reader = null;
158 160
    	//InputStreamReader reader;
159 161
    	//InputStream is = null;
160 162
    	DataInputStream dis = null;
161 163
		try
162
		{   	
164
		{
163 165
			URL url = new URL(request);
164 166
            byte[] buffer = new byte[1024];//new byte[1024*256];
165
//            is = url.openStream();            
166
//            reader = new InputStreamReader(is);            
167
            //int numberOfBytes = is.read(buffer);            
167
//            is = url.openStream();
168
//            reader = new InputStreamReader(is);
169
            //int numberOfBytes = is.read(buffer);
168 170
            //String readed = new String(buffer);
169
            dis = new DataInputStream(url.openStream()); 
171
            dis = new DataInputStream(url.openStream());
170 172
            dis.readFully(buffer);
171
       
172
            reader = new StringReader(new String(buffer));
173
            String string = new String(buffer);
174

  
175
            // patch for ArcIMS + WMS connector > 9.0 bug
176
            int a = string.toLowerCase().indexOf("<?xml");
177
            if (a !=-1)
178
            	string = string.substring(a, string.length());
179
            // end patch
180

  
181

  
182
            reader = new StringReader(string);
173 183
	    	KXmlParser kxmlParser = null;
174 184
	    	kxmlParser = new KXmlParser();
175
    		kxmlParser.setInput(reader);		
185
    		kxmlParser.setInput(reader);
176 186
			kxmlParser.nextTag();
177
    		if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
178
    		{    		
187
    		if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
188
    		{
179 189
    			if ((kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_0)==0)
180 190
    				||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_1_1)==0)
181 191
    				||(kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITIES_ROOT1_3_0)==0))
......
183 193
    				version = kxmlParser.getAttributeValue("", CapabilitiesTags.VERSION);
184 194
    			}
185 195
    		}
186
    		// do not forget to close the Stream.    		
187
    		reader.close();    		
196
    		// do not forget to close the Stream.
197
    		reader.close();
188 198
    		dis.close();
189
			return version;		
199
			return version;
190 200
		}
191 201
		catch(ConnectException conEx)
192 202
		{
193
			throw new ConnectException(conEx.getMessage()); 			
203
			throw new ConnectException(conEx.getMessage());
194 204
		}
195 205
		catch(IOException ioEx)
196 206
		{
197
			throw new IOException(ioEx.getMessage()); 	
207
			throw new IOException(ioEx.getMessage());
198 208
		}
199 209
		catch(XmlPullParserException xmlEx)
200 210
		{
201 211
			xmlEx.printStackTrace();
202 212
			return "";
203 213
		}
204
		finally{		
214
		finally{
205 215
			if (reader != null)
206 216
			{
207 217
				try{
208 218
					reader.close();
209 219
				}catch(Exception ex){
210
					ex.printStackTrace(); 
220
					ex.printStackTrace();
211 221
				}
212 222
			}
213 223
			if (dis != null)
......
215 225
				try {
216 226
					dis.close();
217 227
				} catch(Exception ex) {
218
					ex.printStackTrace(); 
228
					ex.printStackTrace();
219 229
				}
220 230
			}
221
		}    	 
231
		}
222 232
     }
223
     
233

  
224 234
     /**
225 235
      * It creates an instance of a WMSDriver class.
226 236
      *
227 237
      * @param String, with the version of the driver to be created
228 238
      * @return WMSDriver.
229 239
      */
230
       private static WMSProtocolHandler createVersionDriver(String version)             
231
       {    	   
240
       private static WMSProtocolHandler createVersionDriver(String version)
241
       {
232 242
         try
233 243
         {
234 244
           Class driver;
......
242 252
           //throw new Exception("WMSDriverFactory. Unknown driver version " + e);
243 253
        	 return null;
244 254
         }
245
       }     
246
  
255
       }
256

  
247 257
 }

Also available in: Unified diff