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 @ 44390
History | View | Annotate | Download (6.23 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.ExportParameters; |
42 |
import org.gvsig.export.ExportProcess; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.tools.bookmarksandhistory.Bookmarks; |
45 |
import org.gvsig.tools.bookmarksandhistory.History; |
46 |
import org.gvsig.tools.bookmarksandhistory.impl.BaseBookmarks; |
47 |
import org.gvsig.tools.bookmarksandhistory.impl.BaseHistory; |
48 |
|
49 |
/**
|
50 |
* Default {@link ExportManager} implementation.
|
51 |
*
|
52 |
* @author gvSIG Team
|
53 |
* @version $Id$
|
54 |
*/
|
55 |
public class DefaultExportManager implements ExportManager { |
56 |
|
57 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultExportManager.class); |
58 |
|
59 |
private static class DefaultExportFilter implements ExportFilter { |
60 |
|
61 |
private final String name; |
62 |
private final String expression; |
63 |
|
64 |
public DefaultExportFilter(String name, String expression) { |
65 |
this.name = name;
|
66 |
this.expression = expression;
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
public String getName() { |
71 |
return name;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public String getExpression() { |
76 |
return expression;
|
77 |
} |
78 |
} |
79 |
|
80 |
private Map<String, ExportFilter> filters = null; |
81 |
private File homeFolder = null; |
82 |
private final History<ExportParameters> history; |
83 |
private final Bookmarks<ExportParameters> bookmarks; |
84 |
|
85 |
|
86 |
public DefaultExportManager() {
|
87 |
super();
|
88 |
this.history = new BaseHistory<>(10); |
89 |
this.bookmarks = new BaseBookmarks<>(); |
90 |
} |
91 |
|
92 |
private void loadFilters() { |
93 |
this.filters = new HashMap<>(); |
94 |
if (this.homeFolder != null) { |
95 |
File filtersFile = new File(this.homeFolder, "filters"); |
96 |
if (filtersFile.exists()) {
|
97 |
FileInputStream fis = null; |
98 |
try {
|
99 |
fis = new FileInputStream(filtersFile); |
100 |
Properties properties = new Properties(); |
101 |
properties.load(fis); |
102 |
for (Object name : properties.keySet()) { |
103 |
String value = (String) properties.get(name); |
104 |
ExportFilter filter = new DefaultExportFilter((String) name, value); |
105 |
this.filters.put(filter.getName(), filter);
|
106 |
} |
107 |
} catch (IOException ex) { |
108 |
LOG.warn("Can't load filters file '" + filtersFile.getAbsolutePath() + "'.", ex); |
109 |
} finally {
|
110 |
IOUtils.closeQuietly(fis); |
111 |
} |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
public void saveFilters() { |
117 |
if (this.homeFolder != null) { |
118 |
Properties properties = new Properties(); |
119 |
for (ExportFilter filter : this.filters.values()) { |
120 |
properties.setProperty(filter.getName(), filter.getExpression()); |
121 |
} |
122 |
File filtersFile = new File(this.homeFolder, "filters"); |
123 |
FileOutputStream fos = null; |
124 |
try {
|
125 |
fos = new FileOutputStream(filtersFile); |
126 |
properties.store(fos, "");
|
127 |
} catch (IOException ex) { |
128 |
LOG.warn("Can't load filters file '" + filtersFile.getAbsolutePath() + "'.", ex); |
129 |
} finally {
|
130 |
IOUtils.closeQuietly(fos); |
131 |
} |
132 |
} |
133 |
|
134 |
} |
135 |
|
136 |
public Map<String,ExportFilter> getFiltersAsMap() { |
137 |
if (this.filters == null) { |
138 |
loadFilters(); |
139 |
} |
140 |
return this.filters; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public List<ExportFilter> getFilters() { |
145 |
List<ExportFilter> l = new ArrayList<>(); |
146 |
l.addAll(this.getFiltersAsMap().values());
|
147 |
return l;
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public ExportFilter getFilter(String filterName) { |
152 |
return this.getFiltersAsMap().get(filterName); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public ExportFilter addFilter(String name, String expression) { |
157 |
ExportFilter filter = new DefaultExportFilter(name, expression);
|
158 |
this.filters.put(filter.getName(), filter);
|
159 |
return filter;
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void setHomeFolder(File homeFolder) { |
164 |
this.homeFolder = homeFolder;
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
public ExportProcess createProcess() {
|
169 |
ExportProcess process = new DefaultExportProcess();
|
170 |
return process;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public ExportProcess createProcess(FeatureStore store) {
|
175 |
ExportProcess process = new DefaultExportProcess();
|
176 |
process.setSourceFeatureStore(store); |
177 |
return process;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public ExportProcess createProcess(FeatureStore store, IProjection contextProjection) {
|
182 |
ExportProcess process = new DefaultExportProcess();
|
183 |
process.setSourceFeatureStore(store); |
184 |
process.setContextProjection(contextProjection); |
185 |
return process;
|
186 |
} |
187 |
|
188 |
|
189 |
@Override
|
190 |
public History<ExportParameters> getHistory() {
|
191 |
return this.history; |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public Bookmarks<ExportParameters> getBookmarks() {
|
196 |
return this.bookmarks; |
197 |
} |
198 |
} |