Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / wms / wms_1_1_1 / WMSProtocolHandler1_1_1.java @ 3483

History | View | Annotate | Download (7.52 KB)

1

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

    
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.IOException;
8
import java.util.TreeMap;
9
import java.util.Vector;
10

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

    
15
/**
16
 * <p>
17
 * Describes the handler to comunicate to a WMS 1.1.1
18
 * </p>
19
 */
20
public class WMSProtocolHandler1_1_1 extends org.gvsig.remoteClient.wms.WMSProtocolHandler {
21
        
22
        public WMSProtocolHandler1_1_1()
23
        {
24
                this.version = "1.1.1";
25
                this.name = "WMS1.1.1";
26
                this.serviceInfo = new ServiceInformation(); 
27
                this.layers = new TreeMap();
28
                this.srs = new Vector();
29
        }
30
    
31
//------------------------------------------------------------------------------
32
// Parsing methods....    
33
//------------------------------------------------------------------------------    
34
/**
35
 * <p>Parse the xml data retrieved from the WMS, it will parse the WMS Capabilities</p>
36
 * 
37
 */
38
    public void parse(File f)
39
    {       
40
            FileReader reader = null;            
41
            try
42
            {
43
                    reader = new FileReader(f);
44
            }
45
            catch(FileNotFoundException ex)        {
46
                    ex.printStackTrace();
47
            }
48
            
49
            int tag;
50
            KXmlParser kxmlParser = null;
51
            kxmlParser = new KXmlParser();
52
            try
53
            {
54
                    kxmlParser.setInput(reader);                
55
                        kxmlParser.nextTag();
56
                    if ( kxmlParser.getEventType() != KXmlParser.END_DOCUMENT ) 
57
                    {                    
58
                            kxmlParser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT);                            
59
                            tag = kxmlParser.nextTag();
60
                                 while(tag != KXmlParser.END_DOCUMENT)
61
                                 {
62
                                         switch(tag)
63
                                         {
64
                                                case KXmlParser.START_TAG:
65
                                                        if (kxmlParser.getName().compareTo(CapabilitiesTags.SERVICE )==0)
66
                                                        {
67
                                                                parseServiceTag(kxmlParser);
68
                                                        }        
69
                                                        else if (kxmlParser.getName().compareTo(CapabilitiesTags.CAPABILITY)==0)
70
                                                        {
71
                                                                parseCapabilityTag(kxmlParser);
72
                                                        }
73
                                                        break;
74
                                                case KXmlParser.END_TAG:                                                        
75
                                                        break;
76
                                                case KXmlParser.TEXT:
77
                                                        System.out.println("[TEXT]["+kxmlParser.getText()+"]");                                                        
78
                                                break;
79
                                         }
80
                                     tag = kxmlParser.next();
81
                             }
82
//                            while (kxmlParser.nextTag() != KXmlParser.END_DOCUMENT) {
83
//                                    //readXMLData(kxmlParser);
84
//                            }
85
                            //kxmlParser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITIES_ROOT);
86
                            //kxmlParser.next();
87
                            kxmlParser.require(KXmlParser.END_DOCUMENT, null, null);                            
88
                    }
89
            }
90
            catch(XmlPullParserException parser_ex){
91
                    System.out.println(parser_ex.getMessage());
92
                    parser_ex.printStackTrace();
93
                    // TODO: Implement logic to handle the exceptions
94
            }
95
                   catch (IOException ioe) {                        
96
                           ioe.printStackTrace();
97
                           // TODO: Implement logic to handle the exceptions
98
                }                   
99
                   // In the parsing process the layer has been filled                  
100
    } 
101
    
102
    /**
103
     * <p>Parses the Service Information </p>
104
     */    
105
    private void parseServiceTag(KXmlParser parser) throws IOException, XmlPullParserException 
106
    {
107
            int currentTag;
108
            boolean end = false;
109
            
110
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.SERVICE);
111
            currentTag = parser.next();
112
            
113
            while (!end) 
114
            {
115
                         switch(currentTag)
116
                         {
117
                                case KXmlParser.START_TAG:
118
                                        if (parser.getName().compareTo(CapabilitiesTags.NAME)==0)
119
                                        {
120
                                                serviceInfo.name = parser.nextText(); 
121
                                        }        
122
                                        else if (parser.getName().compareTo(CapabilitiesTags.TITLE)==0)
123
                                        {
124
                                                serviceInfo.title = parser.nextText(); 
125
                                        }
126
                                        else if (parser.getName().compareTo(CapabilitiesTags.ABSTRACT)==0)
127
                                        {
128
                                                serviceInfo.abstr = parser.nextText(); 
129
                                        }
130
                                        else if ((parser.getName().compareTo(CapabilitiesTags.KEYWORDLIST)==0) ||
131
                                                        (parser.getName().compareTo(CapabilitiesTags.CONTACTINFORMATION)==0))
132
                                        {
133
                                                parser.skipSubTree();
134
                                        }                                        
135
                                        break;
136
                                case KXmlParser.END_TAG:
137
                                        if (parser.getName().compareTo(CapabilitiesTags.SERVICE) == 0)
138
                                                end = true;
139
                                        break;
140
                                case KXmlParser.TEXT:                                        
141
                                break;
142
                         }
143
                         currentTag = parser.next();
144
            }
