Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / layers / FLyrWFS.java @ 14492

History | View | Annotate | Download (23.9 KB)

1
package com.iver.cit.gvsig.fmap.layers;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.IOException;
5
import java.net.ConnectException;
6
import java.net.MalformedURLException;
7
import java.net.URL;
8
import java.util.HashMap;
9
import java.util.Hashtable;
10
import java.util.Map;
11
import java.util.Vector;
12

    
13
import javax.swing.ImageIcon;
14

    
15
import org.gvsig.remoteClient.gml.schemas.XMLElement;
16
import org.gvsig.remoteClient.gml.types.IXMLType;
17
import org.gvsig.remoteClient.gml.types.XMLComplexType;
18
import org.gvsig.remoteClient.wfs.WFSStatus;
19
import org.gvsig.remoteClient.wfs.exceptions.WFSException;
20
import org.gvsig.remoteClient.wfs.filters.AFilter;
21
import org.gvsig.remoteClient.wfs.filters.FilterEncoding;
22

    
23
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
24
import com.hardcode.gdbms.engine.data.driver.DriverException;
25
import com.iver.andami.PluginServices;
26
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
27
import com.iver.cit.gvsig.exceptions.layers.URLLayerException;
28
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
29
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
30
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
31
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriver;
32
import com.iver.cit.gvsig.fmap.drivers.wfs.FMapWFSDriverFactory;
33
import com.iver.cit.gvsig.fmap.drivers.wfs.filters.SQLExpressionFormat;
34
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
35
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
36
import com.iver.utiles.StringUtilities;
37
import com.iver.utiles.XMLEntity;
38

    
39
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
40
 *
41
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
42
 *
43
 * This program is free software; you can redistribute it and/or
44
 * modify it under the terms of the GNU General Public License
45
 * as published by the Free Software Foundation; either version 2
46
 * of the License, or (at your option) any later version.
47
 *
48
 * This program is distributed in the hope that it will be useful,
49
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51
 * GNU General Public License for more details.
52
 *
53
 * You should have received a copy of the GNU General Public License
54
 * along with this program; if not, write to the Free Software
55
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
56
 *
57
 * For more information, contact:
58
 *
59
 *  Generalitat Valenciana
60
 *   Conselleria d'Infraestructures i Transport
61
 *   Av. Blasco Ib??ez, 50
62
 *   46010 VALENCIA
63
 *   SPAIN
64
 *
65
 *      +34 963862235
66
 *   gvsig@gva.es
67
 *      www.gvsig.gva.es
68
 *
69
 *    or
70
 *
71
 *   IVER T.I. S.A
72
 *   Salamanca 50
73
 *   46005 Valencia
74
 *   Spain
75
 *
76
 *   +34 963163400
77
 *   dac@iver.es
78
 */
