comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/StaFileParser.java @ 1211:f08fe480092c

Moved file parsers to separate package. flys-backend/trunk@2337 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jul 2011 13:07:45 +0000
parents
children fe89d6cf55fb
comparison
equal deleted inserted replaced
1210:31d8638760b1 1211:f08fe480092c
1 package de.intevation.flys.importer.parsers;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.LineNumberReader;
6 import java.io.FileInputStream;
7 import java.io.InputStreamReader;
8
9 import java.math.BigDecimal;
10
11 import java.util.regex.Pattern;
12 import java.util.regex.Matcher;
13
14 import java.util.HashMap;
15 import java.util.ArrayList;
16
17 import org.apache.log4j.Logger;
18
19 import de.intevation.flys.importer.ImportMainValueType;
20 import de.intevation.flys.importer.ImportMainValue;
21 import de.intevation.flys.importer.ImportNamedMainValue;
22 import de.intevation.flys.importer.ImportGauge;
23
24 public class StaFileParser
25 {
26 private static Logger log = Logger.getLogger(StaFileParser.class);
27
28 public static final String ENCODING = "ISO-8859-1";
29
30 public static final String TYPES =
31 System.getProperty("flys.backend.main.value.types", "QWTD");
32
33 public static final Pattern QWTD_ =
34 Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([" +
35 Pattern.quote(TYPES) + "]).*");
36
37 public StaFileParser() {
38 }
39
40 public boolean parse(ImportGauge gauge) throws IOException {
41
42 File file = gauge.getStaFile();
43
44 log.info("parsing STA file: " + file);
45 LineNumberReader in = null;
46 try {
47 in =
48 new LineNumberReader(
49 new InputStreamReader(
50 new FileInputStream(file), ENCODING));
51
52 String line = in.readLine();
53
54 if (line == null) {
55 log.warn("STA file is empty.");
56 return false;
57 }
58
59 if (line.length() < 37) {
60 log.warn("first line in STA file is too short.");
61 return false;
62 }
63
64 gauge.setName(line.substring(16, 37).trim());
65
66 String [] values = line.substring(38).trim().split("\\s+", 2);
67
68 if (values.length < 2) {
69 log.warn("Not enough columns for aeo and datum");
70 }
71 try {
72 gauge.setAeo(new BigDecimal(values[0].replace(",", ".")));
73 gauge.setDatum(new BigDecimal(values[1].replace(",", ".")));
74 }
75 catch (NumberFormatException nfe) {
76 log.warn("cannot parse aeo or datum");
77 return false;
78 }
79
80 line = in.readLine();
81
82 if (line == null) {
83 log.warn("STA file has not enough lines");
84 return false;
85 }
86
87 if (line.length() < 36) {
88 log.warn("second line is too short");
89 return false;
90 }
91
92 try {
93 gauge.setStation(
94 new BigDecimal(line.substring(29, 36).trim()));
95 }
96 catch (NumberFormatException nfe) {
97 log.warn("parsing of the datum of the gauge failed");
98 return false;
99 }
100
101 // overread the next six lines
102 for (int i = 0; i < 6; ++i) {
103 if ((line = in.readLine()) == null) {
104 log.warn("STA file is too short");
105 return false;
106 }
107 }
108
109 HashMap<String, ImportMainValueType> types =
110 new HashMap<String, ImportMainValueType>();
111
112 ArrayList<ImportNamedMainValue> namedMainValues =
113 new ArrayList<ImportNamedMainValue>();
114
115 ArrayList<ImportMainValue> mainValues =
116 new ArrayList<ImportMainValue>();
117
118 while ((line = in.readLine()) != null) {
119 Matcher m = QWTD_.matcher(line);
120 if (m.matches()) {
121 BigDecimal value;
122 try {
123 value = new BigDecimal(m.group(2).replace(",", "."));
124 }
125 catch (NumberFormatException nfe) {
126 log.warn("value not parseable in line "
127 + in.getLineNumber());
128 continue;
129 }
130 String typeString = m.group(3);
131 log.debug("\t type: " + typeString);
132 ImportMainValueType type = types.get(typeString);
133 if (type == null) {
134 type = new ImportMainValueType(typeString);
135 types.put(typeString, type);
136 }
137 String name = m.group(1);
138 ImportNamedMainValue namedMainValue =
139 new ImportNamedMainValue(type, name);
140 namedMainValues.add(namedMainValue);
141
142 ImportMainValue mainValue =
143 new ImportMainValue(gauge, namedMainValue, value);
144
145 mainValues.add(mainValue);
146 }
147 else {
148 // TODO: treat as a comment
149 }
150 }
151 gauge.setMainValueTypes(
152 new ArrayList<ImportMainValueType>(types.values()));
153 gauge.setNamedMainValues(namedMainValues);
154 gauge.setMainValues(mainValues);
155 }
156 finally {
157 if (in != null) {
158 in.close();
159 }
160 }
161 log.info("finished parsing STA file: " + file);
162 return true;
163 }
164 }
165 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org