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