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 @ 40435

History | View | Annotate | Download (3.61 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.dal.store.dbf;
29

    
30
import java.io.File;
31
import java.io.IOException;
32
import java.io.RandomAccessFile;
33
import java.nio.channels.FileChannel;
34
import java.nio.channels.WritableByteChannel;
35
import java.nio.charset.Charset;
36

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

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

    
54
        protected DBFFeatureWriter(String name) {
55
                this.name= name;
56
        }
57

    
58
        public void begin(DBFStoreParameters storeParameters,
59
                        FeatureType featureType, long numRows) throws DataException {
60

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

    
68
                dbfFile = storeParameters.getDBFFile();
69

    
70
                dbfChannel = null;
71

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

    
81

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

    
87

    
88
                this.featuretype=featureType;
89
                dbfFile = storeParameters.getDBFFile();
90

    
91
        }
92

    
93
        public void append(Feature feature) throws DataException {
94
                // TODO use FeatureProvider
95
                dbfWriter.append(feature);
96
        }
97

    
98
        public void end() throws DataException {
99
                dbfWriter.close();
100
        }
101

    
102
        public void dispose() {
103
                dbfWriter = null;
104

    
105
        }
106

    
107
        protected static WritableByteChannel getWriteChannel(String path)
108
        throws IOException {
109
                WritableByteChannel channel;
110

    
111
                File f = new File(path);
112

    
113
                if (!f.exists()) {
114
                        //                        System.out.println("Creando fichero " + f.getAbsolutePath());
115

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

    
123
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
124
                channel = raf.getChannel();
125

    
126
                return channel;
127
        }
128

    
129
        /**
130
         * @return
131
         */
132
        protected int getRowCount() {
133
                return this.myHeader.getNumRecords();
134
        }
135

    
136

    
137
}