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 @ 44455
History | View | Annotate | Download (11.4 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 | 44309 | omartinez | public boolean accept(File pathname) { |
75 | return (pathname.getName().toLowerCase().endsWith(".csv")); |
||
76 | } |
||
77 | 40846 | jjdelcerro | |
78 | 43400 | jjdelcerro | @Override
|
79 | 44309 | omartinez | public String getDescription() { |
80 | return CSVStoreProvider.DESCRIPTION;
|
||
81 | } |
||
82 | 40846 | jjdelcerro | |
83 | 44309 | omartinez | @Override
|
84 | public DataStoreParameters getParameters(File file) throws DataException { |
||
85 | DataManager manager = DALLocator.getDataManager(); |
||
86 | CSVStoreParameters params = (CSVStoreParameters) manager |
||
87 | .createStoreParameters(this.getDataStoreProviderName());
|
||
88 | params.setFile(file); |
||
89 | return params;
|
||
90 | } |
||
91 | 40846 | jjdelcerro | |
92 | 44309 | omartinez | @Override
|
93 | public boolean canCreate() { |
||
94 | return true; |
||
95 | } |
||
96 | |||
97 | @Override
|
||
98 | public boolean canCreate(NewDataStoreParameters parameters) { |
||
99 | CSVNewStoreParameters params = (CSVNewStoreParameters) parameters; |
||
100 | if (params.getFile().getParentFile().canWrite()) {
|
||
101 | return false; |
||
102 | } |
||
103 | if (params.getFile().exists()) {
|
||
104 | if (!params.getFile().canWrite()) {
|
||
105 | return false; |
||
106 | } |
||
107 | } |
||
108 | if (params.getDefaultFeatureType() == null) { |
||
109 | return false; |
||
110 | } |
||
111 | return true; |
||
112 | } |
||
113 | |||
114 | public void create(NewDataStoreParameters parameters, boolean overwrite) |
||
115 | throws CreateException {
|
||
116 | |||
117 | final CSVNewStoreParameters params = (CSVNewStoreParameters) parameters;
|
||
118 | final File file = params.getFile(); |
||
119 | |||
120 | if (!file.isAbsolute()) {
|
||
121 | throw new AbsolutePathRequiredException(file.getPath()); |
||
122 | } |
||
123 | |||
124 | if (file.exists()) {
|
||
125 | if (overwrite) {
|
||
126 | // if (!file.delete()) {
|
||
127 | // throw new CreateException(this.getDataStoreProviderName(),
|
||
128 | // new IOException(
|
||
129 | // "cannot delete file"));
|
||
130 | // }
|
||
131 | } else {
|
||
132 | throw new CreateException(this.getDataStoreProviderName(), |
||
133 | new IOException( |
||
134 | "file already exist"));
|
||
135 | } |
||
136 | } |
||
137 | |||
138 | final FileResource resource;
|
||
139 | try {
|
||
140 | |||
141 | resource = (FileResource) this.serverExplorer
|
||
142 | .getServerExplorerProviderServices().createResource( |
||
143 | FileResource.NAME, |
||
144 | new Object[]{file.getAbsolutePath()}); |
||
145 | } catch (InitializeException e1) {
|
||
146 | throw new CreateException(params.getFile().getAbsolutePath(), e1); |
||
147 | } |
||
148 | resource.addConsumer(this);
|
||
149 | |||
150 | try {
|
||
151 | resource.execute(new ResourceAction() {
|
||
152 | @Override
|
||
153 | public Object run() throws Exception { |
||
154 | 44388 | omartinez | String s;
|
155 | char quoteChar;
|
||
156 | int delimiterChar;
|
||
157 | String endOfLineSymbols;
|
||
158 | |||
159 | 44309 | omartinez | CSVFeatureWriter writer = new CSVFeatureWriter();
|
160 | 44388 | omartinez | |
161 | CSVNewStoreParameters newparams = (CSVNewStoreParameters) params; |
||
162 | CsvPreference defaultPreference = CSVStoreParameters.getPredefinedCSVPreferences(newparams); |
||
163 | 44395 | omartinez | |
164 | 44388 | omartinez | s = CSVStoreParameters.getQuoteCharacter(params); |
165 | if (StringUtils.isBlank(s)) {
|
||
166 | quoteChar = (char) defaultPreference.getQuoteChar();
|
||
167 | } else {
|
||
168 | quoteChar = s.charAt(0);
|
||
169 | } |
||
170 | |||
171 | s = CSVStoreParameters.getDelimiter(newparams); |
||
172 | if (StringUtils.isBlank(s)) {
|
||
173 | delimiterChar = defaultPreference.getDelimiterChar(); |
||
174 | } else {
|
||
175 | delimiterChar = s.charAt(0);
|
||
176 | } |
||
177 | |||
178 | endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
||
179 | if (StringUtils.isBlank(endOfLineSymbols)) {
|
||
180 | endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
||
181 | } |
||
182 | |||
183 | CsvPreference.Builder builder = new CsvPreference.Builder(quoteChar, delimiterChar, endOfLineSymbols);
|
||
184 | |||
185 | s = CSVStoreParameters.getCommentStartMarker(newparams); |
||
186 | if (!StringUtils.isBlank(s)) {
|
||
187 | CommentStartsWith cs = new CommentStartsWith(s);
|
||
188 | builder.skipComments(cs); |
||
189 | } |
||
190 | |||
191 | 44395 | omartinez | //builder.surroundingSpacesNeedQuotes(CSVStoreParameters
|
192 | // .getSurroundingSpacesNeedQuotes(newparams));
|
||
193 | builder.surroundingSpacesNeedQuotes(true);
|
||
194 | |||
195 | 44388 | omartinez | QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(newparams); |
196 | if (quoteMode != null) { |
||
197 | builder.useQuoteMode(quoteMode); |
||
198 | } |
||
199 | |||
200 | CsvPreference csvpreferences = builder.build(); |
||
201 | 44395 | omartinez | |
202 | 44388 | omartinez | writer.initialize(params, file.getAbsoluteFile(), params.getDefaultFeatureType(), csvpreferences); |
203 | 44309 | omartinez | writer.begin(); |
204 | |||
205 | // writer.begin(
|
||
206 | // params,
|
||
207 | // fixFeatureType(params.getDefaultFeatureType()),
|
||
208 | // 0
|
||
209 | // );
|
||
210 | writer.end(); |
||
211 | //writer.dispose();
|
||
212 | resource.notifyChanges(); |
||
213 | return null; |
||
214 | } |
||
215 | }); |
||
216 | } catch (ResourceExecuteException e) {
|
||
217 | throw new CreateException(this.getDataStoreProviderName(), e); |
||
218 | } finally {
|
||
219 | resource.removeConsumer(this);
|
||
220 | } |
||
221 | } |
||
222 | |||
223 | protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
|
||
224 | return new CSVNewStoreParameters(); |
||
225 | } |
||
226 | |||
227 | @Override
|
||
228 | public NewDataStoreParameters getCreateParameters() {
|
||
229 | NewFeatureStoreParameters params = (NewFeatureStoreParameters) this
|
||
230 | .createInstanceNewDataStoreParameters(); |
||
231 | params.setDefaultFeatureType(this.serverExplorer
|
||
232 | .getServerExplorerProviderServices().createNewFeatureType()); |
||
233 | return params;
|
||
234 | } |
||
235 | |||
236 | @Override
|
||
237 | public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
||
238 | this.serverExplorer = serverExplorer;
|
||
239 | |||
240 | } |
||
241 | |||
242 | @Override
|
||
243 | public void remove(DataStoreParameters parameters) throws RemoveException { |
||
244 | CSVStoreParameters params = (CSVStoreParameters) parameters; |
||
245 | File file = params.getFile();
|
||
246 | if (!file.exists()) {
|
||
247 | throw new RemoveException(this.getDataStoreProviderName(), |
||
248 | new FileNotFoundException(params.getFile())); |
||
249 | } |
||
250 | if (!file.delete()) {
|
||
251 | throw new RemoveException(this.getDataStoreProviderName(), |
||
252 | new IOException()); // FIXME Exception |
||
253 | } |
||
254 | } |
||
255 | |||
256 | @Override
|
||
257 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
258 | // while it is using a resource anyone can't close it
|
||
259 | return !(this.equals(resource)); |
||
260 | } |
||
261 | |||
262 | /*
|
||
263 | * (non-Javadoc)
|
||
264 | *
|
||
265 | * @see
|
||
266 | * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
||
267 | * gvsig.fmap.dal.resource.spi.ResourceProvider)
|
||
268 | */
|
||
269 | @Override
|
||
270 | public void resourceChanged(ResourceProvider resource) { |
||
271 | // Do nothing
|
||
272 | } |
||
273 | |||
274 | protected EditableFeatureType fixFeatureType(FeatureType featureType) {
|
||
275 | EditableFeatureType newFeatureType; |
||
276 | |||
277 | if (featureType instanceof EditableFeatureType) { |
||
278 | newFeatureType = (EditableFeatureType) featureType.getCopy(); |
||
279 | } else {
|
||
280 | newFeatureType = featureType.getEditable(); |
||
281 | } |
||
282 | |||
283 | for (int i = 0; i < newFeatureType.size(); i++) { |
||
284 | EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) newFeatureType.getAttributeDescriptor(i); |
||
285 | String s;
|
||
286 | switch (attr.getType()) {
|
||
287 | case DataTypes.INT:
|
||
288 | s = String.valueOf(Integer.MAX_VALUE); |
||
289 | attr.setSize(18);
|
||
290 | attr.setPrecision(0);
|
||
291 | break;
|
||
292 | case DataTypes.LONG:
|
||
293 | s = String.valueOf(Long.MAX_VALUE); |
||
294 | attr.setSize(18);
|
||
295 | attr.setPrecision(0);
|
||
296 | break;
|
||
297 | case DataTypes.FLOAT:
|
||
298 | attr.setSize(18);
|
||
299 | attr.setPrecision(6);
|
||
300 | break;
|
||
301 | case DataTypes.DOUBLE:
|
||
302 | attr.setSize(18);
|
||
303 | attr.setPrecision(6);
|
||
304 | break;
|
||
305 | } |
||
306 | } |
||
307 | return newFeatureType;
|
||
308 | } |
||
309 | 40846 | jjdelcerro | } |