svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeaturesWriter.java @ 21865
History | View | Annotate | Download (3.93 KB)
1 |
package org.gvsig.data.datastores.vectorial.file.dbf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.gvsig.data.DataException; |
7 |
import org.gvsig.data.OpenException; |
8 |
import org.gvsig.data.ReadException; |
9 |
import org.gvsig.data.WriteException; |
10 |
import org.gvsig.data.datastores.vectorial.FeaturesWriter; |
11 |
import org.gvsig.data.datastores.vectorial.InitializeWriterException; |
12 |
import org.gvsig.data.datastores.vectorial.UnsupportedEncodingException; |
13 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileHeader; |
14 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileWriter; |
15 |
import org.gvsig.data.vectorial.Feature; |
16 |
import org.gvsig.data.vectorial.FeatureStore; |
17 |
import org.gvsig.data.vectorial.FeatureType; |
18 |
import org.gvsig.datasources.common.ICharset; |
19 |
import org.gvsig.datasources.common.IRandomAccessFile; |
20 |
import org.gvsig.datasources.common.IRandomFileChannel; |
21 |
import org.gvsig.datasources.impljme.Impl; |
22 |
|
23 |
public class DBFFeaturesWriter implements FeaturesWriter { |
24 |
private DbaseFileWriter dbfWrite;
|
25 |
|
26 |
private DbaseFileHeader myHeader;
|
27 |
|
28 |
private FeatureType featureType;
|
29 |
|
30 |
private File dbfFile; |
31 |
|
32 |
protected int numRows; |
33 |
|
34 |
private ICharset charset = Impl.getCharsetForName("ISO-8859-1"); |
35 |
|
36 |
public void setFile(File file) { |
37 |
dbfFile = file; |
38 |
} |
39 |
|
40 |
public void init(FeatureStore store) throws InitializeWriterException { |
41 |
DBFStoreParameters parameters = (DBFStoreParameters) store |
42 |
.getParameters(); |
43 |
dbfFile = ((DBFStoreParameters) parameters).getDBFFile(); |
44 |
} |
45 |
|
46 |
public void postProcess() throws OpenException, WriteException { |
47 |
try {
|
48 |
myHeader.setNumRecords(numRows); |
49 |
dbfWrite = new DbaseFileWriter(myHeader, getWriteChannel(dbfFile
|
50 |
.getAbsolutePath())); |
51 |
|
52 |
} catch (IOException e) { |
53 |
throw new WriteException(DBFStore.DATASTORE_NAME, e); |
54 |
} |
55 |
} |
56 |
|
57 |
public void preProcess() throws WriteException, ReadException { |
58 |
myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
59 |
myHeader.setNumRecords(0);
|
60 |
numRows = 0;
|
61 |
try {
|
62 |
dbfWrite = new DbaseFileWriter(myHeader, getWriteChannel(dbfFile
|
63 |
.getAbsolutePath())); |
64 |
dbfWrite.setCharset(charset); |
65 |
} catch (IOException e) { |
66 |
throw new InitializeWriterException("DBF Feature Writer", e); |
67 |
} |
68 |
} |
69 |
|
70 |
public void insertFeature(Feature feature) throws WriteException, |
71 |
UnsupportedEncodingException {
|
72 |
dbfWrite.write(feature); |
73 |
numRows++; |
74 |
|
75 |
} |
76 |
|
77 |
public void updateFeatureType(FeatureType featureType) { |
78 |
this.featureType = featureType;
|
79 |
} |
80 |
|
81 |
protected static IRandomFileChannel getWriteChannel(String path) |
82 |
throws IOException { |
83 |
IRandomFileChannel channel; |
84 |
|
85 |
File f = new File(path); |
86 |
|
87 |
if (!f.exists()) {
|
88 |
// System.out.println("Creando fichero " + f.getAbsolutePath());
|
89 |
|
90 |
if (!f.createNewFile()) {
|
91 |
System.err.print("Error al crear el fichero " |
92 |
+ f.getAbsolutePath()); |
93 |
throw new IOException("Cannot create file " + f); |
94 |
} |
95 |
} |
96 |
|
97 |
IRandomAccessFile raf = Impl.getRandomAccessFile(f, "rw");
|
98 |
channel = raf.getChannel(); |
99 |
|
100 |
return channel;
|
101 |
} |
102 |
|
103 |
public static void create(DBFStoreParameters parameters, |
104 |
FeatureType featureType) throws OpenException, WriteException {
|
105 |
DbaseFileHeader myHeader = DbaseFileHeader |
106 |
.createDbaseHeader(featureType); |
107 |
try {
|
108 |
IRandomFileChannel filechannel = getWriteChannel(parameters |
109 |
.getDBFFile().getAbsolutePath()); |
110 |
DbaseFileWriter dbfWrite = new DbaseFileWriter(myHeader,
|
111 |
filechannel); |
112 |
filechannel.close(); |
113 |
} catch (IOException e) { |
114 |
throw new InitializeWriterException("DBF Feature Writer", e); |
115 |
} |
116 |
} |
117 |
|
118 |
/* (non-Javadoc)
|
119 |
* @see org.gvsig.data.datastores.vectorial.FeaturesWriter#cancelProcess()
|
120 |
*/
|
121 |
public void cancelProcess() throws WriteException { |
122 |
// TODO Auto-generated method stub
|
123 |
// RESTAURAR Ficheros anteriores????
|
124 |
|
125 |
} |
126 |
/* (non-Javadoc)
|
127 |
* @see org.gvsig.data.datastores.vectorial.FeaturesWriter#dispose()
|
128 |
*/
|
129 |
public void dispose() throws DataException { |
130 |
this.dbfFile = null; |
131 |
if (this.dbfWrite != null){ |
132 |
this.dbfWrite.close();
|
133 |
this.dbfWrite = null; |
134 |
} |
135 |
this.featureType = null; |
136 |
this.myHeader = null; |
137 |
|
138 |
} |
139 |
|
140 |
} |