145
//            parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.SERVICE);
146
    }
147
    /**
148
     * <p>Parses the Capability Tag </p>
149
     */    
150
    private void parseCapabilityTag(KXmlParser parser) throws IOException, XmlPullParserException
151
    {         
152
            int i;
153
            int currentTag;
154
            boolean end = false;
155
            
156
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.CAPABILITY);
157
            currentTag = parser.next();
158
            
159
            while (!end) 
160
            {
161
                         switch(currentTag)
162
                         {
163
                                case KXmlParser.START_TAG:
164
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST)==0)
165
                                        {
166
                                                parseRequestTag(parser); 
167
                                        }        
168
                                        else if (parser.getName().compareTo(CapabilitiesTags.EXCEPTION)==0)
169
                                        {
170
                                                //Parse exception tags...
171
                                        }
172
                                        else if (parser.getName().compareTo(CapabilitiesTags.LAYER)==0)
173
                                        {
174
                                                WMSLayer1_1_1 lyr = new WMSLayer1_1_1();
175
                        rootLayer = lyr;
176
                                                lyr.parse(parser, layers);
177
                                                //if((lyr != null) && (lyr.getName() != null))
178
                                                //{
179
                        if (lyr.getName()!=null)
180
                                                        layers.put(lyr.getName(), lyr);                                                         
181
                                                        //Add all the SRS that the layer supports to the WMSProtocolHandler if they dont exist already
182
                                                        for (i=0;i<lyr.getAllSrs().size();i++)
183
                                                        {
184
                                                                if (!this.srs.contains(lyr.getAllSrs().elementAt(i)))
185
                                                                {
186
                                                                        this.srs.add( lyr.getAllSrs().elementAt(i));
187
                                                                }
188
                                                        }
189
                                                //}
190
                                                //else
191
                                                //TODO: a layer without title is a parent layer.... not queryable
192
                                        }
193
                                        else if ((parser.getName().compareTo(CapabilitiesTags.VENDORSPECIFICCAPABILITIES)==0) ||
194
                                                        (parser.getName().compareTo(CapabilitiesTags.USERDEFINEDSYMBOLIZATION )==0))
195
                                        {
196
                                                parser.skipSubTree();
197
                                        }                                        
198
                                        break;
199
                                case KXmlParser.END_TAG:
200
                                        if (parser.getName().compareTo(CapabilitiesTags.CAPABILITY) == 0)
201
                                                end = true;
202
                                        break;
203
                                case KXmlParser.TEXT:                                        
204
                                break;
205
                         }
206
                         currentTag = parser.next();
207
            }
208
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.CAPABILITY);            
209
    }
210
    
211
    /**
212
     * <p>Parses the Request tag </p>
213
     */ 
214
    private void parseRequestTag(KXmlParser parser) throws IOException, XmlPullParserException
215
    {        
216
            int currentTag;
217
            boolean end = false;
218
            
219
            parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.REQUEST);
220
            currentTag = parser.next();
221
            
222
            while (!end) 
223
            {
224
                         switch(currentTag)
225
                         {
226
                                case KXmlParser.START_TAG:
227
                                        if (parser.getName().compareTo(CapabilitiesTags.GETCAPABILITIES)==0)
228
                                        {
229
                                        }        
230
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETMAP)==0)
231
                                        {
232
                                                currentTag = parser.nextTag();
233
                                                parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.FORMAT);  
234
                                                while ((currentTag == KXmlParser.START_TAG) &&
235
                                                                (parser.getName().compareTo(CapabilitiesTags.FORMAT)==0))
236
                                                {
237
                                                        parser.require(KXmlParser.START_TAG, null, CapabilitiesTags.FORMAT);
238
                                                        serviceInfo.formats.add(parser.nextText());
239
                                                        parser.nextTag();
240
                                                }                                                                                                                                                                
241
                                        }
242
                                        else if (parser.getName().compareTo(CapabilitiesTags.GETFEATUREINFO)==0)
243
                                        {
244
                                        }                                
245
                                        break;
246
                                case KXmlParser.END_TAG:
247
                                        if (parser.getName().compareTo(CapabilitiesTags.REQUEST) == 0)
248
                                                end = true;
249
                                        break;
250
                                case KXmlParser.TEXT:                                        
251
                                break;
252
                         }
253
                         currentTag = parser.next();
254
            }
255
            // TODO: does not get such a tag when arrives here!!!!!!
256
            //parser.require(KXmlParser.END_TAG, null, CapabilitiesTags.REQUEST);            
257
    }
258
    
259
  }