Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java @ 177:31895d24387e
Importer: Added info gew parser.
flys-backend/trunk@1485 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 16 Mar 2011 17:01:55 +0000 |
parents | |
children | 3908bfb2aa43 |
comparison
equal
deleted
inserted
replaced
176:3035d861a576 | 177:31895d24387e |
---|---|
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.util.regex.Pattern; | |
9 import java.util.regex.Matcher; | |
10 | |
11 import java.io.IOException; | |
12 import java.io.LineNumberReader; | |
13 import java.io.FileInputStream; | |
14 import java.io.InputStreamReader; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 import de.intevation.flys.utils.FileTools; | |
19 | |
20 public class InfoGewParser | |
21 { | |
22 private static Logger log = Logger.getLogger(InfoGewParser.class); | |
23 | |
24 public static final String ENCODING = "ISO-8859-1"; | |
25 | |
26 public static final Pattern GEWAESSER = | |
27 Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)"); | |
28 | |
29 public static final Pattern WST_DATEI = | |
30 Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)"); | |
31 | |
32 public static final Pattern BB_INFO = | |
33 Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)"); | |
34 | |
35 protected ArrayList<ImportRiver> rivers; | |
36 | |
37 public InfoGewParser() { | |
38 rivers = new ArrayList<ImportRiver>(); | |
39 } | |
40 | |
41 public static final String normalize(String f) { | |
42 return f.replace("\\", "/").replace("/", File.separator); | |
43 } | |
44 | |
45 public void parse(File file) throws IOException { | |
46 | |
47 LineNumberReader in = null; | |
48 | |
49 File root = file.getParentFile(); | |
50 | |
51 ImportRiver importRiver = new ImportRiver(); | |
52 try { | |
53 in = | |
54 new LineNumberReader( | |
55 new InputStreamReader( | |
56 new FileInputStream(file), ENCODING)); | |
57 | |
58 String line = null; | |
59 | |
60 String riverName = null; | |
61 File wstFile = null; | |
62 File bbInfoFile = null; | |
63 | |
64 while ((line = in.readLine()) != null) { | |
65 if ((line = line.trim()).length() == 0) { | |
66 continue; | |
67 } | |
68 Matcher m = GEWAESSER.matcher(line); | |
69 | |
70 if (m.matches()) { | |
71 String river = m.group(1); | |
72 log.info("Found river '" + river + "'"); | |
73 if (riverName != null) { | |
74 rivers.add(new ImportRiver(riverName, wstFile, bbInfoFile)); | |
75 } | |
76 riverName = river; | |
77 wstFile = null; | |
78 bbInfoFile = null; | |
79 } | |
80 else if ((m = WST_DATEI.matcher(line)).matches()) { | |
81 String wstFilename = m.group(1); | |
82 File wst = new File(wstFilename = normalize(wstFilename)); | |
83 if (!wst.isAbsolute()) { | |
84 wst = new File(root, wstFilename); | |
85 } | |
86 wst = FileTools.repair(wst); | |
87 log.info("Found wst file '" + wst + "'"); | |
88 if (!wst.isFile() || !wst.canRead()) { | |
89 log.warn("cannot access WST file '" + wstFilename + "'"); | |
90 continue; | |
91 } | |
92 wstFile = wst; | |
93 } | |
94 else if ((m = BB_INFO.matcher(line)).matches()) { | |
95 //TODO: Make it relative to the wst file. | |
96 String bbInfo = m.group(1); | |
97 bbInfoFile = new File(normalize(bbInfo)); | |
98 } | |
99 } | |
100 if (riverName != null) { | |
101 rivers.add(new ImportRiver(riverName, wstFile, bbInfoFile)); | |
102 } | |
103 } | |
104 finally { | |
105 if (in != null) { | |
106 in.close(); | |
107 } | |
108 } | |
109 } | |
110 } | |
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |