root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeaturesWriter.java @ 21606
History | View | Annotate | Download (3.35 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.datastores.vectorial.IFeaturesWriter; |
7 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileHeader; |
8 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileWriter; |
9 |
import org.gvsig.data.exception.InitializeWriterException; |
10 |
import org.gvsig.data.exception.OpenException; |
11 |
import org.gvsig.data.exception.ReadException; |
12 |
import org.gvsig.data.exception.UnsupportedEncodingException; |
13 |
import org.gvsig.data.exception.WriteException; |
14 |
import org.gvsig.data.vectorial.IFeature; |
15 |
import org.gvsig.data.vectorial.IFeatureStore; |
16 |
import org.gvsig.data.vectorial.IFeatureType; |
17 |
import org.gvsig.datasources.common.ICharset; |
18 |
import org.gvsig.datasources.common.IRandomAccessFile; |
19 |
import org.gvsig.datasources.common.IRandomFileChannel; |
20 |
import org.gvsig.datasources.impljme.Impl; |
21 |
|
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 ICharset charset = Impl.getCharsetForName("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 |
try {
|
40 |
myHeader.setNumRecords(numRows); |
41 |
dbfWrite = new DbaseFileWriter(myHeader,
|
42 |
getWriteChannel(dbfFile.getAbsolutePath())); |
43 |
|
44 |
} catch (IOException e) { |
45 |
throw new WriteException(DBFStore.DATASTORE_NAME,e); |
46 |
} |
47 |
} |
48 |
|
49 |
public void preProcess() throws WriteException, ReadException { |
50 |
myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
51 |
myHeader.setNumRecords(0);
|
52 |
numRows = 0;
|
53 |
try {
|
54 |
dbfWrite = new DbaseFileWriter(myHeader,
|
55 |
getWriteChannel(dbfFile.getAbsolutePath())); |
56 |
dbfWrite.setCharset(charset); |
57 |
} catch (IOException e) { |
58 |
throw new InitializeWriterException("DBF Feature Writer",e); |
59 |
} |
60 |
} |
61 |
|
62 |
public void insertFeature(IFeature feature) throws WriteException, UnsupportedEncodingException { |
63 |
dbfWrite.write(feature); |
64 |
numRows++; |
65 |
|
66 |
} |
67 |
public void updateFeatureType(IFeatureType featureType) { |
68 |
this.featureType=featureType;
|
69 |
} |
70 |
|
71 |
protected static IRandomFileChannel getWriteChannel(String path) throws IOException { |
72 |
IRandomFileChannel channel; |
73 |
|
74 |
File f = new File(path); |
75 |
|
76 |
if (!f.exists()) {
|
77 |
// System.out.println("Creando fichero " + f.getAbsolutePath());
|
78 |
|
79 |
if (!f.createNewFile()) {
|
80 |
System.err.print("Error al crear el fichero " |
81 |
+ f.getAbsolutePath()); |
82 |
throw new IOException("Cannot create file " + f); |
83 |
} |
84 |
} |
85 |
|
86 |
IRandomAccessFile raf = Impl.getRandomAccessFile(f, "rw");
|
87 |
channel = raf.getChannel(); |
88 |
|
89 |
return channel;
|
90 |
} |
91 |
public static void create(DBFStoreParameters parameters, IFeatureType featureType)throws OpenException, WriteException { |
92 |
DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
93 |
try {
|
94 |
IRandomFileChannel filechannel= |
95 |
getWriteChannel(parameters.getDBFFile().getAbsolutePath()); |
96 |
DbaseFileWriter dbfWrite = new DbaseFileWriter(myHeader,filechannel
|
97 |
); |
98 |
filechannel.close(); |
99 |
} catch (IOException e) { |
100 |
throw new InitializeWriterException("DBF Feature Writer",e); |
101 |
} |
102 |
} |
103 |
|
104 |
} |