comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/ChannelParser.java @ 9653:3b3c7513472e

Importer (s/u-info) extensions: support of multiple channel data series, check for non-overlapping year ranges
author mschaefer
date Mon, 23 Mar 2020 15:06:26 +0100
parents ae76f618d990
children
comparison
equal deleted inserted replaced
9652:dd93bb84561d 9653:3b3c7513472e
36 36
37 /***** FIELDS *****/ 37 /***** FIELDS *****/
38 38
39 private static final Logger log = Logger.getLogger(ChannelParser.class); 39 private static final Logger log = Logger.getLogger(ChannelParser.class);
40 40
41 private static final String IMPORT_FILENAME = "Fahrrinne.csv"; 41 private static final Pattern IMPORT_FILENAME = Pattern.compile("Fahrrinne.*\\.csv", Pattern.CASE_INSENSITIVE);
42 42
43 protected static final Pattern META_YEARS = Pattern.compile("^#\\sZeitraum:\\s*([12]\\d\\d\\d)*\\s*-\\s*([12]\\d\\d\\d)*.*", Pattern.CASE_INSENSITIVE); 43 private static final Pattern META_FIRST = Pattern.compile("^#\\sFahrrinne.*", Pattern.CASE_INSENSITIVE);
44
45 private static final Pattern META_YEARS = Pattern.compile("^#\\sZeitraum:\\s*([12]\\d\\d\\d)*\\s*-\\s*([12]\\d\\d\\d)*.*", Pattern.CASE_INSENSITIVE);
44 46
45 private static final Pattern WIDTH_COLUMNTITLE = Pattern.compile("Sollbreite\\s*\\[(.*)\\].*", Pattern.CASE_INSENSITIVE); 47 private static final Pattern WIDTH_COLUMNTITLE = Pattern.compile("Sollbreite\\s*\\[(.*)\\].*", Pattern.CASE_INSENSITIVE);
46 48
47 private static final Pattern DEPTH_COLUMNTITLE = Pattern.compile("Solltiefe\\s*\\[(.*)\\].*", Pattern.CASE_INSENSITIVE); 49 private static final Pattern DEPTH_COLUMNTITLE = Pattern.compile("Solltiefe\\s*\\[(.*)\\].*", Pattern.CASE_INSENSITIVE);
48 50
77 /** 79 /**
78 * Creates a list of parsers for all channel import files in a directory 80 * Creates a list of parsers for all channel import files in a directory
79 */ 81 */
80 public static List<ChannelParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) { 82 public static List<ChannelParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
81 final List<ChannelParser> parsers = new ArrayList<>(); 83 final List<ChannelParser> parsers = new ArrayList<>();
82 final File importFile = new File(importDir, IMPORT_FILENAME); 84 if (importDir.exists())
83 if (importFile.exists()) 85 for (final File file : listFiles(importDir, IMPORT_FILENAME))
84 parsers.add(new ChannelParser(importFile, new File(relativeDir, IMPORT_FILENAME), river)); 86 parsers.add(new ChannelParser(file, new File(relativeDir, file.getName()), river));
85 return parsers; 87 return parsers;
86 } 88 }
87 89
88 @Override 90 @Override
89 protected boolean handleMetaOther() { 91 protected boolean handleMetaOther() {
90 if (handleMetaYears()) 92 if (handleMetaFirst())
93 return true;
94 else if (handleMetaYears())
91 return true; 95 return true;
92 else 96 else
93 return false; 97 return false;
98 }
99
100 private boolean handleMetaFirst() {
101 final Matcher m = META_FIRST.matcher(this.currentLine);
102 if (m.matches()) {
103 this.metaPatternsMatched.add(META_FIRST);
104 return true;
105 }
106 return false;
94 } 107 }
95 108
96 private boolean handleMetaYears() { 109 private boolean handleMetaYears() {
97 final Matcher m = META_YEARS.matcher(this.currentLine); 110 final Matcher m = META_YEARS.matcher(this.currentLine);
98 if (m.matches()) { 111 if (m.matches()) {
99 this.metaPatternsMatched.add(META_YEARS); 112 this.metaPatternsMatched.add(META_YEARS);
100 if (m.group(1) != null) 113 try {
101 this.seriesHeader.setYear_from(Integer.valueOf(m.group(1))); 114 if (m.group(1) != null)
102 else 115 this.seriesHeader.setYear_from(Integer.valueOf(m.group(1)));
103 this.seriesHeader.setYear_from(null); 116 else
104 if (m.group(2) != null) 117 throw new NumberFormatException();
105 this.seriesHeader.setYear_to(Integer.valueOf(m.group(2))); 118 if (m.group(2) != null)
106 else 119 this.seriesHeader.setYear_to(Integer.valueOf(m.group(2)));
107 this.seriesHeader.setYear_to(null); 120 else
121 throw new NumberFormatException();
122 }
123 catch (final Exception e) {
124 logLineError("Invalid or missing start and/or end year");
125 this.headerParsingState = ParsingState.STOP;
126 }
108 return true; 127 return true;
109 } 128 }
110 return false; 129 return false;
111 } 130 }
112 131
118 this.depthColIndex = i; 137 this.depthColIndex = i;
119 else if (WIDTH_COLUMNTITLE.matcher(this.columnTitles.get(i)).matches()) 138 else if (WIDTH_COLUMNTITLE.matcher(this.columnTitles.get(i)).matches())
120 this.widthColIndex = i; 139 this.widthColIndex = i;
121 } 140 }
122 if ((this.widthColIndex < 0) || (this.depthColIndex < 0)) { 141 if ((this.widthColIndex < 0) || (this.depthColIndex < 0)) {
123 logError("Columns of width and/or depth values could not be identified, missing column title 'Sollbreite...'/'Sollhöhe...'"); 142 logLineError("Columns of width and/or depth values could not be identified, missing column title 'Sollbreite...'/'Sollhöhe...'");
124 this.headerParsingState = ParsingState.STOP; 143 this.headerParsingState = ParsingState.STOP;
125 return false;
126 } 144 }
127 return true; 145 return true;
128 } 146 }
129 else 147 else
130 return false; 148 return false;
135 return new ChannelSeriesImport(filename); 153 return new ChannelSeriesImport(filename);
136 } 154 }
137 155
138 @Override 156 @Override
139 protected ChannelKmLineImport createKmLineImport(final Double km, final String[] values) { 157 protected ChannelKmLineImport createKmLineImport(final Double km, final String[] values) {
140 if (parseDoubleWithNull(values[this.widthColIndex]) == null) { 158 final Number width = parseDoubleCheckNull(values, this.widthColIndex);
141 logError("Invalid width value in line " + this.in.getLineNumber()); 159 if ((width == null) || Double.isNaN(width.doubleValue())) {
160 logLineError(INVALID_VALUE_ERROR_FORMAT, "width");
142 return null; 161 return null;
143 } 162 }
144 if (parseDoubleWithNull(values[this.depthColIndex]) == null) { 163 final Number depth = parseDoubleCheckNull(values, this.depthColIndex);
145 logError("Invalid depth value in line " + this.in.getLineNumber()); 164 if ((depth == null) || Double.isNaN(depth.doubleValue())) {
165 logLineError(INVALID_VALUE_ERROR_FORMAT, "depth");
146 return null; 166 return null;
147 } 167 }
148 return new ChannelKmLineImport(km, parseDoubleWithNull(values[this.widthColIndex]).doubleValue(), 168 return new ChannelKmLineImport(km, width.doubleValue(), depth.doubleValue());
149 parseDoubleWithNull(values[this.depthColIndex]).doubleValue());
150 } 169 }
151 } 170 }

http://dive4elements.wald.intevation.org