comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java @ 1215:8aef353e54fb

Initial version of the HYK parser. Not ready, yet. flys-backend/trunk@2341 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jul 2011 16:57:13 +0000
parents
children f8b5c37f15e4
comparison
equal deleted inserted replaced
1214:32ee9babe42c 1215:8aef353e54fb
1 package de.intevation.flys.importer.parsers;
2
3 import de.intevation.flys.importer.ImportHYKFlowZoneType;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.FileInputStream;
8 import java.io.InputStreamReader;
9 import java.io.LineNumberReader;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import java.math.BigDecimal;
15
16 import org.apache.log4j.Logger;
17
18 public class HYKParser
19 {
20 private static Logger log = Logger.getLogger(HYKParser.class);
21
22 public static enum State {
23 LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6
24 };
25
26 private static final String ENCODING = "ISO-8859-1";
27
28 protected Map<String, ImportHYKFlowZoneType> flowZoneTypes;
29
30 public HYKParser() {
31 flowZoneTypes = new HashMap<String, ImportHYKFlowZoneType>();
32 }
33
34 public boolean parse(File file) {
35
36 log.info("Parsing HYK file '" + file + "'");
37
38 LineNumberReader in = null;
39
40 try {
41 in =
42 new LineNumberReader(
43 new InputStreamReader(
44 new FileInputStream(file), ENCODING));
45
46 String line;
47
48 State state = State.LINE_1;
49
50 int numFormations = 0;
51
52 BigDecimal km = null;
53 BigDecimal top = null;
54 BigDecimal bottom = null;
55 BigDecimal distanceVL = null;
56 BigDecimal distanceHF = null;
57 BigDecimal distanceVR = null;
58
59 Integer year = null;
60 int numZones = 0;
61
62 ImportHYKFlowZoneType [] fzts = null;
63 BigDecimal [] coords = null;
64 int coordPos = 0;
65
66 while ((line = in.readLine()) != null) {
67 if ((line = line.trim()).length() == 0) {
68 continue;
69 }
70 if (line.startsWith("*")) {
71 continue;
72 }
73 String [] parts = line.split("\\s+");
74
75 switch (state) {
76 case LINE_1:
77 if (parts.length < 2) {
78 log.error("not enough elements in line " +
79 in.getLineNumber());
80 return false;
81 }
82
83 if (parts.length == 2) {
84 // no year given
85 year = null;
86 }
87 else {
88 try {
89 year = Integer.valueOf(parts[1]);
90 }
91 catch (NumberFormatException nfe) {
92 log.error(
93 "year is not an integer in line " +
94 in.getLineNumber());
95 return false;
96 }
97 }
98 try {
99 km = new BigDecimal(parts[0]);
100 numFormations = Integer.parseInt(
101 parts[parts.length > 2 ? 2 : 1]);
102 }
103 catch (NumberFormatException nfe) {
104 log.error(
105 "parsing number of formations " +
106 "or km failed in line " + in.getLineNumber());
107 return false;
108 }
109 // TODO: Store HYKEntry
110
111 state = State.LINE_2;
112 break;
113
114 case LINE_2:
115 if (parts.length < 3) {
116 log.error("not enough elements in line " +
117 in.getLineNumber());
118 return false;
119 }
120 try {
121 numZones = Integer.parseInt(parts[0]);
122 bottom = new BigDecimal(parts[1]);
123 top = new BigDecimal(parts[2]);
124 }
125 catch (NumberFormatException nfe) {
126 log.error(
127 "parsing num zones, bottom or top height " +
128 "failed in line " + in.getLineNumber());
129 return false;
130 }
131 state = State.LINE_3;
132 break;
133
134 case LINE_3:
135 if (parts.length != numZones) {
136 log.error(
137 "number of flow zones mismatches " +
138 "in line " + in.getLineNumber());
139 return false;
140 }
141
142 fzts = new ImportHYKFlowZoneType[parts.length];
143 for (int i = 0; i < fzts.length; ++i) {
144 fzts[i] = getFlowZoneType(parts[i]);
145 }
146 state = State.LINE_4;
147 break;
148
149 case LINE_4:
150 try {
151 int N = Math.min(parts.length, coords.length);
152 for (coordPos = 0; coordPos < N; ++coordPos) {
153 coords[coordPos] =
154 new BigDecimal(parts[coordPos]);
155 }
156 }
157 catch (NumberFormatException nfe) {
158 log.error("cannot parse number in line " +
159 in.getLineNumber());
160 return false;
161 }
162 state = State.LINE_5;
163 break;
164
165 case LINE_5:
166 if (parts.length + coordPos < coords.length) {
167 log.error("not enough elements in line " +
168 in.getLineNumber());
169 return false;
170 }
171 try {
172 for (int i = 0;
173 i < parts.length && coordPos < coords.length;
174 ++i, ++coordPos
175 ) {
176 coords[coordPos] = new BigDecimal(parts[i]);
177 }
178 }
179 catch (NumberFormatException nfe) {
180 log.error("cannot parse number in line " +
181 in.getLineNumber());
182 return false;
183 }
184 state = State.LINE_6;
185 break;
186
187 case LINE_6:
188 if (parts.length < 3) {
189 log.error("not enough elements in line " +
190 in.getLineNumber());
191 return false;
192 }
193 try {
194 distanceVL = new BigDecimal(parts[0]);
195 distanceHF = new BigDecimal(parts[1]);
196 distanceVL = new BigDecimal(parts[2]);
197 }
198 catch (NumberFormatException nfe) {
199 log.error("cannot parse number in line " +
200 in.getLineNumber());
201 return false;
202 }
203 // TODO: now we have enough information
204 // to store a formation.
205
206 // continue with next formation.
207 state = --numFormations > 0 // formations left?
208 ? State.LINE_2
209 : State.LINE_1;
210 break;
211 }
212 }
213 }
214 catch (IOException ioe) {
215 log.error(ioe);
216 return false;
217 }
218 finally {
219 if (in != null) {
220 try {
221 in.close();
222 }
223 catch (IOException ioe) {
224 log.error(ioe);
225 }
226 }
227 }
228 return true;
229 }
230
231 protected ImportHYKFlowZoneType getFlowZoneType(String name) {
232 name = name.toUpperCase();
233 ImportHYKFlowZoneType fzt = flowZoneTypes.get(name);
234 if (fzt == null) {
235 fzt = new ImportHYKFlowZoneType(name);
236 flowZoneTypes.put(name, fzt);
237 }
238 return fzt;
239 }
240 }
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org