Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClientOld / wms / ParserManager.java @ 3321

History | View | Annotate | Download (11 KB)

1
package org.gvsig.remoteClientOld.wms;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.BufferedOutputStream;
5
import java.io.BufferedReader;
6
import java.io.DataOutputStream;
7
import java.io.File;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.io.Reader;
13
import java.io.StringReader;
14
import java.net.URL;
15
import java.util.Stack;
16
import java.util.TreeMap;
17

    
18
import org.gvsig.remoteClientOld.descriptor.Capabilities;
19
import org.gvsig.remoteClientOld.descriptor.Layer;
20
import org.gvsig.remoteClientOld.utils.CapabilitiesTags;
21
import org.gvsig.remoteClientOld.utils.DumbXMLParser;
22

    
23
public class ParserManager 
24
{
25
        private final static int NOWHERE                = 0x000;
26
        private final static int INSERVICE                = 0x001;
27
        private final static int INCAPABILITY        = 0x002;
28
        private final static int INREQUEST                = 0x004;
29
        private final static int INGETCAPABILITIES = 0x008;
30
        private final static int INGETMAP                = 0x010;
31
        private final static int INLAYER                = 0x040;
32
        private final static int INSTYLE                = 0x080;
33
        private final static int INBBOX                        = 0x100;
34
        private final static int INABSTRACT                = 0x400;
35
        private final static int INEX_GeographicBoundingBox = 0x200;
36
        private final static String LT="<", GT=">";
37
        private int where = NOWHERE;
38
        
39
        private Stack layerStack = new Stack();
40
        
41
        private WMSLayer currentLayer = null;
42
        private WMSStyle currentStyle = null;
43
        private String savedLine = "";
44
        
45
        private DumbXMLParser parser = new DumbXMLParser();
46
        Capabilities capabilities;
47
        
48
        double minX, minY, maxX, maxY;
49

    
50
    public File downloadFile(URL url, String fName) throws IOException 
51
    {
52
            File f = null;
53
                   f = new File(fName);
54
            
55
            if (f.exists())        return f;
56
            
57
            DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)));
58
            byte[] buffer = new byte[1024*256];
59
            InputStream is = url.openStream();
60
            long readed = 0;
61
            for (int i = is.read(buffer); i>0; i = is.read(buffer)){
62
                    dos.write(buffer, 0, i);
63
                    readed += i;
64
            }
65
            dos.close();
66
            return f;
67
        }
68
    
69
        public Capabilities parseCapabilities(String caps)
70
        {                
71
                StringReader sr = new StringReader(caps);
72
                return parseCapabilities(sr);
73
        }    
74
    
75
    public Capabilities parseCapabilities(Reader reader) 
76
    {
77
            capabilities = new Capabilities();
78
            String strBuf;
79
            int l=0;
80
            BufferedReader brd;
81
            where = NOWHERE;
82
            currentLayer = null;
83
            savedLine = "";
84
            
85
                try {
86
                        brd = new BufferedReader(reader);
87
                        while ((strBuf = brd.readLine()) != null) {
88
                                l++;
89
                                parseLine((savedLine+" "+strBuf).trim());
90
                        }
91
                        brd.close();
92
                        System.out.println("Capabilities parseado. ("+l+" l?neas).");
93
                } catch (FileNotFoundException e) {
94
                        e.printStackTrace();
95
                } catch (Exception ie) {
96
                        System.err.println("ERROR."+l+"lineas leidas");
97
                        ie.printStackTrace();
98
                }                
99
                return capabilities;
100
        }
101
    
102
        private void parseLine(String l) 
103
        {
104
                String token = null;
105

    
106
                if (l.length()>2 && l.substring(0,1).compareTo(LT) == 0) {
107
                        token = l.substring(1);//,l.indexOf(GT));
108
                        if (l.indexOf(GT) < 0) {
109
                                savedLine = l;
110
                                return;
111
                        } else
112
                                savedLine = "";
113
                        
114
                        /* Parsing de los casos del tipo
115
                         * <Style> <Name>all</Name> <Title>SRTM average reflectance, grayscale</Title> </Style> */
116
                        // si hay m?s de dos tokens en la l?nea
117
                        if (parser.countTokens(l) > 2) {
118
                                String t1 = parser.getToken(l,0);
119
                                String t2 = parser.getToken(l,1);
120
                                // si los dos primeros tokens no forman pareja
121
                                if (t2.compareTo("/"+t1) != 0) {                                        
122
                                        //System.out.println(t1+"<>"+t2+" : "+l);
123
                                        int pos = l.indexOf(LT, 1);
124
                                        parseLine(l.substring(0, pos).trim());
125
                                        parseLine(l.substring(pos).trim());
126
                                        return;
127
                                        // si los dos primeros tokens forman pareja
128
                                } else {
129
                                        //System.out.println(t1+"=="+t2+" : "+l);
130
                                        int pos = l.indexOf(LT,
131
                                                        l.indexOf(LT, 1)+1);
132
                                        parseLine(l.substring(0, pos).trim());
133
                                        parseLine(l.substring(pos).trim());
134
                                        return;
135
                                }
136
                        }                        
137
                        
138
                        if (token.startsWith(CapabilitiesTags.CAPABILITIES_ROOT) ||
139
                                        token.startsWith("WMS_Capabilities")) {
140
                                TreeMap pairs = parser.getPairs(l);
141
                                capabilities.getServiceInformation().setVersion((String)pairs.get(CapabilitiesTags.VERSION));
142
                        } else if (token.startsWith(CapabilitiesTags.SERVICE)) {
143
                                where |= INSERVICE;
144
                        } else if ((where & INSERVICE) == INSERVICE) {
145
                                parsingService(l,token);
146
                        } else if (token.startsWith(CapabilitiesTags.CAPABILITY)) {
147
                                where |= INCAPABILITY;
148
                        } else if ((where & INCAPABILITY) == INCAPABILITY) {
149
                                parsingCapability(l,token);
150
                        }
151
                }
152
        }
