Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.remoteclient / src / main / java / org / gvsig / remoteclient / wms / WMSProtocolHandler.java @ 3325

History | View | Annotate | Download (25 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.wms;
25

    
26
import java.io.ByteArrayInputStream;
27
import java.io.File;
28
import java.io.FileInputStream;
29
import java.io.FileReader;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.URL;
33
import java.net.URLConnection;
34
import java.nio.ByteBuffer;
35
import java.nio.channels.FileChannel;
36
import java.util.ArrayList;
37
import java.util.Iterator;
38
import java.util.StringTokenizer;
39
import java.util.TreeMap;
40

    
41
import org.gvsig.compat.net.ICancellable;
42
import org.gvsig.remoteclient.exceptions.ServerErrorException;
43
import org.gvsig.remoteclient.exceptions.WMSException;
44
import org.gvsig.remoteclient.ogc.OGCProtocolHandler;
45
import org.gvsig.remoteclient.ogc.OGCServiceInformation;
46
import org.gvsig.remoteclient.utils.CapabilitiesTags;
47
import org.gvsig.remoteclient.utils.ExceptionTags;
48
import org.gvsig.remoteclient.utils.Utilities;
49
import org.gvsig.remoteclient.wms.request.WMSGetCapabilitiesRequest;
50
import org.gvsig.remoteclient.wms.request.WMSGetFeatureInfoRequest;
51
import org.gvsig.remoteclient.wms.request.WMSGetLegendGraphicRequest;
52
import org.gvsig.remoteclient.wms.request.WMSGetMapRequest;
53
import org.kxml2.io.KXmlParser;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56
import org.xmlpull.v1.XmlPullParserException;
57

    
58
/**
59
 * <p> Abstract class that represents handlers to comunicate via WMS protocol.
60
 * </p>
61
 *
62
 */
63
public abstract class WMSProtocolHandler extends OGCProtocolHandler {
64
    
65
    private static final Logger logger = LoggerFactory.getLogger(WMSProtocolHandler.class);
66
        /**
67
         * Encoding used to parse different xml documents.
68
         */
69
        protected String encoding = "UTF-8";
70
    /**
71
     * WMS metadata
72
     */
73
    protected WMSServiceInformation serviceInfo;
74
    public TreeMap layers;
75
    public WMSLayer rootLayer;
76

    
77
    /**
78
     * returns the alfanumeric information of the layers at the specified point.
79
     * the diference between the other getfeatureInfo method is that this will
80
     * be implemented by each specific version because the XML from the server will be
81
     * parsed and presented by a well known structure.
82
     */
83

    
84
    public String getName() {
85
            return name;
86
    }
87

    
88
    /*
89
     * (non-Javadoc)
90
     * @see org.gvsig.remoteClient.ogc.OGCProtocolHandler#getServiceInformation()
91
     */
92
    public OGCServiceInformation getServiceInformation() {
93
        return serviceInfo;
94
    }
95

    
96
    /**
97
         * <p>Builds a GetCapabilities request that is sent to the WMS
98
         * the response will be parse to extract the data needed by the
99
         * WMS client</p>
100
         * @param override, if true the previous downloaded data will be overridden
101
         */
102
    public void getCapabilities(WMSStatus status, boolean override, ICancellable cancel) {
103
            URL request = null;
104
                try {
105
                        request = new URL(buildCapabilitiesRequest(status));
106
                } catch(Exception e) {
107
                    logger.warn("Can't get capabilities, error building url.",e);
108
                }
109
                try {
110
                        if (override)
111
                                Utilities.removeURL(request);
112
                        File f = Utilities.downloadFile(request,"wms_capabilities.xml", cancel);
113
                        if (f == null)
114
                                return;
115
                        clear();
116
                        parseCapabilities(f);
117
            } catch(Exception e) {
118
                    logger.warn("Can't get capabilities.",e);
119
                }
120
    }
121

    
122
    private void clear() {
123
                layers.clear();
124
                serviceInfo.clear();
125
        }
126

    
127
        /**
128
     * <p>It will send a GetFeatureInfo request to the WMS
129
     * Parsing the response and redirecting the info to the WMS client</p>
130
     * TODO: return a stored file instead a String.
131
     */
132
    public String getFeatureInfo(WMSStatus status, int x, int y, int featureCount, ICancellable cancel)
133
    {
134
            StringBuffer output = new StringBuffer();
135
            String outputFormat = new String();
136
            String ServiceException = "ServiceExceptionReport";
137
            StringBuffer sb = new StringBuffer();
138
            sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
139
                try
140
                {
141
                        WMSGetFeatureInfoRequest request = createGetFeatureInfoRequest(status, x, y);
142
                        URL url = request.getURL();
143
                    outputFormat = url.openConnection().getContentType();
144
                    File f = request.sendRequest(cancel);
145
                        if (f == null){
146
                                return "";
147
                        }
148

    
149
                        FileReader fReader = new FileReader(f);
150
                        char[] buffer = new char[1024*256];
151
                        for (int i = fReader.read(buffer); i>0; i = fReader.read(buffer))
152
                    {
153
                            String str = new String(buffer,0,i);
154
                            output.append(str);
155
                    }
156
                    if ( (outputFormat == null) || (outputFormat.indexOf("xml") != -1)
157
                                    ||output.toString().toLowerCase().startsWith("<?xml")
158
                                    ||(outputFormat.indexOf("gml") != -1))
159
                    {
160
                            int tag;
161
                            KXmlParser kxmlParser = null;
162
                            kxmlParser = new KXmlParser();
163
                            //kxmlParser.setInput(new StringReader(output.toString()));
164
                            kxmlParser.setInput(new FileReader(f));
165

    
166
                            tag = kxmlParser.nextTag();
167
                            if (kxmlParser.getName().compareTo(ServiceException)==0)
168
                                {
169
                                    sb.append("<INFO>").append(parseException( output.toString().getBytes())).append("</INFO>");
170
                                    return sb.toString();
171
                                }
172
                                else if (kxmlParser.getName().compareToIgnoreCase("ERROR")==0)
173
                                {
174
                                        return output.toString();
175
                                }
176
                                else
177
                                {
178
                                        return output.toString();
179
                                }
180
                    }
181
                    else
182
                    {                  
183
                            //Para que funcione con el GetFeatureInfo Viewer generico hay que devolver:
184
                             return output.toString();
185
                    }
186
                }
187
            catch(XmlPullParserException parserEx)
188
            {
189
                    if (output.toString().toLowerCase().indexOf("xml") != -1)
190
                    {
191
                            return output.toString().trim();
192
                    }
193
                    else
194
                    {
195
                               sb.append("<INFO>").append("Info format not supported").append("</INFO>");
196
                        return sb.toString();
197
                    }
198
            }
199
            catch(Exception e)
200
            {
201
                logger.warn("Can't get information by point.",e);
202
                    sb.append("<INFO>").append("Info format not supported").append("</INFO>");
203
                    return sb.toString();
204

    
205
            }
206
    }
207
    /**
208
     * <p>Builds a GetMap request that is sent to the WMS
209
     * the response (image) will be redirect to the
210
     * WMS client</p>
211
     */
212
    public byte[] _getMap(WMSStatus status) throws ServerErrorException, WMSException
213
    {
214
            try
215
                {
216
                        //TODO:
217
                        //pass this buildXXXRequest to the WMSProtocolHandlerXXX: The request can depend on the WMS version.
218
                        WMSGetMapRequest request = createGetMapRequest(status);
219
                        URL url = request.getURL();
220
                        
221
                        URLConnection conn = url.openConnection();
222
                        logger.info(request.toString());
223
            String type = conn.getContentType();
224

    
225

    
226
                    byte[] imageBytes = null;
227
                    byte[] buffer = new byte[1024*256];
228
            InputStream is = conn.getInputStream();
229
                    int readed = 0;
230

    
231
                    for (int i = is.read(buffer); i>0; i = is.read(buffer)){
232
                // Creates a new buffer to contain the previous readed bytes and the next bunch of bytes
233
                            byte[] buffered = new byte[readed+i];
234
                            for (int j = 0; j < buffered.length; j++) {
235
                                    if (j<readed){
236
                        // puts the previously downloaded bytes into the image buffer
237
                                            buffered[j] = imageBytes[j];
238
                                    }
239
                                    else {
240
                        // appends the recently downloaded bytes to the image buffer.
241
                                            buffered[j] = buffer[j-readed];
242
                                    }
243
                                }
244
                            imageBytes = (byte[]) buffered.clone();
245
                            readed += i;
246
                    }
247

    
248
                    if ((type !=null && !type.subSequence(0,5).equals("image"))
249
                            ||(Utilities.isTextData(imageBytes)))
250
                    {
251
                       WMSException wmsEx = null;
252

    
253
                    String exceptionMessage = parseException(imageBytes);
254
                if (exceptionMessage==null)
255
                {
256
                         String error = new String(imageBytes);
257
                        int pos = error.indexOf("<?xml");
258
                        if (pos!= -1)
259
                        {
260
                                String xml = error.substring(pos,error.length());
261
                                exceptionMessage = parseException(xml.getBytes());
262
                        if (exceptionMessage == null)
263
                                exceptionMessage = new String(imageBytes);
264
                        }
265
                }
266
                     wmsEx = new WMSException(exceptionMessage);
267
                    wmsEx.setWMSMessage(new String(imageBytes));
268
                throw wmsEx;
269
            }
270
                        return imageBytes;
271
                }
272
                catch(IOException e)
273
                {
274
                    logger.warn("Can't build map request.",e);
275
                    throw new ServerErrorException();
276
                }
277
    }
278

    
279
    public File getLegendGraphic_old(WMSStatus status, String layerName, ICancellable cancel) throws ServerErrorException, WMSException
280
    {
281
            try
282
                {
283
                        WMSGetLegendGraphicRequest request = createGetLegendGraphicRequest(status, layerName);
284
                        File f = request.sendRequest(cancel);
285
                    if (f== null)
286
                            return null;
287
            if (Utilities.isTextFile(f)) {
288
                            
289
                byte[] data = fileToBytes(f);
290

    
291
                            WMSException wmsEx = null;
292

    
293
                    String exceptionMessage = parseException(data);
294
                if (exceptionMessage==null)
295
                {
296
                         String error = new String(data);
297
                        int pos = error.indexOf("<?xml");
298
                        if (pos!= -1)
299
                        {
300
                                String xml = error.substring(pos,error.length());
301
                                exceptionMessage = parseException(xml.getBytes());
302
                        }
303
                    if (exceptionMessage == null)
304
                            exceptionMessage = new String(data);
305

    
306
                }
307
                     wmsEx = new WMSException(exceptionMessage);
308
                    wmsEx.setWMSMessage(new String(data));
309
                    downloader.removeURL(request);
310
                throw wmsEx;
311
            }
312
                        return f;
313
                }
314
                catch(IOException e)
315
                {
316
                        logger.warn("Can't get legend graphics.",e);
317
                        throw new ServerErrorException();
318
                }
319
    }
320
    
321
    public File getLegendGraphic(WMSStatus status, String layerName, ICancellable cancel) 
322
            throws ServerErrorException, WMSException {
323
            URL request = null;
324
                try {
325
                        request = new URL(buildGetLegendGraphicRequest(status, layerName));
326
                        logger.info(request.toString());
327
            File f = Utilities.downloadFile(request, "wmsGetLegendGraphic", cancel);
328
                    if (f== null)
329
                            return null;
330
            if (Utilities.isTextFile(f)) {
331
                            FileInputStream fis = new FileInputStream(f);
332
                            FileChannel fc = fis.getChannel();
333
                            byte[] data = new byte[(int)fc.size()];
334
                            ByteBuffer bb = ByteBuffer.wrap(data);
335
                            fc.read(bb);
336

    
337
                            WMSException wmsEx = null;
338

    
339
                    String exceptionMessage = parseException(data);
340
                if (exceptionMessage == null) {
341
                         String error = new String(data);
342
                        int pos = error.indexOf("<?xml");
343
                        if (pos!= -1) {
344
                                String xml = error.substring(pos,error.length());
345
                                exceptionMessage = parseException(xml.getBytes());
346
                        }
347
                    if (exceptionMessage == null)
348
                            exceptionMessage = new String(data);
349

    
350
                }
351
                     wmsEx = new WMSException(exceptionMessage);
352
                    wmsEx.setWMSMessage(new String(data));
353
                    Utilities.removeURL(request);
354
                throw wmsEx;
355
            }
356
                        return f;
357
                } catch(IOException e) {
358
                        logger.warn("Can't get legend graphics",e);
359
                        throw new ServerErrorException();
360
                }
361
    }
362
    
363
    /**
364
     * @return string that represents the url for getting the wms legend
365
     * If the layer has the object layer-->style-->legendurl that url will be returned 
366
     * otherwise builds the getLegendGraphic according to the OGC WMS Specifications
367
     * 
368
     */
369
    private String buildGetLegendGraphicRequest(WMSStatus status, String layerName) {
370
            //TODO: deal with more than one layer            
371
            WMSLayer lyr = (WMSLayer) this.layers.get(layerName);
372
            WMSStyle sty =null;
373
            if (lyr != null){
374
                    Iterator it = lyr.getStyles().iterator();
375
                    while (it.hasNext()){
376
                            sty = (WMSStyle) it.next();
377
                            if (sty.getName().equals(status.getStyles().get(0).toString())){                                    
378
                                    return sty.getLegendURLOnlineResourceHRef();
379
                            }
380
                    }
381
            }
382
            //TODO: pass by parameter the legend output format?
383
                StringBuffer req = new StringBuffer();
384
                String symbol = null;
385
                String onlineResource = null;
386

    
387
                if (status.getOnlineResource() == null)
388
                        onlineResource = getHost();
389
                else
390
                        onlineResource = status.getOnlineResource();
391
                symbol = getSymbol(onlineResource);
392

    
393
                req.append(onlineResource + symbol + "REQUEST=GetLegendGraphic&SERVICE=WMS&VERSION=").append(getVersion());
394
        req.append("&LAYER=" + layerName).append("&TRANSPARENT=TRUE").append("&FORMAT=image/png");
395
        String aux = req.toString().replaceAll(" ", "%20");
396
        logger.info("GetLegendGraphic url:" + aux);
397
                return aux;
398
    }
399
    
400
    public URL getMapURL(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException {
401
            try {
402
                        WMSGetMapRequest request = createGetMapRequest(status);
403
                        return request.getURL();
404
                } catch(IOException e) {
405
                    logger.warn("Can't get map URL",e);
406
                    throw new ServerErrorException();
407
                }
408
    }
409
    
410
    /**
411
     * Returns the exception message if the file is a XML instead of a image.
412
     * @param file3
413
     * @return
414
     * @throws IOException 
415
     */
416
    public String getExceptionMessage(File f) throws IOException {
417
            if (f == null)
418
                    return null;
419
            
420
        if (Utilities.isTextFile(f)) {
421
            byte[] data = fileToBytes(f);
422

    
423
                String exceptionMessage = parseException(data);
424
            if (exceptionMessage == null) {
425
                     String error = new String(data);
426
                    int pos = error.indexOf("<?xml");
427
                    if (pos!= -1) {
428
                            String xml = error.substring(pos,error.length());
429
                            exceptionMessage = parseException(xml.getBytes());
430
                    }
431
                if (exceptionMessage == null)
432
                        exceptionMessage = new String(data);
433
            }
434
                 return exceptionMessage;
435
        }
436
        return null;
437
    }
438

    
439
    public File getMap(WMSStatus status, ICancellable cancel) throws ServerErrorException, WMSException {
440
            try {
441
                        WMSGetMapRequest request = createGetMapRequest(status);
442
                        File f = request.sendRequest(cancel);
443
                        
444
                        if (f== null)
445
                            return null;
446
            if (Utilities.isTextFile(f)) {
447
                byte[] data = fileToBytes(f);
448

    
449
                            WMSException wmsEx = null;
450

    
451
                    String exceptionMessage = parseException(data);
452
                if (exceptionMessage==null) {
453
                         String error = new String(data);
454
                        int pos = error.indexOf("<?xml");
455
                        if (pos!= -1) {
456
                                String xml = error.substring(pos,error.length());
457
                                exceptionMessage = parseException(xml.getBytes());
458
//                        if (exceptionMessage == null)
459
//                                exceptionMessage = new String(data);
460
                        }
461
                    if (exceptionMessage == null)
462
                            exceptionMessage = new String(data);
463

    
464
                }
465
                     wmsEx = new WMSException(exceptionMessage);
466
                    wmsEx.setWMSMessage(new String(data));
467

    
468
                    // Since it is an error file, It must be deleted from the cache
469
                    downloader.removeURL(request);
470
                throw wmsEx;
471
            }
472
                        return f;
473
                } catch(IOException e) {
474
            throw new ServerErrorException(e.getMessage(), e);
475
                }
476
    }
477

    
478

    
479
    /* (non-Javadoc)
480
     * @see org.gvsig.remoteClient.wms.WMSProtocolHandler#parseException(byte[])
481
     */
482
    protected String parseException(byte[] data) {
483
        ArrayList errors = new ArrayList();
484
        KXmlParser kxmlParser = new KXmlParser();
485
        try
486
        {
487
            kxmlParser.setInput(new ByteArrayInputStream(data), encoding);
488
            kxmlParser.nextTag();
489
            int tag;
490
            if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT )
491
            {
492
                kxmlParser.require(KXmlParser.START_TAG, null, ExceptionTags.EXCEPTION_ROOT);
493
                tag = kxmlParser.nextTag();
494
                 while(tag != KXmlParser.END_DOCUMENT)
495
                 {
496
                     switch(tag)
497
                     {
498
                        case KXmlParser.START_TAG:
499
                            if (kxmlParser.getName().compareTo(ExceptionTags.SERVICE_EXCEPTION)==0){
500
                                String errorCode = kxmlParser.getAttributeValue("", ExceptionTags.CODE);
501
                                errorCode = (errorCode != null) ? "["+errorCode+"] " : "";
502
                                String errorMessage = kxmlParser.nextText();
503
                                errors.add(errorCode+errorMessage);
504
                            }
505
                            break;
506
                        case KXmlParser.END_TAG:
507
                            break;
508

    
509
                     }
510
                     tag = kxmlParser.nextTag();
511
                 }
512
                 //kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);
513
            }
514
        }
515
        catch(XmlPullParserException parser_ex){
516
            logger.warn("",parser_ex);
517
        }
518
        catch (IOException ioe) {
519
            logger.warn("",ioe);
520
        }
521
        String message = errors.size()>0? "" : null;
522
        for (int i = 0; i < errors.size(); i++) {
523
            message += (String) errors.get(i)+"\n";
524
        }
525
        return message;
526
    }
527
    /**
528
     * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
529
     * without a VERSION, to get the highest version than a WMS supports.
530
     */
531
    public static String buildCapabilitiesSuitableVersionRequest(String _host, String _version)
532
    {
533
                int index = _host.indexOf('?');
534
                
535
                if (index > -1) {
536
                        String host = _host.substring(0, index + 1);
537
                        String query = _host.substring(index + 1, _host.length());
538
                        
539
                        StringTokenizer tokens = new StringTokenizer(query, "&");
540
                        String newQuery = "", token;
541

    
542
                        // If there is a field or a value with spaces, (and then it's on different tokens) -> unify them
543
                        while (tokens.hasMoreTokens()) {
544
                                token = tokens.nextToken().trim();
545

    
546
                                if (token.toUpperCase().compareTo("REQUEST=GETCAPABILITIES") == 0)
547
                                        continue;
548

    
549
                                if (token.toUpperCase().compareTo("SERVICE=WMS") == 0)
550
                                        continue;
551

    
552
                                if ((_version != null) && (_version.length() > 0)) {
553
                                    if (token.toUpperCase().compareTo("VERSION=" + _version) == 0)
554
                                            continue;
555
                                }
556

    
557
                                if (token.toUpperCase().compareTo("EXCEPTIONS=XML") == 0)
558
                                        continue;
559

    
560
                                newQuery += token + "&";
561
                        }
562

    
563
                _host = host + newQuery;
564
                }
565
                else {
566
                        _host += "?";
567
                }
568

    
569
            if ((_version != null) && (_version.compareTo("") != 0))
570
                    _host += "REQUEST=GetCapabilities&SERVICE=WMS&VERSION=" + _version;
571
            else
572
                    _host += "REQUEST=GetCapabilities&SERVICE=WMS";
573

    
574
            return _host;
575
    }
576

    
577
    /**
578
     * Builds the GetCapabilitiesRequest according to the OGC WMS Specifications
579
     * @param WMSStatus
580
     */
581
    private String buildCapabilitiesRequest(WMSStatus status)
582
    {
583
                StringBuffer req = new StringBuffer();
584
                String symbol = null;
585

    
586
                String onlineResource;
587
                if (status == null || status.getOnlineResource() == null)
588
                        onlineResource = getHost();
589
                else
590
                        onlineResource = status.getOnlineResource();
591
                symbol = getSymbol(onlineResource);
592

    
593
                req.append(onlineResource).append(symbol).append("REQUEST=GetCapabilities&SERVICE=WMS&");
594
                req.append("VERSION=").append(getVersion());
595
                return req.toString();
596
    }  
597
   
598
     public void close() {
599
        // your code here
600
    } 
601
     
602
     /**
603
          * @param status
604
          * The WMS status
605
          * @param protocolHandler
606
          * The handler to parse the requests
607
          * @return an object to send the GetMap requests
608
          */
609
         protected abstract WMSGetMapRequest createGetMapRequest(WMSStatus status);
610
         
611
         protected abstract WMSGetFeatureInfoRequest createGetFeatureInfoRequest(WMSStatus status, int x, int y);
612
         
613
         protected abstract WMSGetLegendGraphicRequest createGetLegendGraphicRequest(WMSStatus status, String layerName);
614

    
615
         protected abstract WMSGetCapabilitiesRequest createGetCapabilitiesRequest(WMSStatus status);
616
         
617
         /**
618
     * <p>Parses the Request tag </p>
619
     */ 
620
    protected void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
621
    {        
622
            int currentTag;
623
            boolean end = false;
624
            
625
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
626
            currentTag = parser.next();
627
            
628
            while (!end) 
629
            {
630
                         switch(currentTag)
631
                         {
632
                                case KXmlParser.START_TAG:
633
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
634
                                        {
635
                                                parserDcpType(parser, CapabilitiesTags.GETCAPABILITIES);
636
                                        }        
637
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
638
                                        {        
639
                                                parseGetMapTag(parser);                                                
640
                                        }
641
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
642
                                        {
643
                                                parseGetFeatureInfoTag(parser);
644
                                        }                
645
                                        else if (parser.getName().compareTo(CapabilitiesTags.DESCRIBELAYER)==0)
646
                                        {
647
                                                parserDcpType(parser, CapabilitiesTags.DESCRIBELAYER);
648
                                        }        
649
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC)==0)
650
                                        {
651
                                                parseGetLegendGraphicTag(parser);
652
                                        }                                        
653
                                        break;
654
                                case KXmlParser.END_TAG:
655
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
656
                                                end = true;
657
                                        break;
658
                                case KXmlParser.TEXT:                                        
659
                                break;
660
                         }
661
                         if(!end)
662
                                 currentTag = parser.next();
663
            }
664
            // TODO: does not get such a tag when arrives here!!!!!!
665
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
666
    }   
