svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / export / impl / DefaultExportManager.java @ 43925
History | View | Annotate | Download (5.56 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.export.impl; |
24 |
|
25 |
import java.io.File; |
26 |
import java.io.FileInputStream; |
27 |
import java.io.FileOutputStream; |
28 |
import java.io.IOException; |
29 |
import java.util.ArrayList; |
30 |
import java.util.HashMap; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
import java.util.Properties; |
34 |
import org.apache.commons.io.IOUtils; |
35 |
import org.cresques.cts.IProjection; |
36 |
|
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
import org.gvsig.export.ExportManager; |
41 |
import org.gvsig.export.ExportProcess; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
|
44 |
/**
|
45 |
* Default {@link ExportManager} implementation.
|
46 |
*
|
47 |
* @author gvSIG Team
|
48 |
* @version $Id$
|
49 |
*/
|
50 |
public class DefaultExportManager implements ExportManager { |
51 |
|
52 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultExportManager.class); |
53 |
|
54 |
private static class DefaultExportFilter implements ExportFilter { |
55 |
|
56 |
private final String name; |
57 |
private final String expression; |
58 |
|
59 |
public DefaultExportFilter(String name, String expression) { |
60 |
this.name = name;
|
61 |
this.expression = expression;
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String getName() { |
66 |
return name;
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
public String getExpression() { |
71 |
return expression;
|
72 |
} |
73 |
} |
74 |
|
75 |
private Map<String, ExportFilter> filters = null; |
76 |
private File homeFolder = null; |
77 |
|
78 |
public DefaultExportManager() {
|
79 |
super();
|
80 |
} |
81 |
|
82 |
private void loadFilters() { |
83 |
this.filters = new HashMap<>(); |
84 |
if (this.homeFolder != null) { |
85 |
File filtersFile = new File(this.homeFolder, "filters"); |
86 |
if (filtersFile.exists()) {
|
87 |
FileInputStream fis = null; |
88 |
try {
|
89 |
fis = new FileInputStream(filtersFile); |
90 |
Properties properties = new Properties(); |
91 |
properties.load(fis); |
92 |
for (Object name : properties.keySet()) { |
93 |
String value = (String) properties.get(name); |
94 |
ExportFilter filter = new DefaultExportFilter((String) name, value); |
95 |
this.filters.put(filter.getName(), filter);
|
96 |
} |
97 |
} catch (IOException ex) { |
98 |
LOG.warn("Can't load filters file '" + filtersFile.getAbsolutePath() + "'.", ex); |
99 |
} finally {
|
100 |
IOUtils.closeQuietly(fis); |
101 |
} |
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
public void saveFilters() { |
107 |
if (this.homeFolder != null) { |
108 |
Properties properties = new Properties(); |
109 |
for (ExportFilter filter : this.filters.values()) { |
110 |
properties.setProperty(filter.getName(), filter.getExpression()); |
111 |
} |
112 |
File filtersFile = new File(this.homeFolder, "filters"); |
113 |
FileOutputStream fos = null; |
114 |
try {
|
115 |
fos = new FileOutputStream(filtersFile); |
116 |
properties.store(fos, "");
|
117 |
} catch (IOException ex) { |
118 |
LOG.warn("Can't load filters file '" + filtersFile.getAbsolutePath() + "'.", ex); |
119 |
} finally {
|
120 |
IOUtils.closeQuietly(fos); |
121 |
} |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
public Map<String,ExportFilter> getFiltersAsMap() { |
127 |
if (this.filters == null) { |
128 |
loadFilters(); |
129 |
} |
130 |
return this.filters; |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public List<ExportFilter> getFilters() { |
135 |
List<ExportFilter> l = new ArrayList<>(); |
136 |
l.addAll(this.getFiltersAsMap().values());
|
137 |
return l;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public ExportFilter getFilter(String filterName) { |
142 |
return this.getFiltersAsMap().get(filterName); |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public ExportFilter addFilter(String name, String expression) { |
147 |
ExportFilter filter = new DefaultExportFilter(name, expression);
|
148 |
this.filters.put(filter.getName(), filter);
|
149 |
return filter;
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void setHomeFolder(File homeFolder) { |
154 |
this.homeFolder = homeFolder;
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public ExportProcess createProcess() {
|
159 |
ExportProcess process = new DefaultExportProcess();
|
160 |
return process;
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public ExportProcess createProcess(FeatureStore store) {
|
165 |
ExportProcess process = new DefaultExportProcess();
|
166 |
process.setSourceFeatureStore(store); |
167 |
return process;
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public ExportProcess createProcess(FeatureStore store, IProjection contextProjection) {
|
172 |
ExportProcess process = new DefaultExportProcess();
|
173 |
process.setSourceFeatureStore(store); |
174 |
process.setContextProjection(contextProjection); |
175 |
return process;
|
176 |
} |
177 |
|
178 |
} |