153

    
154
        protected void parsingService(String l, String token) {
155
                if (token.startsWith("/" +CapabilitiesTags.SERVICE)) {
156
                        where &= INSERVICE ^ 0xffff;
157
                }
158
        }
159
        
160
        protected void parsingCapability(String l, String token) {
161
                if (token.startsWith("/"+ CapabilitiesTags.CAPABILITY)) {
162
                        where &= INCAPABILITY ^ 0xffff;
163
                } else if (token.startsWith(CapabilitiesTags.LAYER)) {
164
                        if ((where & INLAYER) != INLAYER) 
165
                                where |= INLAYER;
166
                        //layerNr++;
167
                        Layer parentLayer = currentLayer;
168
                        if (currentLayer != null) {
169
                                layerStack.push(currentLayer);
170
                                //System.out.println("Eltos +:"+layerStack.size());
171
                                for (int i=0; i<layerStack.size(); i++)
172
                                        System.out.print("-");
173
                        }
174
                        currentLayer = new WMSLayer();
175
                        currentLayer.setParent(parentLayer);
176
                        //System.out.println("+Layer "+(layerNr)+":");
177
                } else if ((where & INLAYER) == INLAYER) {
178
                        parsingLayer(l,token);
179
                } else if (token.startsWith(CapabilitiesTags.REQUEST)) {
180
                        where |= INREQUEST;
181
                } else if ((where & INREQUEST) == INREQUEST) {
182
                        parsingRequest(l,token);
183
                } 
184
        }
185
        
186
        protected void parsingRequest(String l, String token) {
187
                if (token.startsWith("/" + CapabilitiesTags.REQUEST)) {
188
                        where &= INREQUEST ^ 0xffff;
189
                } else if (token.startsWith(CapabilitiesTags.GETMAP)) {
190
                        where |= INGETMAP;
191
                } else if ((where & INGETMAP) == INGETMAP) {
192
                        if (token.startsWith("/" + CapabilitiesTags.GETMAP)) {
193
                                where &= INGETMAP ^ 0xffff;
194
                        } else if (token.startsWith(CapabilitiesTags.FORMAT)) {
195
                                capabilities.getServiceInformation().mapRequestInfo.formats.add(parser.getValue(l));
196
                                //System.out.println("<Format>"+imageFormats.lastElement()+"</Format>");
197
                        }
198
                } 
199
        }
200
        
