root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / dbf / DbfWriter.java @ 4328
History | View | Annotate | Download (4.19 KB)
1 |
package com.iver.cit.gvsig.fmap.edition.writers.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.sql.Types; |
9 |
|
10 |
import com.hardcode.driverManager.DriverLoadException; |
11 |
import com.iver.cit.gvsig.fmap.DriverException; |
12 |
import com.iver.cit.gvsig.fmap.core.IRow; |
13 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileHeaderNIO; |
14 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileWriterNIO; |
15 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
16 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
17 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
18 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
19 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
20 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
21 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
22 |
|
23 |
public class DbfWriter extends AbstractWriter { |
24 |
private String dbfPath; |
25 |
|
26 |
private DbaseFileWriterNIO dbfWrite;
|
27 |
|
28 |
private DbaseFileHeaderNIO myHeader;
|
29 |
|
30 |
private int numRows; |
31 |
private Object[] record; |
32 |
|
33 |
// private FLyrVect lyrVect;
|
34 |
private FBitSet selection = null; |
35 |
|
36 |
private void setFile(File f) |
37 |
{ |
38 |
String strFichDbf = f.getAbsolutePath().replaceAll("\\.shp", ".dbf"); |
39 |
dbfPath = strFichDbf.replaceAll("\\.SHP", ".DBF"); |
40 |
} |
41 |
|
42 |
private WritableByteChannel getWriteChannel(String path) |
43 |
throws IOException |
44 |
{ |
45 |
WritableByteChannel channel;
|
46 |
|
47 |
File f = new File(path); |
48 |
|
49 |
if (!f.exists()) {
|
50 |
System.out.println("Creando fichero " + f.getAbsolutePath()); |
51 |
|
52 |
if (!f.createNewFile()) {
|
53 |
System.err.print("Error al crear el fichero " + |
54 |
f.getAbsolutePath()); |
55 |
throw new IOException("Cannot create file " + f); |
56 |
} |
57 |
} |
58 |
|
59 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
60 |
channel = raf.getChannel(); |
61 |
|
62 |
return channel;
|
63 |
} |
64 |
|
65 |
|
66 |
|
67 |
/*public DbfWriter(File shpFile, IEditableSource ies) throws IOException, DriverException, DriverLoadException
|
68 |
{
|
69 |
SelectableDataSource sds = ies.getRecordset();
|
70 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(sds);
|
71 |
|
72 |
initialize(shpFile);
|
73 |
}
|
74 |
*/
|
75 |
|
76 |
public void initialize(File shpFile, IEditableSource ies)throws IOException, DriverException, DriverLoadException{ |
77 |
SelectableDataSource sds = ies.getRecordset(); |
78 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(sds); |
79 |
|
80 |
initialize(shpFile); |
81 |
} |
82 |
private void initialize(File shpFile) throws IOException //, FLyrVect lyrVect, FBitSet selection) throws IOException, DriverException |
83 |
{ |
84 |
setFile(shpFile); |
85 |
} |
86 |
|
87 |
public void preProcess() throws EditionException { |
88 |
// Por ahora solo escribimos los primeros bytes
|
89 |
// de las cabeceras. Luego en el postprocess los escribiremos
|
90 |
// correctamente, con el fullExtent y el numero de
|
91 |
// registros que tocan.
|
92 |
if (selection == null) |
93 |
{ |
94 |
|
95 |
try {
|
96 |
myHeader.setNumRecords(0);
|
97 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
98 |
(FileChannel) getWriteChannel(dbfPath));
|
99 |
record = new Object[myHeader.getNumFields()]; |
100 |
numRows = 0;
|
101 |
|
102 |
} catch (IOException e) { |
103 |
e.printStackTrace(); |
104 |
throw new EditionException(e); |
105 |
} |
106 |
} |
107 |
|
108 |
|
109 |
} |
110 |
|
111 |
public void process(IRowEdited row) throws EditionException { |
112 |
IRow rowEdit = (IRow) row.getLinkedRow(); |
113 |
|
114 |
try {
|
115 |
for (int i=0; i < record.length; i++) |
116 |
record[i] = rowEdit.getAttribute(i); |
117 |
dbfWrite.write(record); |
118 |
numRows++; |
119 |
|
120 |
} catch (IOException e) { |
121 |
e.printStackTrace(); |
122 |
throw new EditionException(e); |
123 |
} |
124 |
|
125 |
} |
126 |
|
127 |
public void postProcess() throws EditionException { |
128 |
try {
|
129 |
myHeader.setNumRecords(numRows); |
130 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
131 |
(FileChannel) getWriteChannel(dbfPath));
|
132 |
} catch (IOException e) { |
133 |
e.printStackTrace(); |
134 |
throw new EditionException(e); |
135 |
} |
136 |
|
137 |
|
138 |
} |
139 |
|
140 |
public String getName() { |
141 |
return "DBF Writer"; |
142 |
} |
143 |
|
144 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
145 |
return false; |
146 |
} |
147 |
|
148 |
public boolean canWriteAttribute(int sqlType) { |
149 |
switch (sqlType)
|
150 |
{ |
151 |
case Types.DOUBLE: |
152 |
case Types.FLOAT: |
153 |
case Types.INTEGER: |
154 |
case Types.BIGINT: |
155 |
return true; |
156 |
case Types.DATE: |
157 |
return true; |
158 |
case Types.BIT: |
159 |
case Types.BOOLEAN: |
160 |
return true; |
161 |
case Types.VARCHAR: |
162 |
case Types.CHAR: |
163 |
case Types.LONGVARCHAR: |
164 |
return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes |
165 |
|
166 |
} |
167 |
|
168 |
return false; |
169 |
} |
170 |
|
171 |
} |