Statistics
| Revision:

root / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeaturesWriter.java @ 20501

History | View | Annotate | Download (3.82 KB)

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