Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/uinfo/parsers/SalixParser.java @ 9660:f0cad5212f49
Importer (s/u-info) extensions: iota (salix): detecting, logging, cancelling in case of wrong column titles/units,
detecting, logging and skipping lines with missing values
author | mschaefer |
---|---|
date | Mon, 23 Mar 2020 15:40:12 +0100 |
parents | 66a43d9f65c8 |
children |
comparison
equal
deleted
inserted
replaced
9659:75bd347147ad | 9660:f0cad5212f49 |
---|---|
39 | 39 |
40 private static final Logger log = Logger.getLogger(SalixParser.class); | 40 private static final Logger log = Logger.getLogger(SalixParser.class); |
41 | 41 |
42 private static final String IMPORT_FILENAME = "Iota.csv"; | 42 private static final String IMPORT_FILENAME = "Iota.csv"; |
43 | 43 |
44 private static final Pattern META_FIRST = Pattern.compile("^#\\sIota.*", Pattern.CASE_INSENSITIVE); | |
45 | |
44 private static final Pattern META_EVALUATOR = Pattern.compile("^#\\sAuswerter:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE); | 46 private static final Pattern META_EVALUATOR = Pattern.compile("^#\\sAuswerter:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE); |
45 | 47 |
46 private enum ColTitlePattern { | 48 private enum ColTitlePattern { |
47 FACTOR("Iota\\s*\\[(.*)\\].*"), // | 49 FACTOR("Iota\\s*\\[(.*)\\].*"), // |
48 MWMNW("\\(MW-MNW\\).*\\[(.*)\\].*"); | 50 MWMNW("\\(MW-MNW\\).*\\[(.*)\\].*"); |
56 public Pattern getPattern() { | 58 public Pattern getPattern() { |
57 return this.pattern; | 59 return this.pattern; |
58 } | 60 } |
59 } | 61 } |
60 | 62 |
61 private final EnumMap<ColTitlePattern, Integer> cols = new EnumMap<>(ColTitlePattern.class); | 63 private final EnumMap<ColTitlePattern, Integer> cols; |
64 | |
65 private final EnumMap<ColTitlePattern, String> units; | |
66 | |
62 | 67 |
63 /***** CONSTRUCTORS *****/ | 68 /***** CONSTRUCTORS *****/ |
64 | 69 |
65 public SalixParser(final File importPath, final File rootRelativePath, final ImportRiver river) { | 70 public SalixParser(final File importPath, final File rootRelativePath, final ImportRiver river) { |
66 super(importPath, rootRelativePath, river); | 71 super(importPath, rootRelativePath, river); |
72 this.cols = new EnumMap<>(ColTitlePattern.class); | |
73 this.units = new EnumMap<>(ColTitlePattern.class); | |
67 } | 74 } |
68 | 75 |
69 /***** METHODS *****/ | 76 /***** METHODS *****/ |
70 | 77 |
71 @Override | 78 @Override |
96 return new SalixSeriesImport(filename); | 103 return new SalixSeriesImport(filename); |
97 } | 104 } |
98 | 105 |
99 @Override | 106 @Override |
100 protected boolean handleMetaOther() { | 107 protected boolean handleMetaOther() { |
101 if (handleMetaEvaluator()) | 108 if (handleMetaFirst()) |
109 return true; | |
110 else if (handleMetaEvaluator()) | |
102 return true; | 111 return true; |
103 else | 112 else |
104 return false; | 113 return false; |
114 } | |
115 | |
116 private boolean handleMetaFirst() { | |
117 final Matcher m = META_FIRST.matcher(this.currentLine); | |
118 if (m.matches()) { | |
119 this.metaPatternsMatched.add(META_FIRST); | |
120 return true; | |
121 } | |
122 return false; | |
105 } | 123 } |
106 | 124 |
107 private boolean handleMetaEvaluator() { | 125 private boolean handleMetaEvaluator() { |
108 final Matcher m = META_EVALUATOR.matcher(this.currentLine); | 126 final Matcher m = META_EVALUATOR.matcher(this.currentLine); |
109 if (m.matches()) { | 127 if (m.matches()) { |
116 | 134 |
117 @Override | 135 @Override |
118 protected boolean handleMetaColumnTitles() { | 136 protected boolean handleMetaColumnTitles() { |
119 if (!super.handleMetaColumnTitles()) | 137 if (!super.handleMetaColumnTitles()) |
120 return false; | 138 return false; |
121 for (final ColTitlePattern col : ColTitlePattern.values()) | 139 for (final ColTitlePattern col : ColTitlePattern.values()) { |
122 this.cols.put(col, -1); | 140 this.cols.put(col, -1); |
141 this.units.put(col, ""); | |
142 } | |
123 for (int i = 1; i <= this.columnTitles.size() - 1; i++) { | 143 for (int i = 1; i <= this.columnTitles.size() - 1; i++) { |
124 for (final ColTitlePattern col : ColTitlePattern.values()) { | 144 for (final ColTitlePattern col : ColTitlePattern.values()) { |
125 if (col.getPattern().matcher(this.columnTitles.get(i)).matches()) { | 145 final Matcher m = col.getPattern().matcher(this.columnTitles.get(i)); |
146 if (m.matches()) { | |
126 this.cols.put(col, i); | 147 this.cols.put(col, i); |
148 this.units.put(col, m.group(1)); | |
127 break; | 149 break; |
128 } | 150 } |
129 } | 151 } |
130 } | 152 } |
131 if ((this.cols.get(ColTitlePattern.FACTOR) < 0) || (this.cols.get(ColTitlePattern.MWMNW) < 0)) { | 153 if ((this.cols.get(ColTitlePattern.FACTOR) < 0) || (this.cols.get(ColTitlePattern.MWMNW) < 0)) { |
132 logError("Column of the iota value and/or mnw-mw-diff could not be identified"); | 154 logLineError("Column of the iota value and/or mnw-mw-diff could not be identified"); |
133 this.headerParsingState = ParsingState.STOP; | 155 this.headerParsingState = ParsingState.STOP; |
134 return false; | 156 return true; |
157 } | |
158 if (!this.units.get(ColTitlePattern.FACTOR).equals("m") || !this.units.get(ColTitlePattern.MWMNW).equals("m")) { | |
159 logLineError("Column of the iota value and/or mnw-mw-diff have unsupported units"); | |
160 this.headerParsingState = ParsingState.STOP; | |
135 } | 161 } |
136 return true; | 162 return true; |
137 } | 163 } |
138 | 164 |
139 @Override | 165 @Override |
140 protected SalixKmLineImport createKmLineImport(final Double km, final String[] values) { | 166 protected SalixKmLineImport createKmLineImport(final Double km, final String[] values) { |
141 if (Double.isNaN(parseDoubleWithNull(values[this.cols.get(ColTitlePattern.FACTOR)]).doubleValue())) { | 167 final Number factor = parseDoubleCheckNull(values, this.cols.get(ColTitlePattern.FACTOR)); |
142 logError("Iota not found in line " + this.in.getLineNumber()); | 168 if ((factor == null) || Double.isNaN(factor.doubleValue())) { |
169 logLineWarning(INVALID_VALUE_ERROR_FORMAT, "iota"); | |
143 return null; | 170 return null; |
144 } | 171 } |
145 if (Double.isNaN(parseDoubleWithNull(values[this.cols.get(ColTitlePattern.MWMNW)]).doubleValue())) { | 172 final Number mnwmw = parseDoubleCheckNull(values, this.cols.get(ColTitlePattern.MWMNW)); |
146 logError("MNW-MW-diff not found in line " + this.in.getLineNumber()); | 173 if ((mnwmw == null) || Double.isNaN(mnwmw.doubleValue())) { |
174 logLineWarning(INVALID_VALUE_ERROR_FORMAT, "MNW-MW-diff"); | |
147 return null; | 175 return null; |
148 } | 176 } |
149 return new SalixKmLineImport(km, parseDoubleWithNull(values[this.cols.get(ColTitlePattern.FACTOR)]).doubleValue(), | 177 return new SalixKmLineImport(km, factor.doubleValue(), mnwmw.doubleValue()); |
150 parseDoubleWithNull(values[this.cols.get(ColTitlePattern.MWMNW)]).doubleValue()); | |
151 } | 178 } |
152 } | 179 } |