667
          /**
668
     * <p>Parses the GetMap tag </p>
669
     */ 
670
    protected void parseGetMapTag(KXmlParser parser) throws IOException, XmlPullParserException
671
    {        
672
            int currentTag;
673
            boolean end = false;
674
            
675
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETMAP);
676
            currentTag = parser.next();
677
            
678
            while (!end) 
679
            {
680
                         switch(currentTag)
681
                         {
682
                                case KXmlParser.START_TAG:
683
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
684
                                        {
685
                                                serviceInfo.formats.add(parser.nextText());
686
                                        }        
687
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
688
                                        {                
689
                                                parserDcpType(parser, CapabilitiesTags.GETMAP);                                                
690
                                        }                        
691
                                        break;
692
                                case KXmlParser.END_TAG:
693
                                        if (parser.getName().compareTo(CapabilitiesTags.GETMAP) == 0)
694
                                                end = true;
695
                                        break;
696
                                case KXmlParser.TEXT:                                        
697
                                break;
698
                         }
699
                         if(!end)
700
                                 currentTag = parser.next();
701
            }        
702
    }    
703
    
704
    /**
705
     * <p>Parses the GetFeatureInfo tag </p>
706
     */ 
707
    protected void parseGetFeatureInfoTag(KXmlParser parser) throws IOException, XmlPullParserException
