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

History | View | Annotate | Download (3.89 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.DbaseCodepage;
41
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader;
42
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter;
43

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

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

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

    
59
                String charset = storeParameters.getEffectiveEncodingName();        
60
                if (charset==null) {
61
                        // set a safe encoding in case none has been defined
62
                        charset = "UTF-8";
63
                }
64
                try {
65
                        myHeader = DbaseFileHeader.fromFeatureType(featureType, charset);
66
                } catch (DataException e1) {
67
                        throw new WriteException(this.name, e1);
68
                }
69

    
70
                dbfFile = storeParameters.getDBFFile();
71
                
72
                // .cpg will be redundant if LDID was already set, but we still
73
                // want to write it with the hopes of increasing the range of programs
74
                // that will correctly interpret the charset
75
                DbaseCodepage cpWriter = new DbaseCodepage(dbfFile);
76
                cpWriter.write(charset);
77

    
78
                dbfChannel = null;
79

    
80
                try {
81
                        dbfChannel = (FileChannel) getWriteChannel(dbfFile
82
                                        .getAbsolutePath());
83
                } catch (IOException e) {
84
                        throw new WriteException(this.name, e);
85
                }
86
                try{
87
                        this.dbfWriter = new DbaseFileWriter(myHeader, dbfChannel, true);
88
                        } catch (InitializeException e) {
89
                                throw new WriteException(this.name, e);
90
                        }
91

    
92

    
93
                this.featuretype=featureType;
94
                dbfFile = storeParameters.getDBFFile();
95

    
96
        }
97

    
98
        public void append(Feature feature) throws DataException {
99
                // TODO use FeatureProvider
100
                dbfWriter.append(feature);
101
        }
102

    
103
        public void end() throws DataException {
104
                dbfWriter.close();
105
        }
106

    
107
        public void dispose() {
108
                dbfWriter = null;
109

    
110
        }
111

    
112
        protected static WritableByteChannel getWriteChannel(String path)
113
        throws IOException {
114
                WritableByteChannel channel;
115

    
116
                File f = new File(path);
117

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

    
126
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
127
                channel = raf.getChannel();
128

    
129
                return channel;
130
        }
131

    
132
        /**
133
         * @return
134
         */
135
        protected int getRowCount() {
136
                return this.myHeader.getNumRecords();
137
        }
138

    
139

    
140
}