svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / CSVFilesystemServerProvider.java @ 45721
History | View | Annotate | Download (11.7 KB)
1 | 40846 | jjdelcerro | /**
|
---|---|---|---|
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
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.fmap.dal.store.csv; |
||
25 | |||
26 | import java.io.File; |
||
27 | 44309 | omartinez | import java.io.IOException; |
28 | 44388 | omartinez | import org.apache.commons.lang3.StringUtils; |
29 | 44309 | omartinez | import org.gvsig.fmap.dal.DALLocator; |
30 | import org.gvsig.fmap.dal.DataManager; |
||
31 | 40846 | jjdelcerro | |
32 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
33 | 44309 | omartinez | import org.gvsig.fmap.dal.DataStoreParameters; |
34 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
35 | import org.gvsig.fmap.dal.exception.CreateException; |
||
36 | import org.gvsig.fmap.dal.exception.DataException; |
||
37 | import org.gvsig.fmap.dal.exception.FileNotFoundException; |
||
38 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
39 | import org.gvsig.fmap.dal.exception.RemoveException; |
||
40 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
41 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
43 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
44 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
45 | import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
||
46 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
47 | 40846 | jjdelcerro | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
48 | 44309 | omartinez | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
49 | import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException; |
||
50 | 40846 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
51 | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
||
52 | 44309 | omartinez | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
53 | import org.gvsig.tools.dataTypes.DataTypes; |
||
54 | 44388 | omartinez | import org.supercsv.comment.CommentStartsWith; |
55 | 44309 | omartinez | import org.supercsv.prefs.CsvPreference; |
56 | 44388 | omartinez | import org.supercsv.quote.QuoteMode; |
57 | 40846 | jjdelcerro | |
58 | 44309 | omartinez | public class CSVFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider |
59 | implements FilesystemServerExplorerProvider, ResourceConsumer {
|
||
60 | 40846 | jjdelcerro | |
61 | 44309 | omartinez | protected FilesystemServerExplorerProviderServices serverExplorer;
|
62 | |||
63 | 43400 | jjdelcerro | @Override
|
64 | 44309 | omartinez | public String getDataStoreProviderName() { |
65 | return CSVStoreProvider.NAME;
|
||
66 | } |
||
67 | 40846 | jjdelcerro | |
68 | 43400 | jjdelcerro | @Override
|
69 | 44309 | omartinez | public int getMode() { |
70 | return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
|
||
71 | } |
||
72 | 40846 | jjdelcerro | |
73 | 43400 | jjdelcerro | @Override
|
74 | 45563 | jolivas | public boolean accept(File file) { |
75 | String pathname = file.getName().toLowerCase();
|
||
76 | if ( pathname.endsWith(".csv") || pathname.endsWith(".json")){ |
||
77 | return true; |
||
78 | } |
||
79 | return false; |
||
80 | 44309 | omartinez | } |
81 | 40846 | jjdelcerro | |
82 | 43400 | jjdelcerro | @Override
|
83 | 44309 | omartinez | public String getDescription() { |
84 | return CSVStoreProvider.DESCRIPTION;
|
||
85 | } |
||
86 | 40846 | jjdelcerro | |
87 | 44309 | omartinez | @Override
|
88 | public DataStoreParameters getParameters(File file) throws DataException { |
||
89 | DataManager manager = DALLocator.getDataManager(); |
||
90 | CSVStoreParameters params = (CSVStoreParameters) manager |
||
91 | .createStoreParameters(this.getDataStoreProviderName());
|
||
92 | params.setFile(file); |
||
93 | return params;
|
||
94 | } |
||
95 | 40846 | jjdelcerro | |
96 | 44309 | omartinez | @Override
|
97 | public boolean canCreate() { |
||
98 | return true; |
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public boolean canCreate(NewDataStoreParameters parameters) { |
||
103 | CSVNewStoreParameters params = (CSVNewStoreParameters) parameters; |
||
104 | if (params.getFile().getParentFile().canWrite()) {
|
||
105 | return false; |
||
106 | } |
||
107 | if (params.getFile().exists()) {
|
||
108 | if (!params.getFile().canWrite()) {
|
||
109 | return false; |
||
110 | } |
||
111 | } |
||
112 | if (params.getDefaultFeatureType() == null) { |
||
113 | return false; |
||
114 | } |
||
115 | return true; |
||
116 | } |
||
117 | |||
118 | public void create(NewDataStoreParameters parameters, boolean overwrite) |
||
119 | throws CreateException {
|
||
120 | |||
121 | final CSVNewStoreParameters params = (CSVNewStoreParameters) parameters;
|
||
122 | final File file = params.getFile(); |
||
123 | |||
124 | if (!file.isAbsolute()) {
|
||
125 | throw new AbsolutePathRequiredException(file.getPath()); |
||
126 | } |
||
127 | |||
128 | if (file.exists()) {
|
||
129 | if (overwrite) {
|
||
130 | // if (!file.delete()) {
|
||
131 | // throw new CreateException(this.getDataStoreProviderName(),
|
||
132 | // new IOException(
|
||
133 | // "cannot delete file"));
|
||
134 | // }
|
||
135 | } else {
|
||
136 | throw new CreateException(this.getDataStoreProviderName(), |
||
137 | new IOException( |
||
138 | "file already exist"));
|
||
139 | } |
||
140 | } |
||
141 | |||
142 | final FileResource resource;
|
||
143 | try {
|
||
144 | |||
145 | resource = (FileResource) this.serverExplorer
|
||
146 | .getServerExplorerProviderServices().createResource( |
||
147 | FileResource.NAME, |
||
148 | new Object[]{file.getAbsolutePath()}); |
||
149 | } catch (InitializeException e1) {
|
||
150 | throw new CreateException(params.getFile().getAbsolutePath(), e1); |
||
151 | } |
||
152 | resource.addConsumer(this);
|
||
153 | |||
154 | try {
|
||
155 | resource.execute(new ResourceAction() {
|
||
156 | @Override
|
||
157 | public Object run() throws Exception { |
||
158 | 44388 | omartinez | String s;
|
159 | char quoteChar;
|
||
160 | int delimiterChar;
|
||
161 | String endOfLineSymbols;
|
||
162 | |||
163 | 44309 | omartinez | CSVFeatureWriter writer = new CSVFeatureWriter();
|
164 | 44388 | omartinez | |
165 | CSVNewStoreParameters newparams = (CSVNewStoreParameters) params; |
||
166 | CsvPreference defaultPreference = CSVStoreParameters.getPredefinedCSVPreferences(newparams); |
||
167 | 44395 | omartinez | |
168 | 44388 | omartinez | s = CSVStoreParameters.getQuoteCharacter(params); |
169 | if (StringUtils.isBlank(s)) {
|
||
170 | quoteChar = (char) defaultPreference.getQuoteChar();
|
||
171 | } else {
|
||
172 | quoteChar = s.charAt(0);
|
||
173 | } |
||
174 | |||
175 | s = CSVStoreParameters.getDelimiter(newparams); |
||
176 | if (StringUtils.isBlank(s)) {
|
||
177 | delimiterChar = defaultPreference.getDelimiterChar(); |
||
178 | } else {
|
||
179 | delimiterChar = s.charAt(0);
|
||
180 | } |
||
181 | |||
182 | endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
||
183 | if (StringUtils.isBlank(endOfLineSymbols)) {
|
||
184 | endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
||
185 | } |
||
186 | |||
187 | CsvPreference.Builder builder = new CsvPreference.Builder(quoteChar, delimiterChar, endOfLineSymbols);
|
||
188 | |||
189 | s = CSVStoreParameters.getCommentStartMarker(newparams); |
||
190 | if (!StringUtils.isBlank(s)) {
|
||
191 | CommentStartsWith cs = new CommentStartsWith(s);
|
||
192 | builder.skipComments(cs); |
||
193 | } |
||
194 | |||
195 | 44395 | omartinez | //builder.surroundingSpacesNeedQuotes(CSVStoreParameters
|
196 | // .getSurroundingSpacesNeedQuotes(newparams));
|
||
197 | builder.surroundingSpacesNeedQuotes(true);
|
||
198 | |||
199 | 44388 | omartinez | QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(newparams); |
200 | if (quoteMode != null) { |
||
201 | builder.useQuoteMode(quoteMode); |
||
202 | } |
||
203 | 45359 | omartinez | |
204 | 44388 | omartinez | CsvPreference csvpreferences = builder.build(); |
205 | 44395 | omartinez | |
206 | 44388 | omartinez | writer.initialize(params, file.getAbsoluteFile(), params.getDefaultFeatureType(), csvpreferences); |
207 | 44309 | omartinez | writer.begin(); |
208 | |||
209 | // writer.begin(
|
||
210 | // params,
|
||
211 | // fixFeatureType(params.getDefaultFeatureType()),
|
||
212 | // 0
|
||
213 | // );
|
||
214 | writer.end(); |
||
215 | //writer.dispose();
|
||
216 | resource.notifyChanges(); |
||
217 | return null; |
||
218 | } |
||
219 | }); |
||
220 | } catch (ResourceExecuteException e) {
|
||
221 | throw new CreateException(this.getDataStoreProviderName(), e); |
||
222 | } finally {
|
||
223 | resource.removeConsumer(this);
|
||
224 | } |
||
225 | } |
||
226 | |||
227 | protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
|
||
228 | return new CSVNewStoreParameters(); |
||
229 | } |
||
230 | |||
231 | @Override
|
||
232 | public NewDataStoreParameters getCreateParameters() {
|
||
233 | NewFeatureStoreParameters params = (NewFeatureStoreParameters) this
|
||
234 | .createInstanceNewDataStoreParameters(); |
||
235 | params.setDefaultFeatureType(this.serverExplorer
|
||
236 | .getServerExplorerProviderServices().createNewFeatureType()); |
||
237 | return params;
|
||
238 | } |
||
239 | |||
240 | @Override
|
||
241 | public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
||
242 | this.serverExplorer = serverExplorer;
|
||
243 | |||
244 | } |
||
245 | |||
246 | @Override
|
||
247 | public void remove(DataStoreParameters parameters) throws RemoveException { |
||
248 | CSVStoreParameters params = (CSVStoreParameters) parameters; |
||
249 | File file = params.getFile();
|
||
250 | if (!file.exists()) {
|
||
251 | throw new RemoveException(this.getDataStoreProviderName(), |
||
252 | new FileNotFoundException(params.getFile())); |
||
253 | } |
||
254 | if (!file.delete()) {
|
||
255 | throw new RemoveException(this.getDataStoreProviderName(), |
||
256 | new IOException()); // FIXME Exception |
||
257 | } |
||
258 | } |
||
259 | |||
260 | @Override
|
||
261 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
262 | // while it is using a resource anyone can't close it
|
||
263 | return !(this.equals(resource)); |
||
264 | } |
||
265 | |||
266 | /*
|
||
267 | * (non-Javadoc)
|
||
268 | *
|
||
269 | * @see
|
||
270 | * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
||
271 | * gvsig.fmap.dal.resource.spi.ResourceProvider)
|
||
272 | */
|
||
273 | @Override
|
||
274 | public void resourceChanged(ResourceProvider resource) { |
||
275 | // Do nothing
|
||
276 | } |
||
277 | |||
278 | protected EditableFeatureType fixFeatureType(FeatureType featureType) {
|
||
279 | EditableFeatureType newFeatureType; |
||
280 | |||
281 | if (featureType instanceof EditableFeatureType) { |
||
282 | newFeatureType = (EditableFeatureType) featureType.getCopy(); |
||
283 | } else {
|
||
284 | newFeatureType = featureType.getEditable(); |
||
285 | } |
||
286 | |||
287 | for (int i = 0; i < newFeatureType.size(); i++) { |
||
288 | EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) newFeatureType.getAttributeDescriptor(i); |
||
289 | String s;
|
||
290 | switch (attr.getType()) {
|
||
291 | case DataTypes.INT:
|
||
292 | s = String.valueOf(Integer.MAX_VALUE); |
||
293 | attr.setSize(18);
|
||
294 | attr.setPrecision(0);
|
||
295 | 44669 | jjdelcerro | attr.setScale(0);
|
296 | 44309 | omartinez | break;
|
297 | case DataTypes.LONG:
|
||
298 | s = String.valueOf(Long.MAX_VALUE); |
||
299 | attr.setSize(18);
|
||
300 | attr.setPrecision(0);
|
||
301 | 44669 | jjdelcerro | attr.setScale(0);
|
302 | 44309 | omartinez | break;
|
303 | case DataTypes.FLOAT:
|
||
304 | attr.setSize(18);
|
||
305 | attr.setPrecision(6);
|
||
306 | 44669 | jjdelcerro | attr.setScale(0);
|
307 | 44309 | omartinez | break;
|
308 | case DataTypes.DOUBLE:
|
||
309 | attr.setSize(18);
|
||
310 | attr.setPrecision(6);
|
||
311 | 44669 | jjdelcerro | attr.setScale(0);
|
312 | 44309 | omartinez | break;
|
313 | } |
||
314 | } |
||
315 | return newFeatureType;
|
||
316 | } |
||
317 | 40846 | jjdelcerro | } |