201
        protected void parsingLayer(String l, String token) {
202
                if (token.startsWith("/" + CapabilitiesTags.LAYER)) {
203
                        capabilities.getMapComposition().addLayer(currentLayer);
204
                        if (layerStack.empty()) {
205
                                where &= INLAYER ^ 0xffff;
206
                                currentLayer = null;
207
                        } else {
208
                                currentLayer = (WMSLayer) layerStack.pop();
209
                                //System.out.println("Eltos -:"+layerStack.size());
210
                                if (layerStack.empty())
211
                                        layerStack = new Stack();
212
                        }
213
                } else if (token.startsWith(CapabilitiesTags.STYLE)) {
214
                        where |= INSTYLE;
215
                        currentStyle = new WMSStyle();
216
                        System.out.println("  Style");
217
                } else if ((where & INSTYLE) == INSTYLE) {
218
                        parsingStyle(l,token);
219
                } else if (token.startsWith(CapabilitiesTags.DIMENSION)) {
220
                        TreeMap pairs = parser.getPairs(l);
221
                        WMSDimension t = new WMSDimension();
222
                        t.setName(CapabilitiesTags.DIMENSION_NAME);
223
                        t.setUnits((String) pairs.get(CapabilitiesTags.DIMENSION_UNITS));
224
                        t.setDefault((String) pairs.get(CapabilitiesTags.DIMENSION_DEFAULT));
225
                        t.setValue(parser.getValue(l));
226
                        currentLayer.setProperty(t.getName(), t);
227
                        System.out.println("  "+t.toXML());
228
                } else if (token.startsWith(CapabilitiesTags.NAME)) {
229
                        currentLayer.setName(parser.getValue(l));
230
                        System.out.println("  Name='"+currentLayer.getName()+"'");
231
                } else if (token.startsWith(CapabilitiesTags.TITLE)) {
232
                        currentLayer.setTitle(parser.getValue(l));
233
                        System.out.println("  Title='"+currentLayer.getTitle()+"'");
234
                } else if (token.startsWith(CapabilitiesTags.BOUNDINGBOX)) {
235
                        TreeMap pairs = parser.getPairs(l);
236
                        Rectangle2D bBox = parser.getBoundingBox(pairs);
237
                        String srs = null;
238
                        if (pairs.containsKey(CapabilitiesTags.CRS))
239
                                srs = (String) pairs.get(CapabilitiesTags.CRS);
240
                        else if (pairs.containsKey(CapabilitiesTags.SRS))
241
                                srs = (String) pairs.get(CapabilitiesTags.SRS);
242
                        
243
                        if (srs != null) {
244
                                currentLayer.setLatLonBoundingBox(bBox);//.addBoundingBox(srs, bBox);
245
                                System.out.println("  Bbox("+srs+")='"+currentLayer.getLatLonBoundingBox()+"'");
246
                        }
247
                } else if (token.startsWith(CapabilitiesTags.LATLONBOUNDINGBOX)) {
248
                        currentLayer.setLatLonBoundingBox(
249
                                parser.getBoundingBox(l));
250
                        System.out.println("  LLBbox='"+currentLayer.getLatLonBoundingBox()+"'");
251
                } else if (token.startsWith("EX_GeographicBoundingBox")) {
252
                        where |= INEX_GeographicBoundingBox;
253
                } else if ((where & INEX_GeographicBoundingBox) == INEX_GeographicBoundingBox) {
254
                        if (token.startsWith("/EX_GeographicBoundingBox")) {
255
                                where &= INEX_GeographicBoundingBox ^ 0xffff;
256
                                currentLayer.setLatLonBoundingBox(
257
                                        new Rectangle2D.Double(minX, minY, maxX-minX,maxY-minY));
258
                                System.out.println("  LLBbox='"+currentLayer.getLatLonBoundingBox()+"'");
259
                        } else if (token.startsWith("westBoundLongitude")) {
260
                                minX = parser.getValueDouble(l);
261
                        } else if (token.startsWith("southBoundLatitude")) {
262
                                minY = parser.getValueDouble(l);
263
                        } else if (token.startsWith("eastBoundLongitude")) {
264
                                maxX = parser.getValueDouble(l);
265
                        } else if (token.startsWith("northBoundLatitude")) {
266
                                maxY = parser.getValueDouble(l);
267
                        }
268
                }
269
        }
270
        
271
        protected void parsingStyle(String l, String token){
272
                if (token.startsWith("/" + CapabilitiesTags.STYLE)) {
273
                        where &= INSTYLE ^ 0xffff;
274
                        currentLayer.addStyle(currentStyle);
275
                        currentStyle = null;
276
                } else if (token.startsWith(CapabilitiesTags.NAME)) {
277
                        currentStyle.setName(parser.getValue(l));
278
                        System.out.println("    Name='"+currentStyle.getName()+"'");
279
                } else if (token.startsWith(CapabilitiesTags.TITLE)) {
280
                        currentStyle.setTitle(parser.getValue(l));
281
                        System.out.println("    Title='"+currentStyle.getTitle()+"'");
282
                        currentLayer.addStyle(currentStyle);
283
                }
284
        }
285
        
286
        //------------------------------------------------------------------        
287
        // -------------- Inner class to mover out!!!!????  ----------------
288
        //------------------------------------------------------------------
289
        public static class WMSDimension {
290
                private String name;
291
                private String units;
292
                private String def;
293
                private String value;
294
                /**
295
                 * @return Returns the def.
296
                 */
297
                public String getDefault() {
298
                        return def;
299
                }
300
                /**
301
                 * @param def The def to set.
302
                 */
303
                public void setDefault(String def) {
304
                        this.def = def;
305
                }
306
                /**
307
                 * @return Returns the units.
308
                 */
309
                public String getUnits() {
310
                        return units;
311
                }
312
                /**
313
                 * @param units The units to set.
314
                 */
315
                public void setUnits(String units) {
316
                        this.units = units;
317
                }
318
                /**
319
                 * @return Returns the value.
320
                 */
321
                public String getValue() {
322
                        return value;
323
                }
324
                /**
325
                 * @param value The value to set.
326
                 */
327
                public void setValue(String value) {
328
                        this.value = value;
329
                }
330
                
331
                /**
332
                 * @return Returns the name.
333
                 */
334
                public String getName() {
335
                        return name;
336
                }
337
                /**
338
                 * @param name The name to set.
339
                 */
340
                public void setName(String name) {
341
                        this.name = name;
342
                }
343

    
344
                public String toXML() {
345
                        return "<Dimension name=\"" + name +
346
                                "\" units=\""+units+
347
                                "\" default=\""+def+
348
                                "\">"+value+
349
                                "</Dimension>";
350
                }
351
        }                
352

    
353
}