708
    {        
709
            int currentTag;
710
            boolean end = false;
711
            
712
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETFEATUREINFO);
713
            currentTag = parser.next();
714
            
715
            while (!end) 
716
            {
717
                         switch(currentTag)
718
                         {
719
                                case KXmlParser.START_TAG:
720
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
721
                                        {
722
                                                serviceInfo.infoformats.add(parser.nextText());
723
                                        }        
724
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
725
                                        {                        
726
                                                parserDcpType(parser, CapabilitiesTags.GETFEATUREINFO);                
727
                                        }                        
728
                                        break;
729
                                case KXmlParser.END_TAG:
730
                                        if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO) == 0)
731
                                                end = true;
732
                                        break;
733
                                case KXmlParser.TEXT:                                        
734
                                break;
735
                         }
736
                         if(!end)
737
                                 currentTag = parser.next();
738
            }        
739
    }     
740
 
741
    /**
742
     * <p>Parses the GetLegendGraphic tag </p>
743
     */ 
744
    protected void parseGetLegendGraphicTag(KXmlParser parser) throws IOException, XmlPullParserException
745
    {        
746
            int currentTag;
747
            boolean end = false;
748
            
749
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.GETLEGENDGRAPHIC);
750
            currentTag = parser.next();
751
            
752
            while (!end) 
753
            {
754
                         switch(currentTag)
755
                         {
756
                                case KXmlParser.START_TAG:
757
                                        if (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0)
758
                                        {
759
                                                //TODO:
760
                                                // add the supported formats by the GetLegendGraphic request
761
                                                //serviceInfo.formats.add(parser.nextText());
762
                                        }        
763
                                        else if (parser.getName().compareTo(CapabilitiesTags.DCPTYPE)==0)
764
                                        {                        
765
                                                parserDcpType(parser, CapabilitiesTags.GETLEGENDGRAPHIC);                
766
                                        }                        
767
                                        break;
768
                                case KXmlParser.END_TAG:
769
                                        if (parser.getName().compareTo(CapabilitiesTags.GETLEGENDGRAPHIC) == 0)
770
                                                end = true;
771
                                        break;
772
                                case KXmlParser.TEXT:                                        
773
                                break;
774
                         }
775
                         if(!end)
776
                                 currentTag = parser.next();
777
            }        
778
    }
779
 }