Mercurial > dive4elements > river
comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java @ 5828:dfb26b03b179
Moved directories to org.dive4elements.river
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:53:11 +0200 |
parents | flys-backend/src/main/java/de/intevation/flys/importer/parsers/WaterlevelDifferencesParser.java@b3dd14fc13a6 |
children | 18619c1e7c2a |
comparison
equal
deleted
inserted
replaced
5827:e308d4ecd35a | 5828:dfb26b03b179 |
---|---|
1 package de.intevation.flys.importer.parsers; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.math.BigDecimal; | |
6 import java.text.NumberFormat; | |
7 import java.text.ParseException; | |
8 import java.util.ArrayList; | |
9 import java.util.List; | |
10 import java.util.regex.Matcher; | |
11 import java.util.regex.Pattern; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import de.intevation.flys.importer.ImportUnit; | |
16 | |
17 import de.intevation.flys.importer.ImportWst; | |
18 import de.intevation.flys.importer.ImportWstColumn; | |
19 | |
20 | |
21 /** | |
22 * Parse WaterlevelDifferences CSV file. | |
23 */ | |
24 public class WaterlevelDifferencesParser extends LineParser { | |
25 | |
26 private static final Logger log = | |
27 Logger.getLogger(WaterlevelDifferencesParser.class); | |
28 | |
29 private static final NumberFormat nf = | |
30 NumberFormat.getInstance(DEFAULT_LOCALE); | |
31 | |
32 public static final Pattern META_UNIT = | |
33 Pattern.compile("^Einheit: \\[(.*)\\].*"); | |
34 | |
35 /** List of parsed differences as ImportWst s. */ | |
36 private List<ImportWst> differences; | |
37 | |
38 private ImportWstColumn[] columns; | |
39 | |
40 /** The currently processed dataset. */ | |
41 private ImportWst current; | |
42 | |
43 | |
44 public WaterlevelDifferencesParser() { | |
45 differences = new ArrayList<ImportWst>(); | |
46 } | |
47 | |
48 | |
49 /** Get the differences as wst parsed so far. */ | |
50 public List<ImportWst> getDifferences() { | |
51 return differences; | |
52 } | |
53 | |
54 | |
55 /** | |
56 * Parse a csv waterleveldifferenceparser and create a ImportWst object | |
57 * from it. | |
58 */ | |
59 @Override | |
60 public void parse(File file) throws IOException { | |
61 current = new ImportWst(file.getName()); | |
62 current.setKind(7); | |
63 | |
64 super.parse(file); | |
65 } | |
66 | |
67 | |
68 /** No rewind implemented. */ | |
69 @Override | |
70 protected void reset() { | |
71 } | |
72 | |
73 | |
74 @Override | |
75 protected void finish() { | |
76 if (columns != null && current != null) { | |
77 // TODO figure out if its needed, as the columns | |
78 // are registered at their construction time. | |
79 for (ImportWstColumn col: columns) { | |
80 // TODO place a current.addColumn(col); here? | |
81 } | |
82 | |
83 differences.add(current); | |
84 } | |
85 | |
86 current = null; | |
87 columns = null; | |
88 } | |
89 | |
90 | |
91 @Override | |
92 protected void handleLine(int lineNum, String line) { | |
93 if (line.startsWith(START_META_CHAR)) { | |
94 handleMetaLine(stripMetaLine(line)); | |
95 } | |
96 else { | |
97 handleDataLine(line); | |
98 } | |
99 } | |
100 | |
101 | |
102 private void handleMetaLine(String meta) { | |
103 if (handleMetaUnit(meta)) { | |
104 return; | |
105 } | |
106 else { | |
107 handleMetaColumnNames(meta); | |
108 } | |
109 } | |
110 | |
111 | |
112 private boolean handleMetaUnit(String meta) { | |
113 Matcher m = META_UNIT.matcher(meta); | |
114 | |
115 if (m.matches()) { | |
116 String unit = m.group(1); | |
117 log.debug("Found unit: '" + unit + "'"); | |
118 | |
119 current.setUnit(new ImportUnit(unit)); | |
120 | |
121 return true; | |
122 } | |
123 | |
124 return false; | |
125 } | |
126 | |
127 | |
128 private boolean handleMetaColumnNames(String meta) { | |
129 Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)"); | |
130 Matcher m = META_COLUMN_NAMES.matcher(meta); | |
131 | |
132 if (m.matches()) { | |
133 String colStr = m.group(1); | |
134 String[] cols = colStr.split(SEPERATOR_CHAR); | |
135 | |
136 log.debug("Found " + cols.length + " columns."); | |
137 | |
138 initColumns(cols); | |
139 | |
140 return true; | |
141 } | |
142 | |
143 return false; | |
144 } | |
145 | |
146 | |
147 private void initColumns(String[] cols) { | |
148 current.setNumberColumns(cols.length); | |
149 columns = current.getColumns().toArray(new ImportWstColumn[cols.length]); | |
150 | |
151 for (int i = 0; i < cols.length; i++) { | |
152 String name = cols[i].replace("\"", ""); | |
153 | |
154 log.debug("Create new column '" + name + "'"); | |
155 current.getColumn(i).setName(name); | |
156 current.getColumn(i).setDescription(name); | |
157 } | |
158 } | |
159 | |
160 | |
161 private void handleDataLine(String line) { | |
162 String[] cols = line.split(SEPERATOR_CHAR); | |
163 | |
164 if (cols == null || cols.length < 2) { | |
165 log.warn("skip invalid waterlevel-diff line: '" + line + "'"); | |
166 return; | |
167 } | |
168 | |
169 try { | |
170 Double station = nf.parse(cols[0]).doubleValue(); | |
171 | |
172 for (int i = 0; i < columns.length; i++) { | |
173 int idx = i+1; | |
174 | |
175 if (idx >= cols.length) { | |
176 log.warn("Insufficient column numbers: " + line); | |
177 continue; | |
178 } | |
179 | |
180 String value = cols[idx]; | |
181 | |
182 try { | |
183 columns[i].addColumnValue( | |
184 new BigDecimal(station), | |
185 new BigDecimal(nf.parse(value).doubleValue())); | |
186 } | |
187 catch (ParseException pe) { | |
188 log.warn("Could not parse value: '" + value + "'"); | |
189 } | |
190 } | |
191 } | |
192 catch (ParseException pe) { | |
193 log.warn("Could not parse station: '" + line + "'"); | |
194 } | |
195 } | |
196 } | |
197 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |