root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dxf / DXFFilesystemServerProvider.java @ 25789
History | View | Annotate | Download (5.42 KB)
1 |
package org.gvsig.fmap.dal.store.dxf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.cresques.cts.IProjection; |
7 |
import org.cresques.io.DxfFile; |
8 |
import org.cresques.px.dxf.DxfEntityMaker; |
9 |
import org.gvsig.fmap.dal.DALLocator; |
10 |
import org.gvsig.fmap.dal.DataManager; |
11 |
import org.gvsig.fmap.dal.DataStoreParameters; |
12 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
13 |
import org.gvsig.fmap.dal.exception.CreateException; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
16 |
import org.gvsig.fmap.dal.exception.InitializeException; |
17 |
import org.gvsig.fmap.dal.exception.RemoveException; |
18 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
19 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
20 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
21 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
22 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
23 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
24 |
|
25 |
public class DXFFilesystemServerProvider implements |
26 |
FilesystemServerExplorerProvider, ResourceConsumer { |
27 |
|
28 |
private FilesystemServerExplorerProviderServices serverExplorer;
|
29 |
|
30 |
public String getDataStoreProviderName() { |
31 |
return DXFStoreProvider.NAME;
|
32 |
} |
33 |
|
34 |
public boolean accept(File pathname) { |
35 |
return (pathname.getName().toLowerCase().endsWith(".dxf")); |
36 |
} |
37 |
|
38 |
public String getDescription() { |
39 |
return DXFStoreProvider.DESCRIPTION;
|
40 |
} |
41 |
|
42 |
public DataStoreParameters getParameters(File file) throws DataException { |
43 |
DataManager manager = DALLocator.getDataManager(); |
44 |
DXFStoreParameters params = (DXFStoreParameters) manager |
45 |
.createStoreParameters(this
|
46 |
.getDataStoreProviderName()); |
47 |
params.setFileName(file.getPath()); |
48 |
return params;
|
49 |
} |
50 |
|
51 |
public boolean canCreate() { |
52 |
return true; |
53 |
} |
54 |
|
55 |
public boolean canCreate(NewDataStoreParameters parameters) { |
56 |
if (!(parameters instanceof DXFStoreParameters)) { |
57 |
throw new IllegalArgumentException(); // FIXME ??? |
58 |
} |
59 |
DXFStoreParameters dxfParams = (DXFStoreParameters) parameters; |
60 |
// TODO comporbar si el ftype es correcto (para este formato es fijo)
|
61 |
File file = new File(dxfParams.getFileName()); |
62 |
|
63 |
if (dxfParams.getSRSID() == null) { |
64 |
return false; |
65 |
} |
66 |
// TODO comprobamos extension del fichero ??
|
67 |
if (file.exists()) {
|
68 |
return file.canWrite();
|
69 |
} else {
|
70 |
return file.getParentFile().canWrite();
|
71 |
} |
72 |
} |
73 |
|
74 |
public void create(NewDataStoreParameters parameters, boolean overwrite) |
75 |
throws CreateException {
|
76 |
|
77 |
IProjection projection = null;
|
78 |
DXFStoreParameters params = (DXFStoreParameters) parameters; |
79 |
|
80 |
if (params.getFile().exists()) {
|
81 |
if (overwrite) {
|
82 |
if (!params.getFile().delete()) {
|
83 |
throw new CreateException(this.getDataStoreProviderName(), |
84 |
new IOException( |
85 |
"cannot delete file"));
|
86 |
} |
87 |
} else {
|
88 |
throw new CreateException(this.getDataStoreProviderName(), |
89 |
new IOException( |
90 |
"file already exist"));
|
91 |
} |
92 |
} |
93 |
|
94 |
// projection = CRSFactory.getCRS(params.getSRSID());
|
95 |
|
96 |
File file = params.getFile();
|
97 |
FileResource resource; |
98 |
try {
|
99 |
resource = (FileResource) this.serverExplorer
|
100 |
.getServerExplorerProviderServices().createResource( |
101 |
FileResource.NAME, |
102 |
new Object[] { file.getAbsolutePath() }); |
103 |
} catch (InitializeException e1) {
|
104 |
throw new CreateException(params.getFileName(), e1); |
105 |
} |
106 |
resource.addConsumer(this);
|
107 |
|
108 |
try {
|
109 |
resource.begin(); |
110 |
Builder builder = new Builder().initialice(resource.getFileName(),
|
111 |
projection); |
112 |
resource.notifyOpen(); |
113 |
builder.begin(); |
114 |
builder.create(); |
115 |
builder.end(); |
116 |
resource.notifyClose(); |
117 |
|
118 |
resource.setData(null); // FIXME: Seguro que hay que ponerlo a null |
119 |
// ??
|
120 |
resource.notifyChanges(); |
121 |
} catch (Exception e) { |
122 |
throw new CreateException(params.getFileName(), e); |
123 |
} finally {
|
124 |
resource.end(); |
125 |
resource.removeConsumer(this);
|
126 |
} |
127 |
} |
128 |
|
129 |
public class Builder { |
130 |
private String fileName; |
131 |
private IProjection projection;
|
132 |
|
133 |
public Builder initialice(String fileName, IProjection projection) { |
134 |
this.fileName = fileName;
|
135 |
this.projection = projection;
|
136 |
return this; |
137 |
} |
138 |
|
139 |
public void begin() { |
140 |
|
141 |
} |
142 |
|
143 |
public void end() { |
144 |
|
145 |
} |
146 |
|
147 |
public void create() throws IOException { |
148 |
DxfEntityMaker entityMaker = new DxfEntityMaker(projection);
|
149 |
DxfFile dxfFile = new DxfFile(null, fileName, entityMaker); |
150 |
dxfFile.setCadFlag(true);
|
151 |
dxfFile.setDxf3DFlag(false);
|
152 |
dxfFile.save(fileName); |
153 |
} |
154 |
} |
155 |
|
156 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
157 |
return (NewFeatureStoreParameters) DALLocator.getDataManager()
|
158 |
.createStoreParameters(this.getDataStoreProviderName());
|
159 |
} |
160 |
|
161 |
public void initialize( |
162 |
FilesystemServerExplorerProviderServices serverExplorer) { |
163 |
this.serverExplorer = serverExplorer;
|
164 |
} |
165 |
|
166 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
167 |
File file = new File(((DXFStoreParameters) parameters).getFileName()); |
168 |
if (!file.exists()) {
|
169 |
throw new RemoveException(this.getDataStoreProviderName(), |
170 |
new FileNotFoundException(file)); |
171 |
} |
172 |
if (!file.delete()) {
|
173 |
// FIXME throws ???
|
174 |
} |
175 |
|
176 |
} |
177 |
|
178 |
public boolean closeResourceRequested(ResourceProvider resource) { |
179 |
// while it is using a resource anyone can't close it
|
180 |
return false; |
181 |
} |
182 |
|
183 |
/*
|
184 |
* (non-Javadoc)
|
185 |
*
|
186 |
* @see
|
187 |
* org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
188 |
* gvsig.fmap.dal.resource.spi.ResourceProvider)
|
189 |
*/
|
190 |
public void resourceChanged(ResourceProvider resource) { |
191 |
//Do nothing
|
192 |
|
193 |
} |
194 |
|
195 |
|
196 |
} |