comparison backend/src/main/java/org/dive4elements/river/importer/parsers/InfoGewParser.java @ 9650:a2a42a6bac6b

Importer (s/u-info) extensions: outer try/catch for parse and log of line no, catching parsing exception if not enough value fields, parsing error and warning log messages with line number, detecting and rejecting duplicate data series, better differentiation between error and warning log messages
author mschaefer
date Mon, 23 Mar 2020 14:57:03 +0100
parents 5e38e2924c07
children
comparison
equal deleted inserted replaced
9649:295b3cb5ebc8 9650:a2a42a6bac6b
7 */ 7 */
8 8
9 package org.dive4elements.river.importer.parsers; 9 package org.dive4elements.river.importer.parsers;
10 10
11 import java.io.File; 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; 12 import java.io.FileInputStream;
22 import java.io.InputStreamReader; 13 import java.io.InputStreamReader;
14 import java.io.LineNumberReader;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
23 19
24 import org.apache.log4j.Logger; 20 import org.apache.log4j.Logger;
25
26 import org.dive4elements.artifacts.common.utils.FileTools; 21 import org.dive4elements.artifacts.common.utils.FileTools;
27
28 import org.dive4elements.river.importer.ImportRiver; 22 import org.dive4elements.river.importer.ImportRiver;
29 23
30 24
31 /** Processes files mentioned in an info file for a river. */ 25 /** Processes files mentioned in an info file for a river. */
32 public class InfoGewParser 26 public class InfoGewParser
34 private static Logger log = Logger.getLogger(InfoGewParser.class); 28 private static Logger log = Logger.getLogger(InfoGewParser.class);
35 29
36 public static final String ENCODING = "ISO-8859-1"; 30 public static final String ENCODING = "ISO-8859-1";
37 31
38 public static final Pattern GEWAESSER = 32 public static final Pattern GEWAESSER =
39 Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)"); 33 Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)");
40 34
41 public static final Pattern WST_DATEI = 35 public static final Pattern WST_DATEI =
42 Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)"); 36 Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)");
43 37
44 public static final Pattern BB_INFO = 38 public static final Pattern BB_INFO =
45 Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)"); 39 Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)");
46 40
47 public static final Pattern GEW_UUID = 41 public static final Pattern GEW_UUID =
48 Pattern.compile("^\\s*uuid\\s*:\\s*(.+)"); 42 Pattern.compile("^\\s*uuid\\s*:\\s*(.+)");
49 43
50 protected ArrayList<ImportRiver> rivers; 44 protected ArrayList<ImportRiver> rivers;
51 45
52 protected AnnotationClassifier annotationClassifier; 46 protected AnnotationClassifier annotationClassifier;
53 47
54 public InfoGewParser() { 48 public InfoGewParser() {
55 this(null); 49 this(null);
56 } 50 }
57 51
58 public InfoGewParser(AnnotationClassifier annotationClassifier) { 52 public InfoGewParser(final AnnotationClassifier annotationClassifier) {
59 rivers = new ArrayList<ImportRiver>(); 53 this.rivers = new ArrayList<>();
60 this.annotationClassifier = annotationClassifier; 54 this.annotationClassifier = annotationClassifier;
61 } 55 }
62 56
63 public List<ImportRiver> getRivers() { 57 public List<ImportRiver> getRivers() {
64 return rivers; 58 return this.rivers;
65 } 59 }
66 60
67 public static final String normalize(String f) { 61 public static final String normalize(final String f) {
68 return f.replace("\\", "/").replace("/", File.separator); 62 return f.replace("\\", "/").replace("/", File.separator);
69 } 63 }
70 64
71 /** Handle a gew, wst, or bb_info file. */ 65 /** Handle a gew, wst, or bb_info file. */
72 public void parse(File file) throws IOException { 66 public void parse(final File file) throws Exception {
73 67
74 LineNumberReader in = null; 68 LineNumberReader in = null;
75 69
76 File root = file.getParentFile(); 70 final File root = file.getParentFile();
77 71
78 try { 72 try {
79 in = 73 in =
80 new LineNumberReader( 74 new LineNumberReader(
81 new InputStreamReader( 75 new InputStreamReader(
82 new FileInputStream(file), ENCODING)); 76 new FileInputStream(file), ENCODING));
83 77
84 String line = null; 78 String line = null;
85 79
86 String riverName = null; 80 String riverName = null;
87 String modelUuid = null; 81 String modelUuid = null;
93 continue; 87 continue;
94 } 88 }
95 Matcher m = GEWAESSER.matcher(line); 89 Matcher m = GEWAESSER.matcher(line);
96 90
97 if (m.matches()) { 91 if (m.matches()) {
98 String river = m.group(1); 92 final String river = m.group(1);
99 log.info("Found river '" + river + "'"); 93 log.info("Found river '" + river + "'");
100 if (riverName != null) { 94 if (riverName != null) {
101 rivers.add(new ImportRiver( 95 this.rivers.add(new ImportRiver(
102 riverName, 96 riverName,
103 modelUuid, 97 modelUuid,
104 wstFile, 98 wstFile,
105 bbInfoFile, 99 bbInfoFile,
106 annotationClassifier)); 100 this.annotationClassifier));
107 } 101 }
108 riverName = river; 102 riverName = river;
109 modelUuid = null; 103 modelUuid = null;
110 wstFile = null; 104 wstFile = null;
111 bbInfoFile = null; 105 bbInfoFile = null;
117 wst = new File(root, wstFilename); 111 wst = new File(root, wstFilename);
118 } 112 }
119 wst = FileTools.repair(wst); 113 wst = FileTools.repair(wst);
120 if (!wst.isFile() || !wst.canRead()) { 114 if (!wst.isFile() || !wst.canRead()) {
121 log.error( 115 log.error(
122 "cannot access WST file '" + wstFilename + "'"); 116 "cannot access WST file '" + wstFilename + "'");
123 continue; 117 continue;
124 } 118 }
125 log.info("Found wst file '" + wst + "'"); 119 log.info("Found wst file '" + wst + "'");
126 wstFile = wst; 120 wstFile = wst;
127 } 121 }
128 else if ((m = GEW_UUID.matcher(line)).matches()) { 122 else if ((m = GEW_UUID.matcher(line)).matches()) {
129 modelUuid = m.group(1); 123 modelUuid = m.group(1);
130 log.debug("Found model uuid " + modelUuid + 124 log.debug("Found model uuid " + modelUuid +
131 " for river " + riverName); 125 " for river " + riverName);
132 } 126 }
133 else if ((m = BB_INFO.matcher(line)).matches()) { 127 else if ((m = BB_INFO.matcher(line)).matches()) {
134 //TODO: Make it relative to the wst file. 128 //TODO: Make it relative to the wst file.
135 String bbInfo = m.group(1); 129 final String bbInfo = m.group(1);
136 bbInfoFile = new File(normalize(bbInfo)); 130 bbInfoFile = new File(normalize(bbInfo));
137 } 131 }
138 } 132 }
139 if (riverName != null && wstFile != null) { 133 if (riverName != null && wstFile != null) {
140 rivers.add(new ImportRiver( 134 this.rivers.add(new ImportRiver(
141 riverName, 135 riverName,
142 modelUuid, 136 modelUuid,
143 wstFile, 137 wstFile,
144 bbInfoFile, 138 bbInfoFile,
145 annotationClassifier)); 139 this.annotationClassifier));
146 } 140 }
147 } 141 }
148 finally { 142 finally {
149 if (in != null) { 143 if (in != null) {
150 in.close(); 144 in.close();
151 } 145 }
152 } 146 }
153 147
154 for (ImportRiver river: rivers) { 148 for (final ImportRiver river: this.rivers) {
155 river.parseDependencies(); 149 river.parseDependencies();
156 } 150 }
157 } 151 }
158 } 152 }
159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 153 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org