Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / utils / PatternLoader.java @ 27140

History | View | Annotate | Download (4.35 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 Prodevelop S.L  vsanjaime   programador
26
 */
27

    
28
package org.gvsig.geocoding.utils;
29

    
30
import java.io.File;
31

    
32
import javax.swing.JOptionPane;
33

    
34
import org.exolab.castor.xml.MarshalException;
35
import org.exolab.castor.xml.ValidationException;
36
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
37
import org.gvsig.fmap.mapcontext.layers.FLayer;
38
import org.gvsig.fmap.mapcontext.layers.FLayers;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
40
import org.gvsig.geocoding.patterns.Patterngeocoding;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import com.iver.andami.PluginServices;
45
import com.iver.cit.gvsig.project.documents.view.gui.View;
46
import com.iver.utiles.XMLEntity;
47
import com.iver.utiles.XMLException;
48

    
49
/**
50
 * This internal class loads the pattern
51
 * 
52
 * @author Jorge Gaspar Sanz Salinas (jsanz@prodevelop.es)
53
 * @author Vicente Sanjaime Calvet (vsanjaime@prodevelop.es)
54
 * 
55
 */
56

    
57
public class PatternLoader extends Thread {
58

    
59
        private Logger log = LoggerFactory.getLogger(PatternLoader.class);
60
        private File file = null;
61
        private boolean apto = true;
62
        private Patterngeocoding pattern = null;
63

    
64
        public PatternLoader() {
65
                super();
66
        }
67

    
68
        public PatternLoader(File file) {
69
                this();
70
                this.file = file;
71
        }
72

    
73
        public void run() {
74
                pattern = new Patterngeocoding();
75
                if (this.file != null) {
76
                        try {
77
                                pattern.loadFromXML(this.file);
78
                        } catch (Exception e) {
79
                                this.apto = false;
80
                                String mes = PluginServices.getText(this,
81
                                                "geocorrorreadingfile");
82
                                String tit = PluginServices.getText(this, "geocoding");
83
                                JOptionPane.showMessageDialog(null, mes, tit,
84
                                                JOptionPane.ERROR_MESSAGE);
85
                                log.error("Error parsing the XML Pattern", e);
86
                        }
87
                        if (apto) {
88

    
89
                                String message = PluginServices.getText(null, "loadlayer");
90
                                String title = PluginServices.getText(null, "geocoding");
91
                                int i = JOptionPane.showConfirmDialog(null, message, title,
92
                                                JOptionPane.YES_NO_OPTION);
93
                                if (i == 0) {
94
                                        String source = pattern.getSource().getLayerSource();
95

    
96
                                        // Formating the data source
97
                                        if (source.startsWith("<![CDATA[")) {
98
                                                source = source.substring(9);
99
                                        }
100
                                        if (source.endsWith("]]>")) {
101
                                                int size = source.length();
102
                                                source = source.substring(0, size - 4);
103
                                        }
104

    
105
                                        // Getting XMLEntity and create the layer
106
                                        XMLEntity xml = null;
107

    
108
                                        try {
109
                                                xml = XMLEntity.parse(source);
110

    
111
                                        } catch (MarshalException e) {
112
                                                log.error(
113
                                                                "Parsing the data source store in the pattern",
114
                                                                e);
115
                                                apto = false;
116
                                        } catch (ValidationException e) {
117
                                                log.error(
118
                                                                "Parsing the data source store in the pattern",
119
                                                                e);
120
                                                apto = false;
121
                                        }
122

    
123
                                        // add the layer to the view
124
                                        //TODO
125
                                        if (xml != null) {
126
                                                View view = GeocoUtils.getCurrentView();
127
                                                if (view != null) {
128
                                                        FLayers lyrs = view.getMapControl().getMapContext()
129
                                                                        .getLayers();
130
                                                        try {                                                                
131
                                                                lyrs.setXMLEntity(xml);
132
                                                                                                
133
                                                        } catch (XMLException e) {
134
                                                                log.error("Loading the layer");
135
                                                        }
136

    
137
                                                }
138
                                        }
139

    
140
                                }
141

    
142
                        }
143
                }
144
        }
145

    
146
        /**
147
         * @return the apto
148
         */
149
        public boolean isApto() {
150
                return apto;
151
        }
152

    
153
        /**
154
         * @return the file
155
         */
156
        public File getFile() {
157
                return file;
158
        }
159

    
160
        /**
161
         * @return the pattern
162
         */
163
        public Patterngeocoding getPattern() {
164
                return pattern;
165
        }
166
}