Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java @ 508:a9c7f6ec3a5a 2.3.1
merged flys-backend/2.3.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:12 +0200 |
parents | d37ccb04ab5d |
children |
comparison
equal
deleted
inserted
replaced
462:ebf049a1eb53 | 508:a9c7f6ec3a5a |
---|---|
1 package de.intevation.flys.importer; | |
2 | |
3 import java.io.File; | |
4 | |
5 import java.util.List; | |
6 import java.util.ArrayList; | |
7 | |
8 import java.io.IOException; | |
9 import java.io.LineNumberReader; | |
10 import java.io.FileInputStream; | |
11 import java.io.InputStreamReader; | |
12 | |
13 import java.math.BigDecimal; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 | |
17 import de.intevation.flys.utils.FileTools; | |
18 | |
19 public class PegelGltParser | |
20 { | |
21 private static Logger log = Logger.getLogger(PegelGltParser.class); | |
22 | |
23 public static final String ENCODING = "ISO-8859-1"; | |
24 | |
25 public static final String KM = "km:"; | |
26 | |
27 protected List<ImportGauge> gauges; | |
28 | |
29 public PegelGltParser() { | |
30 gauges = new ArrayList<ImportGauge>(); | |
31 } | |
32 | |
33 public List<ImportGauge> getGauges() { | |
34 return gauges; | |
35 } | |
36 | |
37 public void parse(File file) throws IOException { | |
38 | |
39 File parent = file.getParentFile(); | |
40 | |
41 log.info("parsing GLT file '" + file + "'"); | |
42 LineNumberReader in = null; | |
43 try { | |
44 in = | |
45 new LineNumberReader( | |
46 new InputStreamReader( | |
47 new FileInputStream(file), ENCODING)); | |
48 | |
49 String line = null; | |
50 while ((line = in.readLine()) != null) { | |
51 if ((line = line.trim()).length() == 0) { | |
52 continue; | |
53 } | |
54 | |
55 int kmPos = line.indexOf(KM); | |
56 if (kmPos < 0) { | |
57 log.warn("no gauge found in line " + in.getLineNumber()); | |
58 continue; | |
59 } | |
60 | |
61 String gaugeName = line.substring(0, kmPos).trim(); | |
62 log.info("Found gauge '" + gaugeName + "'"); | |
63 | |
64 line = line.substring(kmPos + KM.length()).trim(); | |
65 | |
66 String [] parts = line.split("\\s+"); | |
67 if (parts.length < 4) { | |
68 log.warn("line " + in.getLineNumber() | |
69 + " has not enough columns"); | |
70 continue; | |
71 } | |
72 | |
73 BigDecimal from = new BigDecimal(parts[0].replace(",", ".")); | |
74 BigDecimal to = new BigDecimal(parts[1].replace(",", ".")); | |
75 if (from.compareTo(from) > 0) { | |
76 BigDecimal t = from; from = to; to = t; | |
77 } | |
78 ImportRange range = new ImportRange(from, to); | |
79 File staFile = FileTools.repair(new File(parent, parts[2])); | |
80 File atFile = FileTools.repair(new File(parent, parts[3])); | |
81 | |
82 if (log.isDebugEnabled()) { | |
83 log.debug("\tfrom: " + from); | |
84 log.debug("\tto: " + to); | |
85 log.debug("\tsta: " + staFile); | |
86 log.debug("\tat: " + atFile); | |
87 } | |
88 | |
89 gauges.add(new ImportGauge(range, staFile, atFile)); | |
90 } | |
91 } | |
92 finally { | |
93 if (in != null) { | |
94 in.close(); | |
95 } | |
96 } | |
97 } | |
98 } | |
99 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |