comparison backend/src/main/java/org/dive4elements/river/importer/parsers/InfoGewParser.java @ 7730:e1b831fe435a slt-simplify-cross-sections

Merged default into slt-simplify-cross-sections branch and updated package and class names.
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Jan 2014 14:04:20 +0100
parents 4c3ccf2b0304
children 24408bce2fdb
comparison
equal deleted inserted replaced
5084:ca45dd039b54 7730:e1b831fe435a
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.importer.parsers;
10
11 import java.io.File;
12
13 import java.util.List;
14 import java.util.ArrayList;
15
16 import java.util.regex.Pattern;
17 import java.util.regex.Matcher;
18
19 import java.io.IOException;
20 import java.io.LineNumberReader;
21 import java.io.FileInputStream;
22 import java.io.InputStreamReader;
23
24 import org.apache.log4j.Logger;
25
26 import org.dive4elements.artifacts.common.utils.FileTools;
27
28 import org.dive4elements.river.importer.ImportRiver;
29
30
31 /** Processes files mentioned in an info file for a river. */
32 public class InfoGewParser
33 {
34 private static Logger log = Logger.getLogger(InfoGewParser.class);
35
36 public static final String ENCODING = "ISO-8859-1";
37
38 public static final Pattern GEWAESSER =
39 Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)");
40
41 public static final Pattern WST_DATEI =
42 Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)");
43
44 public static final Pattern BB_INFO =
45 Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)");
46
47 protected ArrayList<ImportRiver> rivers;
48
49 protected AnnotationClassifier annotationClassifier;
50
51 public InfoGewParser() {
52 this(null);
53 }
54
55 public InfoGewParser(AnnotationClassifier annotationClassifier) {
56 rivers = new ArrayList<ImportRiver>();
57 this.annotationClassifier = annotationClassifier;
58 }
59
60 public List<ImportRiver> getRivers() {
61 return rivers;
62 }
63
64 public static final String normalize(String f) {
65 return f.replace("\\", "/").replace("/", File.separator);
66 }
67
68 /** Handle a gew, wst, or bb_info file. */
69 public void parse(File file) throws IOException {
70
71 LineNumberReader in = null;
72
73 File root = file.getParentFile();
74
75 try {
76 in =
77 new LineNumberReader(
78 new InputStreamReader(
79 new FileInputStream(file), ENCODING));
80
81 String line = null;
82
83 String riverName = null;
84 File wstFile = null;
85 File bbInfoFile = null;
86
87 while ((line = in.readLine()) != null) {
88 if ((line = line.trim()).length() == 0) {
89 continue;
90 }
91 Matcher m = GEWAESSER.matcher(line);
92
93 if (m.matches()) {
94 String river = m.group(1);
95 log.info("Found river '" + river + "'");
96 if (riverName != null) {
97 rivers.add(new ImportRiver(
98 riverName,
99 wstFile,
100 bbInfoFile,
101 annotationClassifier));
102 }
103 riverName = river;
104 wstFile = null;
105 bbInfoFile = null;
106 }
107 else if ((m = WST_DATEI.matcher(line)).matches()) {
108 String wstFilename = m.group(1);
109 File wst = new File(wstFilename = normalize(wstFilename));
110 if (!wst.isAbsolute()) {
111 wst = new File(root, wstFilename);
112 }
113 wst = FileTools.repair(wst);
114 log.info("Found wst file '" + wst + "'");
115 if (!wst.isFile() || !wst.canRead()) {
116 log.error("cannot access WST file '" + wstFilename + "'");
117 continue;
118 }
119 wstFile = wst;
120 }
121 else if ((m = BB_INFO.matcher(line)).matches()) {
122 //TODO: Make it relative to the wst file.
123 String bbInfo = m.group(1);
124 bbInfoFile = new File(normalize(bbInfo));
125 }
126 }
127 if (riverName != null) {
128 rivers.add(new ImportRiver(
129 riverName,
130 wstFile,
131 bbInfoFile,
132 annotationClassifier));
133 }
134 }
135 finally {
136 if (in != null) {
137 in.close();
138 }
139 }
140
141 for (ImportRiver river: rivers) {
142 river.parseDependencies();
143 }
144 }
145 }
146 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org