svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFResource.java @ 20626
History | View | Annotate | Download (6.22 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 {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.data.datastores.vectorial.file.dbf; |
32 |
|
33 |
import java.io.File; |
34 |
import java.nio.channels.FileChannel; |
35 |
import java.util.Date; |
36 |
|
37 |
import org.gvsig.data.Resource; |
38 |
import org.gvsig.data.datastores.vectorial.file.IFileResource; |
39 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile; |
40 |
import org.gvsig.data.exception.CloseException; |
41 |
import org.gvsig.data.exception.DataException; |
42 |
import org.gvsig.data.exception.OpenException; |
43 |
import org.gvsig.data.exception.ReadException; |
44 |
import org.gvsig.data.exception.UnsupportedEncodingException; |
45 |
import org.gvsig.data.exception.WriteException; |
46 |
|
47 |
|
48 |
/**
|
49 |
* @author jmvivo
|
50 |
*
|
51 |
*/
|
52 |
public class DBFResource extends Resource implements IFileResource { |
53 |
|
54 |
protected DbaseFile dbfFile;
|
55 |
protected File file; |
56 |
private boolean isOpen; |
57 |
private long fileLastModified = 0L; |
58 |
|
59 |
DBFResource (File aFile){
|
60 |
this.file = aFile;
|
61 |
} |
62 |
|
63 |
DbaseFile getDbf(){ |
64 |
return this.dbfFile; |
65 |
} |
66 |
|
67 |
/* (non-Javadoc)
|
68 |
* @see org.gvsig.data.Resource#getKey()
|
69 |
*/
|
70 |
public String getKey() { |
71 |
return "DBF:"+this.file.getAbsolutePath(); |
72 |
} |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @see org.gvsig.data.datastores.vectorial.file.IFileResource#getFile()
|
76 |
*/
|
77 |
public File getFile() { |
78 |
return this.file; |
79 |
} |
80 |
|
81 |
/* (non-Javadoc)
|
82 |
* @see org.gvsig.data.Resource#description()
|
83 |
*/
|
84 |
|
85 |
public String description() { |
86 |
if (this.getFile()!= null){ |
87 |
return "DBF File resource: " + this.getFile().getAbsolutePath(); |
88 |
} else{
|
89 |
return "DBF File resource: {No file}"; |
90 |
} |
91 |
} |
92 |
|
93 |
/* (non-Javadoc)
|
94 |
* @see org.gvsig.data.Resource#doClose()
|
95 |
*/
|
96 |
protected boolean doClose() throws CloseException{ |
97 |
dbfFile.close(); |
98 |
isOpen = false;
|
99 |
return true; |
100 |
} |
101 |
|
102 |
/* (non-Javadoc)
|
103 |
* @see org.gvsig.data.Resource#doDispose()
|
104 |
*/
|
105 |
protected void doDispose() throws DataException{ |
106 |
this.dbfFile = null; |
107 |
this.file = null; |
108 |
|
109 |
} |
110 |
|
111 |
/* (non-Javadoc)
|
112 |
* @see org.gvsig.data.Resource#doOpen()
|
113 |
*/
|
114 |
protected boolean doOpen() throws OpenException{ |
115 |
if (this.dbfFile == null){ |
116 |
this.dbfFile = new DbaseFile(this.file); |
117 |
this.fileLastModified = this.file.lastModified(); |
118 |
} else{
|
119 |
if (this.fileLastModified < this.file.lastModified()) { |
120 |
this.changed();
|
121 |
} |
122 |
} |
123 |
this.dbfFile.open();
|
124 |
this.isOpen=true; |
125 |
return true; |
126 |
|
127 |
} |
128 |
|
129 |
/* (non-Javadoc)
|
130 |
* @see org.gvsig.data.Resource#isOpen()
|
131 |
*/
|
132 |
public boolean isOpen() { |
133 |
return this.isOpen; |
134 |
} |
135 |
|
136 |
private void checkOpen() throws OpenException{ |
137 |
if (!this.isOpen){ |
138 |
this.open();
|
139 |
} |
140 |
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* @param rowIndex
|
145 |
* @param fieldId
|
146 |
* @return
|
147 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getBooleanFieldValue(int, int)
|
148 |
*/
|
149 |
public boolean getBooleanFieldValue(int rowIndex, int fieldId) throws ReadException{ |
150 |
this.checkOpen();
|
151 |
return dbfFile.getBooleanFieldValue(rowIndex, fieldId);
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* @return
|
156 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getFieldCount()
|
157 |
*/
|
158 |
public int getFieldCount() throws ReadException { |
159 |
this.checkOpen();
|
160 |
return dbfFile.getFieldCount();
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @param inIndex
|
165 |
* @return
|
166 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getFieldDecimalLength(int)
|
167 |
*/
|
168 |
public int getFieldDecimalLength(int inIndex) throws ReadException { |
169 |
this.checkOpen();
|
170 |
return dbfFile.getFieldDecimalLength(inIndex);
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* @param inIndex
|
175 |
* @return
|
176 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getFieldLength(int)
|
177 |
*/
|
178 |
public int getFieldLength(int inIndex) throws ReadException{ |
179 |
this.checkOpen();
|
180 |
return dbfFile.getFieldLength(inIndex);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* @param inIndex
|
185 |
* @return
|
186 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getFieldName(int)
|
187 |
*/
|
188 |
public String getFieldName(int inIndex) throws ReadException{ |
189 |
this.checkOpen();
|
190 |
return dbfFile.getFieldName(inIndex);
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* @param inIndex
|
195 |
* @return
|
196 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getFieldType(int)
|
197 |
*/
|
198 |
public char getFieldType(int inIndex) throws ReadException{ |
199 |
this.checkOpen();
|
200 |
return dbfFile.getFieldType(inIndex);
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* @return
|
205 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getRecordCount()
|
206 |
*/
|
207 |
public int getRecordCount() throws ReadException{ |
208 |
this.checkOpen();
|
209 |
return dbfFile.getRecordCount();
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* @param rowIndex
|
214 |
* @param fieldId
|
215 |
* @return
|
216 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getStringFieldValue(int, int)
|
217 |
*/
|
218 |
public String getStringFieldValue(int rowIndex, int fieldId) throws ReadException{ |
219 |
this.checkOpen();
|
220 |
return dbfFile.getStringFieldValue(rowIndex, fieldId);
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* @return
|
225 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#getWriteChannel()
|
226 |
*/
|
227 |
public FileChannel getWriteChannel() throws DataException{ |
228 |
this.checkOpen();
|
229 |
return dbfFile.getWriteChannel();
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* @param rowIndex
|
234 |
* @param fieldId
|
235 |
* @param obj
|
236 |
* @throws UnsupportedEncodingException
|
237 |
* @throws WriteException
|
238 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile#setFieldValue(int, int, java.lang.Object)
|
239 |
*/
|
240 |
public void setFieldValue(int rowIndex, int fieldId, Object obj) throws UnsupportedEncodingException, WriteException{ |
241 |
// FIXME: this.checkOpen();?????
|
242 |
dbfFile.setFieldValue(rowIndex, fieldId, obj); |
243 |
} |
244 |
|
245 |
/* (non-Javadoc)
|
246 |
* @see org.gvsig.data.Resource#doChanged()
|
247 |
*/
|
248 |
protected void doChanged() { |
249 |
this.fileLastModified = this.file.lastModified(); |
250 |
|
251 |
} |
252 |
|
253 |
|
254 |
} |
255 |
|