79
/* CVS MESSAGES:
80
 *
81
 * $Id: FLyrWFS.java 14492 2007-10-08 12:41:09Z jmvivo $
82
 * $Log$
83
 * Revision 1.37  2007-09-19 16:14:50  jaume
84
 * removed unnecessary imports
85
 *
86
 * Revision 1.36  2007/03/27 09:51:40  jaume
87
 * patched for v1.1 labeling
88
 *
89
 * Revision 1.35  2007/03/06 16:53:57  caballero
90
 * Exceptions
91
 *
92
 * Revision 1.34  2007/03/05 13:46:28  ppiqueras
93
 * Mejorado el filtrado por ?rea.
94
 *
95
 * Revision 1.33  2007/03/01 13:10:59  ppiqueras
96
 * Corregido para que diferencie entre 3 tipos de filtrado: filtrado por sentencia, filtrado por bounding box y filtrado por ambos.
97
 *
98
 * Revision 1.32  2007/02/20 13:12:29  ppiqueras
99
 * Que no aplique filtro ni bounding box si son null
100
 *
101
 * Revision 1.31  2007/02/19 11:44:42  jorpiell
102
 * A?adidos los filtros por ?rea
103
 *
104
 * Revision 1.30  2007/02/09 14:12:38  jorpiell
105
 * Soporte para WFS 1.1 y WFS-T
106
 *
107
 * Revision 1.29  2007/01/12 13:09:41  jorpiell
108
 * added searches by area
109
 *
110
 * Revision 1.28  2007/01/10 08:23:02  jorpiell
111
 * If the parent element is selected is not necessary to select its children
112
 *
113
 * Revision 1.27  2006/12/26 10:25:37  ppiqueras
114
 * Corregidas las dependencias con las nuevas ubicaciones de clases: IXMLType, XMLElement, IXMLComplexType, etc. (en libRemoteServices)
115
 *
116
 * Revision 1.26  2006/12/26 09:28:01  ppiqueras
117
 * Cambiado "atttibutes" en todas las aparaciones en atributos, m?todos, clases, paquetes o comentarios por "fields". (S?lo a aquellas que afectan a clases dentro del proyecto extWFS2).
118
 *
119
 * Revision 1.24  2006/12/15 13:55:17  ppiqueras
120
 * Almacena todos los campos y valores conocidos de la capa.
121
 *
122
 * Revision 1.23  2006/12/11 11:02:24  ppiqueras
123
 * Corregido bug -> que se mantenga la frase de filtrado
124
 *
125
 * Revision 1.22  2006/11/28 08:04:51  jorpiell
126
 * Se guarda la query para que pueda ser recuperada desde la ventana de propiedades
127
 *
128
 * Revision 1.21  2006/11/16 17:17:48  jorpiell
129
 * Se guarda el filtro en el gvp
130
 *
131
 * Revision 1.20  2006/11/16 13:29:47  jorpiell
132
 * Se ha reescrito losl m?todo para  guardar y recuperar gvps
133
 *
134
 * Revision 1.19  2006/11/15 17:38:08  jorpiell
135
 * Ya se puede guardar una capa en WFS.
136
 *
137
 * Revision 1.18  2006/10/31 12:24:04  jorpiell
138
 * Comprobado el caso en el que los atributos no tienen tipo
139
 *
140
 * Revision 1.17  2006/10/31 09:55:28  jorpiell
141
 * Se ha modificado el constructor del WFS desde cat?logo
142
 *
143
 * Revision 1.16  2006/10/31 09:38:15  jorpiell
144
 * Se ha creado una factoria para crear la capa. De ese modo no se repite c?digo desde le panel de propiedades y desde el panel de la capa
145
 *
146
 * Revision 1.15  2006/10/23 07:37:04  jorpiell
147
 * Ya funciona el filterEncoding
148
 *
149
 * Revision 1.14  2006/10/10 12:55:06  jorpiell
150
 * Se ha a?adido el soporte de features complejas
151
 *
152
 * Revision 1.13  2006/10/02 12:54:35  jorpiell
153
 * No se pod?a crear un mapa que tubiera la opci?n enlace vivo habilitada
154
 *
155
 * Revision 1.12  2006/10/02 09:09:45  jorpiell
156
 * Cambios del 10 copiados al head
157
 *
158
 * Revision 1.10.2.3  2006/09/29 14:12:53  luisw2
159
 * CRSFactory.getCRS substitutes ProjectionPool.get
160
 *
161
 * Revision 1.10.2.2  2006/09/28 08:54:01  jorpiell
162
 * Ya se puede reproyectar
163
 *
164
 * Revision 1.10.2.1  2006/09/26 07:36:24  jorpiell
165
 * El WFS no reproyectaba porque no se le asignaba a la capa un sistema de referencia. Ahora ya se hace.
166
 *
167
 * Revision 1.10  2006/09/05 15:41:52  jorpiell
168
 * A?adida la funcionalidad de cargar WFS desde el cat?logo
169
 *
170
 * Revision 1.9  2006/07/05 12:05:41  jorpiell
171
 * Se ha modificado para que avise si se han recuperado las mismas features que marca el campo buffer
172
 *
173
 * Revision 1.8  2006/06/21 12:52:10  jorpiell
174
 * A?adido un icono para la capa WFS
175
 *
176
 * Revision 1.7  2006/06/21 12:35:45  jorpiell
177
 * Se ha a?adido la ventana de propiedades. Esto implica a?adir listeners por todos los paneles. Adem?s no se muestra la geomatr?a en la lista de atributos y se muestran ?nicamnete los que se van a descargar
178
 *
179
 * Revision 1.6  2006/06/15 11:17:06  jorpiell
180
 * Se ha encontrado la forma de comprobar cuando se prodicia un error de parseo al hacer un hasnext (en la feature). Se atrapa y se lanza la excepci?n hacia arriba
181
 *
182
 * Revision 1.5  2006/06/14 07:57:19  jorpiell
183
 * Ya no se usa la estrategia ni se usa geotools para hacer el getFeature. Ahora se usa ?nicamente para el parseo de GML
184
 *
185
 * Revision 1.4  2006/06/05 16:49:31  caballero
186
 * poder editar y exportar
187
 *
188
 * Revision 1.3  2006/05/25 10:31:55  jorpiell
189
 * Se ha renombrado la clase WFSFields por WFSAttributes porque era algo confusa
190
 *
191
 * Revision 1.2  2006/05/23 13:21:59  jorpiell
192
 * Si hay alg?n problema en la carga se muestra un mensaje de error
193
 *
194
 * Revision 1.1  2006/05/19 12:54:11  jorpiell
195
 * Creada la capa WFS
196
 *
197
 *
198
 */
