comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/InfoGewParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/InfoGewParser.java@255898799ed9
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer.parsers;
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.artifacts.common.utils.FileTools;
19
20 import de.intevation.flys.importer.ImportRiver;
21
22
23 /** Processes files mentioned in an info file for a river. */
24 public class InfoGewParser
25 {
26 private static Logger log = Logger.getLogger(InfoGewParser.class);
27
28 public static final String ENCODING = "ISO-8859-1";
29
30 public static final Pattern GEWAESSER =
31 Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)");
32
33 public static final Pattern WST_DATEI =
34 Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)");
35
36 public static final Pattern BB_INFO =
37 Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)");
38
39 protected ArrayList<ImportRiver> rivers;
40
41 protected AnnotationClassifier annotationClassifier;
42
43 public InfoGewParser() {
44 this(null);
45 }
46
47 public InfoGewParser(AnnotationClassifier annotationClassifier) {
48 rivers = new ArrayList<ImportRiver>();
49 this.annotationClassifier = annotationClassifier;
50 }
51
52 public List<ImportRiver> getRivers() {
53 return rivers;
54 }
55
56 public static final String normalize(String f) {
57 return f.replace("\\", "/").replace("/", File.separator);
58 }
59
60 /** Handle a gew, wst, or bb_info file. */
61 public void parse(File file) throws IOException {
62
63 LineNumberReader in = null;
64
65 File root = file.getParentFile();
66
67 try {
68 in =
69 new LineNumberReader(
70 new InputStreamReader(
71 new FileInputStream(file), ENCODING));
72
73 String line = null;
74
75 String riverName = null;
76 File wstFile = null;
77 File bbInfoFile = null;
78
79 while ((line = in.readLine()) != null) {
80 if ((line = line.trim()).length() == 0) {
81 continue;
82 }
83 Matcher m = GEWAESSER.matcher(line);
84
85 if (m.matches()) {
86 String river = m.group(1);
87 log.info("Found river '" + river + "'");
88 if (riverName != null) {
89 rivers.add(new ImportRiver(
90 riverName,
91 wstFile,
92 bbInfoFile,
93 annotationClassifier));
94 }
95 riverName = river;
96 wstFile = null;
97 bbInfoFile = null;
98 }
99 else if ((m = WST_DATEI.matcher(line)).matches()) {
100 String wstFilename = m.group(1);
101 File wst = new File(wstFilename = normalize(wstFilename));
102 if (!wst.isAbsolute()) {
103 wst = new File(root, wstFilename);
104 }
105 wst = FileTools.repair(wst);
106 log.info("Found wst file '" + wst + "'");
107 if (!wst.isFile() || !wst.canRead()) {
108 log.warn("cannot access WST file '" + wstFilename + "'");
109 continue;
110 }
111 wstFile = wst;
112 }
113 else if ((m = BB_INFO.matcher(line)).matches()) {
114 //TODO: Make it relative to the wst file.
115 String bbInfo = m.group(1);
116 bbInfoFile = new File(normalize(bbInfo));
117 }
118 }
119 if (riverName != null) {
120 rivers.add(new ImportRiver(
121 riverName,
122 wstFile,
123 bbInfoFile,
124 annotationClassifier));
125 }
126 }
127 finally {
128 if (in != null) {
129 in.close();
130 }
131 }
132
133 for (ImportRiver river: rivers) {
134 river.parseDependencies();
135 }
136 }
137 }
138 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org