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

History | View | Annotate | Download (4.72 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.util.Objects;
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.store.dbf.utils.DbaseCodepage;
40
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader;
41
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
import org.gvsig.fmap.dal.store.dbf.utils.LogUtils;
45

    
46
public class DBFFeatureWriter {
47
        private static final Logger LOGGER = LoggerFactory.getLogger(DBFFeatureWriter.class);
48
        
49
        protected FeatureType featuretype;
50
        private DbaseFileWriter dbfWriter = null;
51
        protected String name;
52
        private File dbfFile;
53
        private DbaseFileHeader myHeader;
54
        private FileChannel dbfChannel;
55

    
56
        protected DBFFeatureWriter(String name) {
57
                this.name= name;
58
        }
59

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

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

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

    
82
                dbfChannel = null;
83

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

    
96

    
97
                this.featuretype=featureType;
98
                dbfFile = storeParameters.getDBFFile();
99

    
100
        }
101

    
102
        public void append(Feature feature) throws DataException {
103
            if(feature.isBroken()){
104
                throw new IllegalStateException("The feature '"+Objects.toString(feature.getReference())+"' is broken.");
105
            }
106
            // TODO use FeatureProvider
107
            dbfWriter.append(feature);
108
        }
109

    
110
        public void end() throws DataException {
111
                dbfWriter.close();
112
        }
113

    
114
        public void dispose() {
115
                dbfWriter = null;
116

    
117
        }
118

    
119
        protected static WritableByteChannel getWriteChannel(String path)
120
        throws IOException {
121
                WritableByteChannel channel;
122

    
123
                File f = new File(path);
124
                LogUtils.log(DBFFeatureWriter.class, "begin getWriteChannel");
125
                LogUtils.log(DBFFeatureWriter.class, "exists("+f.getAbsolutePath()+")");
126
                if (!f.exists()) {
127
                        LogUtils.log(DBFFeatureWriter.class, "createNewFile("+f.getAbsolutePath()+")");
128
                        if (!f.createNewFile()) {
129
                            LOGGER.warn("Error al crear el fichero "+ f.getAbsolutePath());
130
                            throw new IOException("Cannot create file " + f);
131
                        }
132
                }
133
                LogUtils.log(DBFFeatureWriter.class, "RandomAccessFile('"+f.getAbsolutePath()+"', \"rw\")");
134
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
135
                LogUtils.log(DBFFeatureWriter.class, "getChanel()");
136
                channel = raf.getChannel();
137
                LogUtils.log(DBFFeatureWriter.class, "end getWriteChannel");
138
                return channel;
139
        }
140

    
141
        protected int getRowCount() {
142
                return this.myHeader.getNumRecords();
143
        }
144
}