Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src / org / gvsig / geocoding / pattern / impl / DefaultGeocodingPattern.java @ 32474

History | View | Annotate | Download (6.03 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
 * 2008 Prodevelop S.L. main development
26
 */
27

    
28
package org.gvsig.geocoding.pattern.impl;
29

    
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.InputStreamReader;
37
import java.io.OutputStream;
38
import java.io.OutputStreamWriter;
39
import java.io.UnsupportedEncodingException;
40

    
41
import org.exolab.castor.xml.MarshalException;
42
import org.exolab.castor.xml.Marshaller;
43
import org.exolab.castor.xml.ValidationException;
44
import org.gvsig.geocoding.GeocodingTags;
45
import org.gvsig.geocoding.pattern.GeocodingPattern;
46
import org.gvsig.geocoding.pattern.GeocodingSettings;
47
import org.gvsig.geocoding.pattern.GeocodingSource;
48
import org.gvsig.utils.XMLEntity;
49
import org.gvsig.utils.XMLException;
50
import org.gvsig.utils.xml.XMLEncodingUtils;
51
import org.gvsig.utils.xmlEntity.generate.XmlTag;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * Patterngeocoding class implementation.
57
 * 
58
 * This class is the geocoding pattern. This pattern has your name (name), the
59
 * settings (settings) and the data source (source)
60
 * 
61
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
62
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
63
 */
64
public class DefaultGeocodingPattern implements GeocodingPattern {
65

    
66
        @SuppressWarnings("unused")
67
        private static final Logger log = LoggerFactory
68
                        .getLogger(DefaultGeocodingPattern.class);        
69

    
70
        private String name;
71
        private GeocodingSettings settings;
72
        private GeocodingSource source;
73

    
74
        /**
75
         * Default constructor
76
         */
77
        public DefaultGeocodingPattern() {
78
                this.setSource(new DefaultGeocodingSource());
79
                this.setSettings(new DefaultGeocodingSettings());
80
        }
81

    
82
        /**
83
         * Constructor with parameters
84
         */
85
        public DefaultGeocodingPattern(GeocodingSource source,
86
                        GeocodingSettings settings) {
87
                this.setSource(source);
88
                this.setSettings(settings);
89
        }
90

    
91
        /**
92
         * Constructor with pattern name
93
         * 
94
         * @param name
95
         */
96
        public DefaultGeocodingPattern(String name) {
97
                this();
98
                this.setPatternName(name);
99
        }
100

    
101
        /**
102
         * Get pattern name
103
         * @return name
104
         */
105
        public String getPatternName() {
106
                return this.name;
107
        }
108

    
109
        /**
110
         * Get pattern settings
111
         * @return settings
112
         */
113
        public GeocodingSettings getSettings() {
114
                return this.settings;
115
        }
116

    
117
        /**
118
         * Get data source
119
         * @return source.
120
         */
121
        public GeocodingSource getDataSource() {
122
                return this.source;
123
        }
124

    
125
        /**
126
         * Set pattern settings
127
         * @param settings
128
         */
129
        public void setSettings(GeocodingSettings settings) {
130
                this.settings = settings;
131
        }
132

    
133
        /**
134
         * Set pattern name
135
         * @param name
136
         * 
137
         */
138
        public void setPatternName(String name) {
139
                this.name = name;
140
        }
141

    
142
        /**
143
         * Set data source
144
         * @param source
145
         */
146
        public void setSource(GeocodingSource source) {
147
                this.source = source;
148
        }
149

    
150
        /**
151
         * Save the pattern to XML file
152
         * 
153
         * @param file
154
         * @throws XMLException
155
         * @throws IOException
156
         * @throws ValidationException
157
         * @throws MarshalException
158
         */
159
        public void saveToXML(File file) throws XMLException, IOException,
160
                        MarshalException, ValidationException {
161
                OutputStream fos = new FileOutputStream(file.getAbsolutePath());
162
                OutputStreamWriter writer = new OutputStreamWriter(fos, GeocodingTags.PROJECTENCODING);
163
                Marshaller m = new Marshaller(writer);
164
                m.setEncoding(GeocodingTags.PROJECTENCODING);
165

    
166
                XMLEntity xml = this.getXMLEntity();
167
                xml.putProperty("followHeaderEncoding", true);
168
                m.marshal(xml.getXmlTag());
169
        }
170

    
171
        /**
172
         * Load the pattern from XML file
173
         * 
174
         * @param reader
175
         * @throws FileNotFoundException
176
         * @throws UnsupportedEncodingException
177
         * @throws ValidationException
178
         * @throws MarshalException
179
         * @throws XMLException
180
         */
181
        public void loadFromXML(File file) throws FileNotFoundException,
182
                        UnsupportedEncodingException, MarshalException,
183
                        ValidationException, XMLException {
184

    
185
                InputStream inStr = new FileInputStream(file);
186
                String encoding = XMLEncodingUtils.getEncoding(inStr);
187
                InputStreamReader reader = new InputStreamReader(inStr, encoding);
188
                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
189
                XMLEntity xml = new XMLEntity(tag);
190
                this.setXMLEntity(xml);
191
        }
192

    
193
        /**
194
         * Save pattern
195
         * 
196
         * @return
197
         * @throws XMLException
198
         */
199
        public XMLEntity getXMLEntity() throws XMLException {
200
                XMLEntity xml = new XMLEntity();
201
                xml.setName(GeocodingTags.GPATTERN);
202
                xml.putProperty(GeocodingTags.PATTERNNAME, this.name);
203
                xml.addChild(this.source.getXMLEntity());
204
                xml.addChild(this.settings.getXMLEntity());
205

    
206
                return xml;
207
        }
208

    
209
        /**
210
         * Load pattern
211
         * 
212
         * @param xml
213
         * @throws XMLException
214
         */
215
        public void setXMLEntity(XMLEntity xml) throws XMLException {
216

    
217
                this.name = xml.getStringProperty(GeocodingTags.PATTERNNAME);
218
                this.source.setXMLEntity(xml.getChild(0));
219
                this.settings.setXMLEntity(xml.getChild(1));
220
        }
221

    
222
        /**
223
         * Get this class name
224
         * 
225
         * @return
226
         */
227
        public String getClassName() {
228
                return this.getClass().getName();
229
        }
230

    
231
        
232

    
233
}