comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/InfoGewParser.java @ 1211:f08fe480092c

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

http://dive4elements.wald.intevation.org