root / trunk / libraries / libGeocoding / src / org / gvsig / normalization / operations / StringListNormalization.java @ 26245
History | View | Annotate | Download (6.52 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 Prodevelop S.L. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.normalization.operations; |
29 |
|
30 |
import java.io.File; |
31 |
import java.util.ArrayList; |
32 |
import java.util.List; |
33 |
|
34 |
import org.apache.log4j.Logger; |
35 |
import org.gvsig.normalization.patterns.Element; |
36 |
import org.gvsig.normalization.patterns.Patternnormalization; |
37 |
|
38 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
39 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
40 |
import com.hardcode.gdbms.engine.values.Value; |
41 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
42 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
43 |
import com.iver.cit.gvsig.fmap.core.DefaultRow; |
44 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
45 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
46 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
47 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
48 |
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter; |
49 |
|
50 |
/**
|
51 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
52 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
53 |
*/
|
54 |
|
55 |
public class StringListNormalization extends AbstractNormalization implements |
56 |
NormalizationNewTable { |
57 |
|
58 |
private static final Logger log = Logger |
59 |
.getLogger(StringListNormalization.class); |
60 |
|
61 |
@SuppressWarnings("unchecked") |
62 |
private ArrayList fileChains; |
63 |
private NormalizationTableDriver driver;
|
64 |
private File ouputFile; |
65 |
|
66 |
/**
|
67 |
* Builder available for New Table from File
|
68 |
*
|
69 |
* @param pat
|
70 |
* @param chains
|
71 |
*/
|
72 |
@SuppressWarnings("unchecked") |
73 |
public StringListNormalization(Patternnormalization pat, ArrayList chains, |
74 |
File theFile) {
|
75 |
|
76 |
super(pat);
|
77 |
this.ouputFile = theFile;
|
78 |
fileChains = chains; |
79 |
FieldDescription[] fieldDescs = new FieldDescription[numNewImportedFields()]; |
80 |
|
81 |
// Second table
|
82 |
FieldDescription auxfd = null;
|
83 |
int posi = -1; |
84 |
|
85 |
// Pattern fields
|
86 |
int contad = pattern.getElements().size();
|
87 |
for (int j = 0; j < contad; j++) { |
88 |
|
89 |
if (((Element) pattern.getElements().get(j)).getImportfield()) { |
90 |
posi++; |
91 |
auxfd = readAddressNormPattern((Element) pattern.getElements()
|
92 |
.get(j)); |
93 |
fieldDescs[posi] = auxfd; |
94 |
} |
95 |
} |
96 |
driver = new NormalizationTableDriver(fieldDescs);
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* From a file, a new table is created
|
101 |
*
|
102 |
* @return
|
103 |
*/
|
104 |
public boolean createNewTable() { |
105 |
return true; |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Fill all rows with values normalized
|
110 |
*
|
111 |
* @param row
|
112 |
* @throws ReadDriverException
|
113 |
*/
|
114 |
@SuppressWarnings("unchecked") |
115 |
public void fillRow(int row) throws ReadDriverException { |
116 |
int posi = -1; |
117 |
|
118 |
/* TABLE */
|
119 |
int nfields = nameNewFields.length;
|
120 |
|
121 |
Value[] vals = new Value[nfields]; |
122 |
|
123 |
/* CUTTING AND FILTERING */
|
124 |
String splitString = ""; |
125 |
List chains = null; |
126 |
List splittedChains = null; |
127 |
String cadena = ""; |
128 |
int tipoCampo = 0; |
129 |
|
130 |
splitString = (String) fileChains.get(row);
|
131 |
|
132 |
splittedChains = nAlgorithm.splitChain(splitString); |
133 |
nAlgorithm.setRow(row); |
134 |
chains = nAlgorithm.filterSplitChains(splittedChains); |
135 |
|
136 |
/* CREATING VALUES */
|
137 |
for (int j = 0; j < nameNewFields.length; j++) { |
138 |
posi++; |
139 |
try {
|
140 |
cadena = (String) chains.get(posNameNewFields[j]);
|
141 |
} catch (Exception e) { |
142 |
log.warn("Null chain replaced for white");
|
143 |
update("ERROR.errornullsubstring." + (row + 1)); |
144 |
cadena = "";
|
145 |
} |
146 |
cadena = cadena.trim(); |
147 |
|
148 |
tipoCampo = driver.getFieldType(posi); |
149 |
// create values
|
150 |
vals[posi] = createValue(row, tipoCampo, posi, cadena); |
151 |
} |
152 |
driver.addRow(vals); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Get the row count
|
157 |
*
|
158 |
* @return number of rows
|
159 |
*/
|
160 |
public long getRowCount() { |
161 |
try {
|
162 |
return this.driver.getRowCount(); |
163 |
} catch (ReadDriverException e) {
|
164 |
log.error("Error retrieving the number of rows of the driver");
|
165 |
return -1; |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Get the estimated row count
|
171 |
*
|
172 |
* @return number of rows
|
173 |
*/
|
174 |
public long getEstimatedRowCount() { |
175 |
return this.fileChains.size(); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Tasks necessary before normalize
|
180 |
*
|
181 |
* @return process ok
|
182 |
*/
|
183 |
public boolean preProcess() { |
184 |
update("INFO.normalizing");
|
185 |
return true; |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Tasks necessary after normalize. Write dbf file
|
190 |
*
|
191 |
* @return process ok
|
192 |
*/
|
193 |
public boolean postProcess() { |
194 |
|
195 |
update("INFO.writtingdbf");
|
196 |
DbfWriter dbf = new DbfWriter();
|
197 |
dbf.setFile(this.ouputFile);
|
198 |
TableDefinition td = new TableDefinition();
|
199 |
td.setName("secondTable");
|
200 |
td.setFieldsDesc(driver.getFieldDescriptions()); |
201 |
|
202 |
try {
|
203 |
dbf.initialize(td); |
204 |
} catch (InitializeWriterException e) {
|
205 |
log.error("ERROR initializing dbf", e);
|
206 |
return false; |
207 |
} |
208 |
try {
|
209 |
dbf.preProcess(); |
210 |
} catch (StartWriterVisitorException e1) {
|
211 |
log.error("ERROR preprocessing dbf", e1);
|
212 |
return false; |
213 |
} |
214 |
|
215 |
DefaultRowEdited dre; |
216 |
for (int i = 0; i < getRowCount(); i++) { |
217 |
try {
|
218 |
Value[] vals = driver.getValues(i);
|
219 |
DefaultRow dr = new DefaultRow(vals);
|
220 |
dre = new DefaultRowEdited(dr, IRowEdited.STATUS_ADDED, i);
|
221 |
dbf.process(dre); |
222 |
} catch (Exception ioe) { |
223 |
log.warn("ERROR traversing driver", ioe);
|
224 |
} |
225 |
} |
226 |
|
227 |
try {
|
228 |
dbf.postProcess(); |
229 |
} catch (StopWriterVisitorException e) {
|
230 |
log.error("ERROR in the postprocess", e);
|
231 |
return false; |
232 |
} |
233 |
update("INFO.endnormalizing");
|
234 |
return true; |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Get the driver
|
239 |
*
|
240 |
* @return driver
|
241 |
*/
|
242 |
public NormalizationTableDriver getDriver() {
|
243 |
return this.driver; |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Get the output file
|
248 |
*
|
249 |
* @return dbf file
|
250 |
*/
|
251 |
public File getOuputFile() { |
252 |
return this.ouputFile; |
253 |
} |
254 |
|
255 |
} |