Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/CollisionParser.java @ 8971:50416a0df385
Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author | mschaefer |
---|---|
date | Tue, 03 Apr 2018 10:18:30 +0200 |
parents | |
children | ae76f618d990 |
comparison
equal
deleted
inserted
replaced
8970:da5dc7446652 | 8971:50416a0df385 |
---|---|
1 /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 | |
11 package org.dive4elements.river.importer.sinfo.parsers; | |
12 | |
13 import java.io.File; | |
14 import java.text.DateFormat; | |
15 import java.text.SimpleDateFormat; | |
16 import java.util.ArrayList; | |
17 import java.util.Date; | |
18 import java.util.EnumMap; | |
19 import java.util.HashMap; | |
20 import java.util.List; | |
21 import java.util.regex.Matcher; | |
22 import java.util.regex.Pattern; | |
23 | |
24 import org.apache.log4j.Logger; | |
25 import org.dive4elements.river.importer.Config; | |
26 import org.dive4elements.river.importer.ImportRiver; | |
27 import org.dive4elements.river.importer.common.AbstractParser; | |
28 import org.dive4elements.river.importer.common.ParsingState; | |
29 import org.dive4elements.river.importer.sinfo.importitem.CollisionKmLineImport; | |
30 import org.dive4elements.river.importer.sinfo.importitem.CollisionSeriesImport; | |
31 import org.dive4elements.river.importer.sinfo.importitem.CollisionTypeImport; | |
32 import org.dive4elements.river.model.sinfo.Collision; | |
33 import org.dive4elements.river.model.sinfo.CollisionType; | |
34 import org.dive4elements.river.model.sinfo.CollisionValue; | |
35 | |
36 /** | |
37 * Reads and parses a collision file | |
38 * | |
39 * @author Matthias Schäfer | |
40 * | |
41 */ | |
42 public class CollisionParser extends AbstractParser<Collision, CollisionValue, CollisionKmLineImport, CollisionSeriesImport> { | |
43 | |
44 /***** FIELDS *****/ | |
45 | |
46 private static final Logger log = Logger.getLogger(CollisionParser.class); | |
47 | |
48 private static final Pattern META_YEAR = Pattern.compile("^#\\sJahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE); | |
49 | |
50 private enum ColTitlePattern { | |
51 DATE("Datum.*"), // | |
52 GAUGE_W("Pegelstand\\s*\\[(.*)\\].*"), // | |
53 GAUGE_NAME("Bezugspegel.*"), // | |
54 TYPE("Unfallart.*"); | |
55 | |
56 private final Pattern pattern; | |
57 | |
58 ColTitlePattern(final String regexp) { | |
59 this.pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); | |
60 } | |
61 | |
62 public Pattern getPattern() { | |
63 return this.pattern; | |
64 } | |
65 } | |
66 | |
67 private static final DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); | |
68 | |
69 private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class); | |
70 | |
71 private final HashMap<String, CollisionTypeImport> types; | |
72 | |
73 | |
74 /***** CONSTRUCTORS *****/ | |
75 | |
76 public CollisionParser(final File importPath, final File rootRelativePath, final ImportRiver river) { | |
77 super(importPath, rootRelativePath, river); | |
78 this.types = new HashMap<>(); | |
79 for (final CollisionType type : CollisionType.getTypes()) | |
80 this.types.put(type.getName().trim().toLowerCase(), new CollisionTypeImport(type.getName())); | |
81 } | |
82 | |
83 | |
84 /***** METHODS *****/ | |
85 | |
86 @Override | |
87 protected Logger getLog() { | |
88 return log; | |
89 } | |
90 | |
91 /** | |
92 * Whether this import type shall be skipped | |
93 */ | |
94 public static boolean shallSkip() { | |
95 return Config.INSTANCE.skipSInfoCollision(); | |
96 } | |
97 | |
98 /** | |
99 * Creates a list of parsers for all collision import files in a directory | |
100 */ | |
101 public static List<CollisionParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { | |
102 final List<CollisionParser> parsers = new ArrayList<>(); | |
103 for (final File file : listFiles(importDir, ".csv")) | |
104 parsers.add(new CollisionParser(file, new File(relativeDir, file.getName()), river)); | |
105 return parsers; | |
106 } | |
107 | |
108 @Override | |
109 protected CollisionSeriesImport createSeriesImport(final String filename) { | |
110 return new CollisionSeriesImport(filename); | |
111 } | |
112 | |
113 @Override | |
114 protected boolean kmMustBeUnique() { | |
115 return false; | |
116 } | |
117 | |
118 @Override | |
119 protected boolean handleMetaOther() { | |
120 if (handleMetaYear()) | |
121 return true; | |
122 else | |
123 return false; | |
124 } | |
125 | |
126 private boolean handleMetaYear() { | |
127 final Matcher m = META_YEAR.matcher(this.currentLine); | |
128 if (m.matches()) { | |
129 this.metaPatternsMatched.add(META_YEAR); | |
130 this.seriesHeader.setYear(Integer.parseInt(m.group(1))); | |
131 return true; | |
132 } | |
133 return false; | |
134 } | |
135 | |
136 @Override | |
137 protected boolean handleMetaColumnTitles() { | |
138 if (!super.handleMetaColumnTitles()) | |
139 return false; | |
140 for (final ColTitlePattern col : ColTitlePattern.values()) | |
141 this.cols.put(col, -1); | |
142 for (int i = 1; i <= this.columnTitles.size() - 1; i++) { | |
143 for (final ColTitlePattern col : ColTitlePattern.values()) { | |
144 if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { | |
145 this.cols.put(col, i); | |
146 break; | |
147 } | |
148 } | |
149 } | |
150 if (this.cols.get(ColTitlePattern.DATE) < 0) | |
151 logWarning("Column of the event dates could not be identified, missing column title 'Datum'"); | |
152 if (this.cols.get(ColTitlePattern.TYPE) < 0) { | |
153 logError("Column of the collision types could not be identified, missing column title 'Unfallart'"); | |
154 this.headerParsingState = ParsingState.STOP; | |
155 return false; | |
156 } | |
157 if (!this.metaPatternsMatched.contains(META_YEAR)) { | |
158 logError("Required meta info for the year is missing"); | |
159 this.headerParsingState = ParsingState.STOP; | |
160 } | |
161 return true; | |
162 } | |
163 | |
164 @Override | |
165 protected CollisionKmLineImport createKmLineImport(final Double km, final String[] values) { | |
166 Date eventDate = null; | |
167 try { | |
168 eventDate = dateFormat.parse(values[this.cols.get(ColTitlePattern.DATE)]); | |
169 } | |
170 catch (final Exception e) { | |
171 logError("Invalid date in line " + this.in.getLineNumber()); | |
172 return null; | |
173 } | |
174 final String typeName = values[this.cols.get(ColTitlePattern.TYPE)].trim(); | |
175 final String typeKey = typeName.toLowerCase(); | |
176 CollisionTypeImport type = null; | |
177 if (this.types.containsKey(typeKey)) | |
178 type = this.types.get(typeKey); | |
179 else { | |
180 type = new CollisionTypeImport(typeName); | |
181 this.types.put(typeKey, type); | |
182 } | |
183 String gaugeName = null; | |
184 if (this.cols.get(ColTitlePattern.GAUGE_NAME) >= 0) | |
185 gaugeName = values[this.cols.get(ColTitlePattern.GAUGE_NAME)].trim(); | |
186 double gaugeW = Double.NaN; | |
187 if (this.cols.get(ColTitlePattern.GAUGE_W) >= 0) | |
188 gaugeW = parseDoubleWithNull(values[this.cols.get(ColTitlePattern.GAUGE_W)]).doubleValue(); | |
189 return new CollisionKmLineImport(km, type, eventDate, gaugeName, gaugeW); | |
190 } | |
191 } |