comparison flys-backend/src/main/java/de/intevation/flys/importer/StaFileParser.java @ 189:bc3747a371cc

First part of parsing main values. flys-backend/trunk@1520 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 18 Mar 2011 16:12:24 +0000
parents
children f1fce41347ea
comparison
equal deleted inserted replaced
188:003ac16812dd 189:bc3747a371cc
1 package de.intevation.flys.importer;
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
16 import org.apache.log4j.Logger;
17
18 public class StaFileParser
19 {
20 private static Logger log = Logger.getLogger(StaFileParser.class);
21
22 public static final String ENCODING = "ISO-8859-1";
23
24 public static final Pattern QWTD_ =
25 Pattern.compile("\\s*([^\\s]+)\\s+([^\\s]+)\\s+([QWTD-]).*");
26
27 public StaFileParser() {
28 }
29
30 public boolean parse(ImportGauge gauge) throws IOException {
31
32 File file = gauge.getStaFile();
33
34 log.info("parsing STA file: " + file);
35 LineNumberReader in = null;
36 try {
37 in =
38 new LineNumberReader(
39 new InputStreamReader(
40 new FileInputStream(file), ENCODING));
41
42 String line = in.readLine();
43
44 if (line == null) {
45 log.warn("STA file is empty.");
46 return false;
47 }
48
49 if (line.length() < 37) {
50 log.warn("first line in STA file is too short.");
51 return false;
52 }
53
54 gauge.setName(line.substring(16, 37).trim());
55
56 String [] values = line.substring(38).trim().split("\\s+", 2);
57
58 if (values.length < 2) {
59 log.warn("Not enough columns for aeo and datum");
60 }
61 try {
62 gauge.setAeo(new BigDecimal(values[0].replace(",", ".")));
63 gauge.setDatum(new BigDecimal(values[1].replace(",", ".")));
64 }
65 catch (NumberFormatException nfe) {
66 log.warn("cannot parse aeo or datum");
67 return false;
68 }
69
70 line = in.readLine();
71
72 if (line == null) {
73 log.warn("STA file has not enough lines");
74 return false;
75 }
76
77 if (line.length() < 36) {
78 log.warn("second line is too short");
79 return false;
80 }
81
82 try {
83 gauge.setDatum(
84 new BigDecimal(line.substring(29, 36).trim()));
85 }
86 catch (NumberFormatException nfe) {
87 log.warn("parsing of the datum of the gauge failed");
88 return false;
89 }
90
91 // overread the next six lines
92 for (int i = 0; i < 6; ++i) {
93 if ((line = in.readLine()) == null) {
94 log.warn("STA file is too short");
95 return false;
96 }
97 }
98
99 HashMap<String, ImportMainValueType> types =
100 new HashMap<String, ImportMainValueType>();
101
102 while ((line = in.readLine()) != null) {
103 Matcher m = QWTD_.matcher(line);
104 if (m.matches()) {
105 BigDecimal value;
106 try {
107 value = new BigDecimal(m.group(2).replace(",", "."));
108 }
109 catch (NumberFormatException nfe) {
110 log.warn("value not parseable in line "
111 + in.getLineNumber());
112 continue;
113 }
114 String typeString = m.group(3);
115 log.debug("\t type: " + typeString);
116 ImportMainValueType type = types.get(typeString);
117 if (type == null) {
118 type = new ImportMainValueType(typeString);
119 types.put(typeString, type);
120 }
121 }
122 else {
123 // TODO: treat as a comment
124 }
125 }
126 }
127 finally {
128 if (in != null) {
129 in.close();
130 }
131 }
132 log.info("finished parsing STA file: " + file);
133 return true;
134 }
135 }
136 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org