199
/**
200
 *  FMap's WFS Layer class.
201
 *
202
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
203
 */
204
public class FLyrWFS extends FLyrVect{
205
        private URL host = null;
206
        private String onlineResource = null;
207
        private String name = null;
208
        private String layerName = null;
209
        private String nameSapce = null;
210
    private String userName = null;
211
    private String password = null;
212
    private String FieldsQuery = null;
213
    private Rectangle2D bbox = null;
214
    private String bboxPropertyName = null;
215
    private String visualFilterQuery = null;
216
    private Rectangle2D extent = null;
217
    private XMLElement[] fields = null;
218
    private int numfeatures = 100;
219
    private int timeout = 10000;
220
    private String srs = null;
221
    private FMapWFSDriver wfsDriver = null;
222
    private WFSLayerNode wfsLayerNode = null;
223
    private Map allFieldsAndValuesKnown = null;
224

    
225
    /**
226
     * Constructor
227
     */
228
    public FLyrWFS(){
229
            super();
230
    }
231

    
232
    /**
233
     * Constructor to load a new layer from a catalog
234
     * search
235
     * @param args
236
     * Params to load a new layer
237
     * @throws MalformedURLException
238
     * @throws LoadLayerException
239
     * @throws WFSException
240
     * @throws IOException
241
     * @throws MalformedURLException
242
     * @throws ConnectException
243
     * @throws DriverIOException
244
     */
245
    public FLyrWFS(Map args) throws LoadLayerException{
246
            String[] sLayer = (String[])args.get("layer");
247
            String[] sNamespace = (String[])args.get("namespace");
248
                String user = (String)args.get("user");
249
                String pwd = (String)args.get("pwd");
250
                String host = (String)args.get("host");
251
                String projection = (String)args.get("projection");
252

    
253
                FMapWFSDriver driver;
254
                try {
255
                        driver = FMapWFSDriverFactory.getFMapDriverForURL(new URL(host));
256
                } catch (MalformedURLException e) {
257
                        throw new URLLayerException(getName(),e);
258
                }
259
                WFSLayerNode[] layers = driver.getLayerList();
260
                WFSLayerNode layer = driver.getLayerInfo(sLayer[0],sNamespace[0]);
261

    
262
                layer.setSelectedFields(layer.getFields());
263

    
264
                Vector vAtts = new Vector();
265
                if (layer.getFields().size() == 1){
266
                        XMLElement element = (XMLElement)layer.getFields().get(0);
267
                        if (element.getEntityType().getType() != IXMLType.COMPLEX){
268
                                vAtts.add(element);
269
                        }else{
270
                                vAtts = ((XMLComplexType)element.getEntityType()).getAttributes();
271
                        }
272
                }
273
                for (int i=0 ; i<layer.getFields().size() ; i++){
274
                        XMLElement element = (XMLElement)layer.getFields().get(i);
275
                        if (element.getEntityType().getType() != IXMLType.COMPLEX){
276
                                vAtts.add((XMLElement)layer.getFields().get(i));
277
                        }else{
278

    
279
                        }
280
                }
281

    
282
                XMLElement[] atts = new XMLElement[vAtts.size()];
283
                for (int i=0 ; i<vAtts.size() ; i++){
284
                        atts[i] = (XMLElement)vAtts.get(i);
285
                }
286

    
287
                setHost(host);
288
                setName(sLayer[0]);
289
                setLayerName(sLayer[0]);
290
                setWfsLayerNode(layer);
291
                setFields(atts);
292
                setUserName(user);
293
                setPassword(pwd);
294
                setNumfeatures(10000);
295
                setTimeout(10000);
296
                setWfsDriver(driver);
297
                setProjection(CRSFactory.getCRS(projection));
298

    
299
            load();
300
    }
301

    
302

    
303
    /**
304
     * Loads the features from the server
305
     */
306
    public void load() throws LoadLayerException{
307
            WFSAdapter adapter = new WFSAdapter();
308
            wfsDriver.getFeature(getWFSStatus());
309

    
310
                   adapter.setDriver((VectorialDriver) wfsDriver);
311
                   setSource(adapter);
312
                   if (getLegend() == null){
313
                         try {
314
                                setLegend(LegendFactory.createSingleSymbolLegend(
315
                                        getShapeType()));
316
                        } catch (ReadDriverException e) {
317
                                throw new LoadLayerException(getName(),e);
318
                        }
319
                   }
320
    }
321

    
322
    /**
323
     * Gets the WFS Status
324
     * @return
325
     */
326
    private WFSStatus getWFSStatus(){
327
            WFSStatus status = new WFSStatus(getLayerName(),getNameSapce());
328
            status.setUserName(getUserName());
329
            status.setPassword(getPassword());
330
            status.setBuffer(getNumfeatures());
331
            status.setTimeout(getTimeout());
332
            status.setFields(getFieldNames());
333
            status.setOnlineResource(getOnlineResource());
334
            status.setSrs(getSrs());
335
            status.setBBox(getExtent());
336
            // Filter Encoding transformation
337
            if (getBbox() == null) {
338
                    if (getFieldsQuery() != null) {
339
                        FilterEncoding fe = SQLExpressionFormat.createFilter();
340

    
341
                        fe.setQuery(getFieldsQuery());
342
                        status.setFilterQuery(fe.toString());
343
                    }
344
            }
345
            else {
346
                           FilterEncoding fe = SQLExpressionFormat.createFilter();
347
                           Rectangle2D r2D = getBbox();
348

    
349
                           status.setBBox(r2D);
350

    
351
                           if ((getFieldsQuery() != null) && (getFieldsQuery().compareTo("") != 0)) {
352
                               fe.setQuery(getFieldsQuery());
353
                       }
354

    
355
                           fe.setBBox(r2D, getBboxPropertyName(), getSrs(), AFilter.BBOX_ENCLOSES);
356
                           status.setFilterQuery(fe.toString());
357
                }
358
                status.setFilterVisualText(getVisualFilterQuery());
359

    
360
            return status;
361
    }
362

    
363
        /**
364
         * @return Returns the layerName.
365
         */
366
        public String getLayerName() {
367
                return layerName;
368
        }
369
        /**
370
         * @param layerName The layerName to set.
371
         */
372
        public void setLayerName(String layerName) {
373
                this.layerName = layerName;
374
        }
375
        /**
376
         * @return Returns the numfeatures.
377
         */
378
        public int getNumfeatures() {
379
                return numfeatures;
380
        }
381
        /**
382
         * @param numfeatures The numfeatures to set.
383
         */
384
        public void setNumfeatures(int numfeatures) {
385
                this.numfeatures = numfeatures;
386
        }
387

    
388
        /**
389
         * @return Returns the pwd.
390
         */
391
        public String getPassword() {
392
                return password;
393
        }
394
        /**
395
         * @param pwd The pwd to set.
396
         */
397
        public void setPassword(String password) {
398
                this.password = password;
399
        }
400
        /**
401
         * @return Returns the timeout.
402
         */
403
        public int getTimeout() {
404
                return timeout;
405
        }
406
        /**
407
         * @param timeout The timeout to set.
408
         */
409
        public void setTimeout(int timeout) {
410
                this.timeout = timeout;
411
        }
412
        /**
413
         * @return Returns the user.
414
         */
415
        public String getUserName() {
416
                return userName;
417
        }
418
        /**
419
         * @param user The user to set.
420
         */
421
        public void setUserName(String userName) {
422
                this.userName = userName;
423
        }
424
        /**
425
         * @return The visual filtering text from "Where"
426
         */
427
        public String getVisualFilterQuery() {
428
                return visualFilterQuery;
429
        }
430
        /**
431
         * @param visualFilterQuery The visual filtering text from "Where"
432
         */
433
        public void setVisualFilterQuery(String visualFilterQuery) {
434
                this.visualFilterQuery = visualFilterQuery;
435
        }
436
        /**
437
         * @return Returns the fields.
438
         */
439
        public XMLElement[] getFields() {
440
                if (fields == null){
441
                        return new XMLElement[0];
442
                }
443
                return fields;
444
        }
445

    
446
        /**
447
         * Return the fields name
448
         * @return
449
         */
450
        public String[] getFieldNames(){
451
                Vector vFields = new Vector();
452
                Hashtable hashFields = new Hashtable();
453
                for (int i=0 ; i<getFields().length ; i++){
454
                        //if is not the root element
455
                        if (getFields()[i].getParentElement() != null){
456
                                //If the parent node has been not added
457
                                if (hashFields.get(getFields()[i].getParentElement()) == null){
458
                                        hashFields.put(getFields()[i],"");
459
                                        vFields.add(getFields()[i].getFullName());
460
                                }
461
                        }
462
                }
463
                String[] fields = new String[vFields.size()];
464
                for (int i=0 ; i<vFields.size() ; i++){
465
                        fields[i] = (String)vFields.get(i);
466
                }
467
                return fields;
468
        }
469

    
470
        /**
471
         * @param fields The fields to set.
472
         */
473
        public void setFields(XMLElement[] fields) {
474
                this.fields = fields;
475
        }
476
        /**
477
         * @return Returns the name.
478
         */
479
        public String getName() {
480
                return name;
481
        }
482
        /**
483
         * @param name The name to set.
484
         */
485
        public void setName(String name) {
486
                this.name = name;
487
        }
488
        /**
489
         * @return Returns the host.
490
         */
491
        public URL getHost() {
492
                return host;
493
        }
494
        /**
495
         * @param host The host to set.
496
         */
497
        public void setHost(URL host) {
498
                this.host = host;
499
        }
500

    
501
        /**
502
         * @return Returns the wfs.
503
         */
504
        public FMapWFSDriver getWfsDriver() {
505
                return wfsDriver;
506
        }
507

    
508
        /**
509
         * @param wfs The wfs to set.
510
         */
511
        public void setWfsDriver(FMapWFSDriver wfs) {
512
                this.wfsDriver = wfs;
513
        }
514

    
515

    
516
        /*
517
         *  (non-Javadoc)
518
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getStrategy()
519
         */
520
//    public Strategy getStrategy() {
521
//        if (wfsStrategy == null){
522
//                wfsStrategy = new WFSStrategy(this);
523
//        }
524
//            return wfsStrategy;
525
//    }
526

    
527
    /*
528
     *  (non-Javadoc)
529
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
530
     */
531
        public Rectangle2D getFullExtent() throws ReadDriverException {
532
                   Rectangle2D extent = wfsDriver.getFullExtent();
533
                if (extent == null){
534
                        return null;
535
                }
536
                   if (getCoordTrans() != null){
537
                           return getCoordTrans().convert(extent);
538
                   }
539
                return extent;
540
        }
541

    
542
        /**
543
         * @return Returns the onlineResource.
544
         */
545
        public String getOnlineResource() {
546
                return onlineResource;
547
        }
548

    
549
        /**
550
         * @param onlineResource The onlineResource to set.
551
         */
552
        public void setOnlineResource(String onlineResource) {
553
                this.onlineResource = onlineResource;
554
        }
555

    
556
        public HashMap getProperties() {
557
                HashMap info = new HashMap();
558
                info.put("name", getName());
559
                info.put("layerName", getLayerName());
560
                // layer text is no longer available
561
                //info.put("layerText", getLayerText());
562
                info.put("attributes", getFields());
563
                info.put("host", getHost());
564
                info.put("wfsLayerNode", getWfsLayerNode());
565
                WFSStatus status = new WFSStatus(getLayerName(),getNameSapce());
566
                status.setBuffer(getNumfeatures());
567
                status.setTimeout(getTimeout());
568
                status.setUserName(getUserName());
569
                status.setPassword(getPassword());
570
                status.setFilterQuery(getFieldsQuery());
571
                status.setFilterVisualText(getVisualFilterQuery());
572
                info.put("status",status);
573
                return info;
574

    
575
        }
576

    
577
        /**
578
         * @return Returns the wfsLayerNode.
579
         */
580
        public WFSLayerNode getWfsLayerNode() {
581
                return wfsLayerNode;
582
        }
583

    
584
        /**
585
         * @param wfsLayerNode The wfsLayerNode to set.
586
         */
587
        public void setWfsLayerNode(WFSLayerNode wfsLayerNode) {
588
                this.wfsLayerNode = wfsLayerNode;
589
        }
590

    
591
        public void setHost(String host2) {
592
                try {
593
                        setHost(new URL(host2));
594
                } catch (MalformedURLException e) {
595
                        // TODO Auto-generated catch block
596
                        e.printStackTrace();
597
                }
598
        }
599

    
600
        public ImageIcon getTocImageIcon() {
601
                return new ImageIcon(PluginServices.getPluginServices("com.iver.cit.gvsig.wfs2").getClassLoader().getResource("images/icoLayer.png"));
602
        }
603

    
604
        /*
605
         *  (non-Javadoc)
606
         * @see com.iver.cit.gvsig.fmap.layers.FLyrVect#isPropertiesMenuVisible()
607
         */
608
        public boolean isPropertiesMenuVisible(){
609
                return false;
610
        }
611

    
612
        public String getSrs() {
613
                return srs;
614
        }
615

    
616
        public void setSrs(String srs) {
617
                this.srs = srs;
618
        }
619

    
620
        /**
621
         * @return XMLEntity.
622
         * @throws XMLException
623
         */
624
        public XMLEntity getXMLEntity() throws XMLException {
625
                XMLEntity xml = super.getXMLEntity();
626

    
627
                // Full extent
628
                try {
629
                        xml.putProperty("fullExtent", StringUtilities.rect2String(getFullExtent()));
630
                } catch (ReadDriverException e) {
631
                        throw new XMLException(e);
632
                }
633

    
634
                // Host
635
                xml.putProperty("host", host.toExternalForm());
636

    
637
                // Layer name
638
                xml.putProperty("layerName", getLayerName());
639
                xml.putProperty("name", getName());
640

    
641
                //Layer fields
642
                XMLElement[] fields = getFields();
643
                String strFields = "";
644
                for (int i=0 ; i<fields.length ; i++){
645
                        //If is not the root node
646
                        if (fields[i].getParentElement() != null){
647
                                strFields = strFields + fields[i].getFullName();
648
                                if (i < fields.length - 1){
649
                                        strFields = strFields + "~##SEP1##~";
650
                                }
651
                        }else{
652
                                xml.putProperty("rootNode", true);
653
                        }
654
                }
655
                xml.putProperty("fields", strFields);
656

    
657
                // User name
658
                xml.putProperty("user", getUserName());
659

    
660
                // SRS
661
                xml.putProperty("srs", getSrs());
662

    
663
        // OnlineResources
664
        xml.putProperty("onlineResource", getOnlineResource());
665

    
666
            // TimeOut
667
                xml.putProperty("timeout", getTimeout());
668

    
669
        // Buffer
670
        xml.putProperty("buffer", getNumfeatures());
671

    
672
        //Projection
673
        xml.putProperty("projection",getProjection().getAbrev());
674

    
675
        //Filter
676
        xml.putProperty("filterEncoding",getFieldsQuery());
677

    
678
        return xml;
679
        }
680

    
681
        /**
682
         * @param xml XMLEntity
683
         *
684
         * @throws XMLException
685
         * @throws DriverException
686
         * @throws DriverI OException
687
         */
688
        public void setXMLEntity(XMLEntity xml)throws XMLException {
689

    
690
                // Host
691
                try {
692
                        host = new URL(xml.getStringProperty("host"));
693
                } catch (MalformedURLException e) {
694
                        throw new XMLException(e);
695
                }
696

    
697
                // Layer name
698
                setLayerName(xml.getStringProperty("layerName"));
699
                setName(xml.getStringProperty("name"));
700

    
701
                // User name
702
                if (xml.contains("user")){
703
                        setUserName(xml.getStringProperty("user"));
704
                }
705

    
706
                // SRS
707
                if (xml.contains("srs")){
708
                        setSrs(xml.getStringProperty("srs"));
709
                }
710

    
711
                // OnlineResources
712
                if (xml.contains("onlineResource")){
713
                        setOnlineResource(xml.getStringProperty("onlineResource"));
714
                }
715

    
716
                // TimeOut
717
                if (xml.contains("timeout")){
718
                        setTimeout(xml.getIntProperty("timeout"));
719
                }
720

    
721
                // Buffer
722
                if (xml.contains("buffer")){
723
                        setNumfeatures(xml.getIntProperty("buffer"));
724
                }
725

    
726
                 //Projection
727
                if (xml.contains("projection")){
728
                        setProjection(CRSFactory.getCRS(xml.getStringProperty("projection")));
729
                }
730

    
731
                 //Filter
732
                if (xml.contains("filterEncoding")){
733
                        setFieldsQuery(xml.getStringProperty("filterEncoding"));
734
                }
735

    
736
                // Layer fields
737
                FMapWFSDriver driver;
738
                try {
739
                        driver = FMapWFSDriverFactory.getFMapDriverForURL(host);
740
                        setWfsDriver(driver);
741
                        WFSAdapter adapter = new WFSAdapter();
742
                    adapter.setDriver((VectorialDriver) wfsDriver);
743
                    setSource(adapter);
744
                    super.setXMLEntity(xml);
745

    
746
                    XMLEntity legendXML = xml.getChild(0);
747
                    setLegend(LegendFactory.createFromXML(legendXML));
748

    
749
                    /* (jaume) begin patch;
750
                     * for backward compatibility purposes. Since gvSIG v1.1 labeling is
751
                     * no longer managed by the Legend but by the ILabelingStrategy. The
752
                     * following allows restoring older projects labelings.
753
                     */
754
                        if (legendXML.contains("labelFieldName")) {
755
                            AttrInTableLabeling labeling = new AttrInTableLabeling();
756
                            labeling.setLayer(this);
757
                            labeling.setTextField(legendXML.getStringProperty("labelFieldName"));
758
                            labeling.setHeightField(legendXML.getStringProperty("labelHeightFieldName"));
759
                            labeling.setRotationField(legendXML.getStringProperty("labelRotationFieldName"));
760
                            this.setLabelingStrategy(labeling);
761
                            this.setIsLabeled(true);
762
                      }
763
                     /* end patch */
764
                } catch (Exception e) {
765
                        throw new XMLException(e);
766
                }
767
                driver.getLayerList();
768
                WFSLayerNode layer = driver.getLayerInfo(getLayerName(),getNameSapce());
769
                XMLElement[] atts = null;
770

    
771
                // The root element always exists
772
                XMLElement rootElement = (XMLElement)layer.getFields().get(0);
773

    
774
                if (xml.contains("fields")) {
775
                        String[] fields = xml.getStringProperty("fields").split("~##SEP1##~");
776
                        if ((fields.length == 1) && (fields[0].equals(""))){
777
                                fields = new String[0];
778
                        }
779
                        //The root element always is a complex type
780
                        Vector allFields = ((XMLComplexType)rootElement.getEntityType()).getAttributes();
781
                        //If the root node has been selected
782
                        if (xml.contains("rootNode")){
783
                                if (xml.getBooleanProperty("rootNode")==true){
784
                                        atts = new XMLElement[fields.length + 1];
785
                                        atts[fields.length] = rootElement;
786
                                }else{
787
                                        atts = new XMLElement[fields.length];
788
                                }
789
                        }else{
790
                                atts = new XMLElement[fields.length];
791
                        }
792
                        //Adding the other fields
793
                        for (int i=0 ; i<fields.length ; i++){
794
                                for (int j=0 ; j<allFields.size() ; j++){
795
                                        XMLElement field = (XMLElement)allFields.get(j);
796
                                        if (field != null){
797
                                                XMLElement found = field.searchAttribute(fields[i]);
798
                                                if (found != null){
799
                                                        atts[i] = found;
800
                                                        break;
801
                                                }
802
                                        }
803
                                }
804
                        }
805
                }else{
806
                        if (xml.contains("rootNode")){
807
                                if (xml.getBooleanProperty("rootNode")==true){
808
                                        atts = new XMLElement[1];
809
                                        atts[0] = rootElement;
810
                                }
811
                        }
812
                }
813
                layer.setSelectedFields(atts);
814
                setWfsLayerNode(layer);
815
                setFields(atts);
816
                setAvailable(true);
817
        }
818

    
819
        /**
820
         * @return Returns the fieldsQuery.
821
         */
822
        public String getFieldsQuery() {
823
                return FieldsQuery;
824
        }
825

    
826
        /**
827
         * @param fieldsQuery The fieldsQuery to set.
828
         */
829
        public void setFieldsQuery(String fieldsQuery) {
830
                FieldsQuery = fieldsQuery;
831
        }
832

    
833
        /**
834
         * Sets all fields and values known about this layer
835
         *
836
         * @param _allFieldsAndValuesKnown A Map
837
         */
838
        public void setAllFieldsAndValuesKnown(Map _allFieldsAndValuesKnown) {
839
                if (this.allFieldsAndValuesKnown == null)
840
                        allFieldsAndValuesKnown = new HashMap();
841

    
842
                allFieldsAndValuesKnown = _allFieldsAndValuesKnown;
843
        }
844

    
845
        /**
846
         * Gets all fields and values known about this layer
847
         *
848
         * @return A Map
849
         */
850
        public Map getAllFieldsAndValuesKnown() {
851
                return allFieldsAndValuesKnown;
852
        }
853

    
854
        /**
855
         * @return Returns the extent.
856
         */
857
        public Rectangle2D getExtent() {
858
                return extent;
859
        }
860

    
861
        /**
862
         * @param extent The extent to set.
863
         */
864
        public void setExtent(Rectangle2D extent) {
865
                this.extent = extent;
866
        }
867

    
868
        /**
869
         * @return Returns the nameSapce.
870
         */
871
        public String getNameSapce() {
872
                return nameSapce;
873
        }
874

    
875
        /**
876
         * @param nameSapce The nameSapce to set.
877
         */
878
        public void setNameSapce(String nameSapce) {
879
                this.nameSapce = nameSapce;
880
        }
881

    
882
        /**
883
         * @return Returns the bbox.
884
         */
885
        public Rectangle2D getBbox() {
886
                return bbox;
887
        }
888

    
889
        /**
890
         * @param bbox The bounding box to set.
891
         */
892
        public void setBbox(Rectangle2D bbox) {
893
                this.bbox = bbox;
894
        }
895

    
896
        public String getBboxPropertyName() {
897
                return bboxPropertyName;
898
        }
899

    
900
        public void setBboxPropertyName(String bboxPropertyName) {
901
                this.bboxPropertyName = bboxPropertyName;
902
        }
903
}