Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.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 | cf8cbcb6a10d |
children | 763c4137d6e1 |
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.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 List<ImportRiver> getRivers() { | |
42 return rivers; | |
43 } | |
44 | |
45 public static final String normalize(String f) { | |
46 return f.replace("\\", "/").replace("/", File.separator); | |
47 } | |
48 | |
49 public void parse(File file) throws IOException { | |
50 | |
51 LineNumberReader in = null; | |
52 | |
53 File root = file.getParentFile(); | |
54 | |
55 ImportRiver importRiver = new ImportRiver(); | |
56 try { | |
57 in = | |
58 new LineNumberReader( | |
59 new InputStreamReader( | |
60 new FileInputStream(file), ENCODING)); | |
61 | |
62 String line = null; | |
63 | |
64 String riverName = null; | |
65 File wstFile = null; | |
66 File bbInfoFile = null; | |
67 | |
68 while ((line = in.readLine()) != null) { | |
69 if ((line = line.trim()).length() == 0) { | |
70 continue; | |
71 } | |
72 Matcher m = GEWAESSER.matcher(line); | |
73 | |
74 if (m.matches()) { | |
75 String river = m.group(1); | |
76 log.info("Found river '" + river + "'"); | |
77 if (riverName != null) { | |
78 rivers.add(new ImportRiver(riverName, wstFile, bbInfoFile)); | |
79 } | |
80 riverName = river; | |
81 wstFile = null; | |
82 bbInfoFile = null; | |
83 } | |
84 else if ((m = WST_DATEI.matcher(line)).matches()) { | |
85 String wstFilename = m.group(1); | |
86 File wst = new File(wstFilename = normalize(wstFilename)); | |
87 if (!wst.isAbsolute()) { | |
88 wst = new File(root, wstFilename); | |
89 } | |
90 wst = FileTools.repair(wst); | |
91 log.info("Found wst file '" + wst + "'"); | |
92 if (!wst.isFile() || !wst.canRead()) { | |
93 log.warn("cannot access WST file '" + wstFilename + "'"); | |
94 continue; | |
95 } | |
96 wstFile = wst; | |
97 } | |
98 else if ((m = BB_INFO.matcher(line)).matches()) { | |
99 //TODO: Make it relative to the wst file. | |
100 String bbInfo = m.group(1); | |
101 bbInfoFile = new File(normalize(bbInfo)); | |
102 } | |
103 } | |
104 if (riverName != null) { | |
105 rivers.add(new ImportRiver(riverName, wstFile, bbInfoFile)); | |
106 } | |
107 } | |
108 finally { | |
109 if (in != null) { | |
110 in.close(); | |
111 } | |
112 } | |
113 | |
114 for (ImportRiver river: rivers) { | |
115 river.parseDependencies(); | |
116 } | |
117 } | |
118 } | |
119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |