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 @ 40559
History | View | Annotate | Download (3.66 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /*
|
||
25 | * AUTHORS (In addition to CIT):
|
||
26 | * 2008 IVER T.I. S.A. {{Task}}
|
||
27 | */
|
||
28 | |||
29 | package org.gvsig.fmap.dal.store.dbf; |
||
30 | |||
31 | import java.io.File; |
||
32 | import java.io.IOException; |
||
33 | import java.io.RandomAccessFile; |
||
34 | import java.nio.channels.FileChannel; |
||
35 | import java.nio.channels.WritableByteChannel; |
||
36 | import java.nio.charset.Charset; |
||
37 | |||
38 | import org.gvsig.fmap.dal.exception.DataException; |
||
39 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
40 | import org.gvsig.fmap.dal.exception.WriteException; |
||
41 | import org.gvsig.fmap.dal.feature.Feature; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
43 | import org.gvsig.fmap.dal.feature.exception.AttributeFeatureTypeNotSuportedException; |
||
44 | import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileHeader; |
||
45 | import org.gvsig.fmap.dal.store.dbf.utils.DbaseFileWriter; |
||
46 | |||
47 | public class DBFFeatureWriter { |
||
48 | protected FeatureType featuretype;
|
||
49 | private DbaseFileWriter dbfWriter = null; |
||
50 | protected String name; |
||
51 | private File dbfFile; |
||
52 | private DbaseFileHeader myHeader;
|
||
53 | private FileChannel dbfChannel; |
||
54 | |||
55 | protected DBFFeatureWriter(String name) { |
||
56 | this.name= name;
|
||
57 | } |
||
58 | |||
59 | public void begin(DBFStoreParameters storeParameters, |
||
60 | FeatureType featureType, long numRows) throws DataException { |
||
61 | |||
62 | // TODO if is new set the langID
|
||
63 | try {
|
||
64 | myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
||
65 | } catch (AttributeFeatureTypeNotSuportedException e1) {
|
||
66 | throw new WriteException(this.name, e1); |
||
67 | } |
||
68 | |||
69 | dbfFile = storeParameters.getDBFFile(); |
||
70 | |||
71 | dbfChannel = null;
|
||
72 | |||
73 | try {
|
||
74 | dbfChannel = (FileChannel) getWriteChannel(dbfFile
|
||
75 | .getAbsolutePath()); |
||
76 | } catch (IOException e) { |
||
77 | throw new WriteException(this.name, e); |
||
78 | } |
||
79 | try{
|
||
80 | this.dbfWriter = new DbaseFileWriter(myHeader, dbfChannel, true); |
||
81 | |||
82 | |||
83 | this.dbfWriter.setCharset(Charset.forName("ISO-8859-1")); |
||
84 | } catch (InitializeException e) {
|
||
85 | throw new WriteException(this.name, e); |
||
86 | } |
||
87 | |||
88 | |||
89 | this.featuretype=featureType;
|
||
90 | dbfFile = storeParameters.getDBFFile(); |
||
91 | |||
92 | } |
||
93 | |||
94 | public void append(Feature feature) throws DataException { |
||
95 | // TODO use FeatureProvider
|
||
96 | dbfWriter.append(feature); |
||
97 | } |
||
98 | |||
99 | public void end() throws DataException { |
||
100 | dbfWriter.close(); |
||
101 | } |
||
102 | |||
103 | public void dispose() { |
||
104 | dbfWriter = null;
|
||
105 | |||
106 | } |
||
107 | |||
108 | protected static WritableByteChannel getWriteChannel(String path) |
||
109 | throws IOException { |
||
110 | WritableByteChannel channel;
|
||
111 | |||
112 | File f = new File(path); |
||
113 | |||
114 | if (!f.exists()) {
|
||
115 | // System.out.println("Creando fichero " + f.getAbsolutePath());
|
||
116 | |||
117 | if (!f.createNewFile()) {
|
||
118 | System.err.print("Error al crear el fichero " |
||
119 | + f.getAbsolutePath()); |
||
120 | throw new IOException("Cannot create file " + f); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
||
125 | channel = raf.getChannel(); |
||
126 | |||
127 | return channel;
|
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | * @return
|
||
132 | */
|
||
133 | protected int getRowCount() { |
||
134 | return this.myHeader.getNumRecords(); |
||
135 | } |
||
136 | |||
137 | |||
138 | } |