Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / DBFFeatureWriter.java @ 40596

History | View | Annotate | Download (3.59 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.store.dbf;
26

    
27
import java.io.File;
28
import java.io.IOException;
29
import java.io.RandomAccessFile;
30
import java.nio.channels.FileChannel;
31
import java.nio.channels.WritableByteChannel;
32
import java.nio.charset.Charset;
33

    
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.InitializeException;
36
import org.gvsig.fmap.dal.exception.WriteException;
37
import org.gvsig.fmap.dal.feature.Feature;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeNotSuportedException;
40
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader;
41
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter;
42

    
43
public class DBFFeatureWriter {
44
        protected FeatureType featuretype;
45
        private DbaseFileWriter dbfWriter = null;
46
        protected String name;
47
        private File dbfFile;
48
        private DbaseFileHeader myHeader;
49
        private FileChannel dbfChannel;
50

    
51
        protected DBFFeatureWriter(String name) {
52
                this.name= name;
53
        }
54

    
55
        public void begin(DBFStoreParameters storeParameters,
56
                        FeatureType featureType, long numRows) throws DataException {
57

    
58
                // TODO if is new set the langID
59
                try {
60
                        myHeader = DbaseFileHeader.createDbaseHeader(featureType);
61
                } catch (AttributeFeatureTypeNotSuportedException e1) {
62
                        throw new WriteException(this.name, e1);
63
                }
64

    
65
                dbfFile = storeParameters.getDBFFile();
66

    
67
                dbfChannel = null;
68

    
69
                try {
70
                        dbfChannel = (FileChannel) getWriteChannel(dbfFile
71
                                        .getAbsolutePath());
72
                } catch (IOException e) {
73
                        throw new WriteException(this.name, e);
74
                }
75
                try{
76
                        this.dbfWriter = new DbaseFileWriter(myHeader, dbfChannel, true);
77

    
78

    
79
                        this.dbfWriter.setCharset(Charset.forName("ISO-8859-1"));
80
                        } catch (InitializeException e) {
81
                                throw new WriteException(this.name, e);
82
                        }
83

    
84

    
85
                this.featuretype=featureType;
86
                dbfFile = storeParameters.getDBFFile();
87

    
88
        }
89

    
90
        public void append(Feature feature) throws DataException {
91
                // TODO use FeatureProvider
92
                dbfWriter.append(feature);
93
        }
94

    
95
        public void end() throws DataException {
96
                dbfWriter.close();
97
        }
98

    
99
        public void dispose() {
100
                dbfWriter = null;
101

    
102
        }
103

    
104
        protected static WritableByteChannel getWriteChannel(String path)
105
        throws IOException {
106
                WritableByteChannel channel;
107

    
108
                File f = new File(path);
109

    
110
                if (!f.exists()) {
111
                        //                        System.out.println("Creando fichero " + f.getAbsolutePath());
112

    
113
                        if (!f.createNewFile()) {
114
                                System.err.print("Error al crear el fichero "
115
                                                + f.getAbsolutePath());
116
                                throw new IOException("Cannot create file " + f);
117
                        }
118
                }
119

    
120
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
121
                channel = raf.getChannel();
122

    
123
                return channel;
124
        }
125

    
126
        /**
127
         * @return
128
         */
129
        protected int getRowCount() {
130
                return this.myHeader.getNumRecords();
131
        }
132

    
133

    
134
}