Mercurial > dive4elements > river
changeset 2811:8926571e47fb
Finished importing MINFO bed heights (single and epoch).
flys-backend/trunk@4225 c6561f87-3c4e-4783-a992-168aeb5c3f6f
line wrap: on
line diff
--- a/flys-backend/ChangeLog Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/ChangeLog Fri Apr 13 07:24:55 2012 +0000 @@ -1,3 +1,36 @@ +2012-04-13 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.java: + New. An abstract super class for BedHeightSingleParser and + BedHeightEpochParser. It implements methods for parsing meta data. + Concrete subclasses need to implements the method for parsing data rows + only. + + * src/main/java/de/intevation/flys/importer/ImportBedHeight.java, + src/main/java/de/intevation/flys/importer/ImportBedHeightValue.java: New. + Interfaces that define some major methods which enables the BedHeightParser + to parse both - single and epoch bed heights. + + * src/main/java/de/intevation/flys/importer/parsers/BedHeightSingleParser.java: + Moved the code for parsing meta data to BedHeightParser which is now the + parent class. + + * src/main/java/de/intevation/flys/importer/parsers/BedHeightEpochParser.java: + New. A further subclass of BedHeightParser for parsing MINFO bed heights + for epochs. + + * src/main/java/de/intevation/flys/importer/ImportBedHeightSingleValue.java, + src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java: + Made them subclasses of ImportBedHeightValue. + + * src/main/java/de/intevation/flys/importer/ImportBedHeightSingle.java, + src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java: Made + them subclasses of ImportBedHeight. + + * src/main/java/de/intevation/flys/importer/ImportRiver.java: Some + adjustments to store ImportBedHeight and ImportBedHeightValue instances + instead of concrete subclasses. + 2012-04-12 Ingo Weinzierl <ingo@intevation.de> * doc/schema/oracle-minfo.sql: Added a link to the river to bed_height_epoch
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeight.java Fri Apr 13 07:24:55 2012 +0000 @@ -0,0 +1,44 @@ +package de.intevation.flys.importer; + + +import java.sql.SQLException; + +import org.hibernate.exception.ConstraintViolationException; + +import de.intevation.flys.model.River; + + +public interface ImportBedHeight { + + String getDescription(); + + void addValue(ImportBedHeightValue value); + + void storeDependencies(River river) + throws SQLException, ConstraintViolationException; + + Object getPeer(River river); + + int getValueCount(); + + void setYear(int year); + + void setTimeInterval(ImportTimeInterval timeInterval); + + void setSoundingWidth(int soundingWidth); + + void setDescription(String description); + + void setEvaluationBy(String evaluationBy); + + void setRange(ImportRange range); + + void setType(ImportBedHeightType type); + + void setLocationSystem(ImportLocationSystem locationSystem); + + void setCurElevationModel(ImportElevationModel model); + + void setOldElevationModel(ImportElevationModel model); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java Fri Apr 13 07:24:55 2012 +0000 @@ -18,7 +18,7 @@ import de.intevation.flys.model.TimeInterval; -public class ImportBedHeightEpoch +public class ImportBedHeightEpoch implements ImportBedHeight { private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class); @@ -46,8 +46,12 @@ } - public List<ImportBedHeightEpochValue> getValues() { - return values; + public int getValueCount() { + return values.size(); + } + + public void setTimeInterval(ImportTimeInterval timeInterval) { + this.timeInterval = timeInterval; } @@ -71,11 +75,29 @@ this.oldElevationModel = oldElevationModel; } - public void addValue(ImportBedHeightEpochValue value) { - values.add(value); + public void setYear(int year) { + // do nothing + } + + public void setSoundingWidth(int soundingWidth) { + // do nothing + } + + public void setLocationSystem(ImportLocationSystem locationSystem) { + // do nothing + } + + public void setType(ImportBedHeightType type) { + // do nothing + } + + @Override + public void addValue(ImportBedHeightValue value) { + values.add((ImportBedHeightEpochValue) value); } + @Override public void storeDependencies(River river) throws SQLException, ConstraintViolationException { @@ -96,23 +118,37 @@ for (ImportBedHeightEpochValue value: values) { value.storeDependencies(peer); } + + Session session = ImporterSession.getInstance().getDatabaseSession(); + session.flush(); } + @Override public BedHeightEpoch getPeer(River river) { if (peer == null) { ElevationModel theCurModel = curElevationModel != null ? curElevationModel.getPeer() : null; + if (theCurModel == null) { + log.warn("Skip file - invalid current elevation model."); + return null; + } + TimeInterval theTime = timeInterval != null ? timeInterval.getPeer() : null; + if (theTime == null) { + log.warn("Skip file - invalid time range."); + return null; + } + Range theRange = range != null ? range.getPeer(river) : null; - if (theCurModel == null || theRange == null || theTime == null) { - log.warn("Skip invalid file '" + description + "'"); + if (theRange == null) { + log.warn("Skip file - invalid km range."); return null; } @@ -124,7 +160,7 @@ " timeInterval=:timeInterval and " + " curElevationModel=:curElevationModel and " + " range=:range and " + - " evaluationBy=: evaluationBy and " + + " evaluationBy=:evaluationBy and " + " description=:description"); query.setParameter("river", river); @@ -148,6 +184,8 @@ evaluationBy, description ); + + session.save(peer); } else { peer = bedHeights.get(0);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java Fri Apr 13 07:24:55 2012 +0000 @@ -13,7 +13,7 @@ import de.intevation.flys.model.BedHeightEpochValue; -public class ImportBedHeightEpochValue { +public class ImportBedHeightEpochValue implements ImportBedHeightValue { private static final Logger log = Logger.getLogger(ImportBedHeightEpochValue.class);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightSingle.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightSingle.java Fri Apr 13 07:24:55 2012 +0000 @@ -18,7 +18,7 @@ import de.intevation.flys.model.River; -public class ImportBedHeightSingle +public class ImportBedHeightSingle implements ImportBedHeight { private static Logger log = Logger.getLogger(ImportBedHeightSingle.class); @@ -49,8 +49,8 @@ return description; } - public List<ImportBedHeightSingleValue> getValues() { - return values; + public int getValueCount() { + return values.size(); } @@ -58,6 +58,10 @@ this.year = year; } + public void setTimeInterval(ImportTimeInterval timeInterval) { + // do nothing + } + public void setSoundingWidth(int soundingWidth) { this.soundingWidth = soundingWidth; } @@ -90,10 +94,12 @@ this.oldElevationModel = oldElevationModel; } - public void addValue(ImportBedHeightSingleValue value) { - values.add(value); + @Override + public void addValue(ImportBedHeightValue value) { + values.add((ImportBedHeightSingleValue) value); } + @Override public void storeDependencies(River river) throws SQLException, ConstraintViolationException { @@ -120,8 +126,12 @@ for (ImportBedHeightSingleValue value: values) { value.storeDependencies(peer); } + + Session session = ImporterSession.getInstance().getDatabaseSession(); + session.flush(); } + @Override public BedHeightSingle getPeer(River river) { if (peer == null) { BedHeightType theType = type != null ? type.getPeer() : null;
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightSingleValue.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightSingleValue.java Fri Apr 13 07:24:55 2012 +0000 @@ -13,7 +13,7 @@ import de.intevation.flys.model.BedHeightSingleValue; -public class ImportBedHeightSingleValue { +public class ImportBedHeightSingleValue implements ImportBedHeightValue { private static final Logger log = Logger.getLogger(ImportBedHeightSingleValue.class);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightValue.java Fri Apr 13 07:24:55 2012 +0000 @@ -0,0 +1,7 @@ +package de.intevation.flys.importer; + + +public interface ImportBedHeightValue { + +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Fri Apr 13 07:24:55 2012 +0000 @@ -92,9 +92,9 @@ protected List<ImportWst> floodProtection; - protected List<ImportBedHeightSingle> bedHeightSingles; + protected List<ImportBedHeight> bedHeightSingles; - protected List<ImportBedHeightEpoch> bedHeightEpochs; + protected List<ImportBedHeight> bedHeightEpochs; protected ImportWst wst; @@ -723,7 +723,9 @@ River river = getPeer(); if (bedHeightSingles != null) { - for (ImportBedHeightSingle single: bedHeightSingles) { + for (ImportBedHeight tmp: bedHeightSingles) { + ImportBedHeightSingle single = (ImportBedHeightSingle) tmp; + String desc = single.getDescription(); log.debug("name: " + desc); @@ -749,7 +751,9 @@ River river = getPeer(); if (bedHeightEpochs != null) { - for (ImportBedHeightEpoch epoch: bedHeightEpochs) { + for (ImportBedHeight tmp: bedHeightEpochs) { + ImportBedHeightEpoch epoch = (ImportBedHeightEpoch) tmp; + String desc = epoch.getDescription(); log.debug("name: " + desc);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightEpochParser.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightEpochParser.java Fri Apr 13 07:24:55 2012 +0000 @@ -1,63 +1,51 @@ package de.intevation.flys.importer.parsers; -import java.io.File; -import java.util.List; -import java.util.ArrayList; +import java.math.BigDecimal; -import java.io.IOException; -import java.io.LineNumberReader; -import java.io.FileInputStream; -import java.io.InputStreamReader; +import java.text.ParseException; + + import org.apache.log4j.Logger; +import de.intevation.flys.importer.ImportBedHeight; import de.intevation.flys.importer.ImportBedHeightEpoch; +import de.intevation.flys.importer.ImportBedHeightEpochValue; -public class BedHeightEpochParser { - - public static final String ENCODING = "ISO-8859-1"; - - - protected List<ImportBedHeightEpoch> bedHeights; - +public class BedHeightEpochParser extends BedHeightParser { private static final Logger log = Logger.getLogger(BedHeightEpochParser.class); - public BedHeightEpochParser() { - bedHeights = new ArrayList<ImportBedHeightEpoch>(); - } - - public List<ImportBedHeightEpoch> getBedHeights() { - return bedHeights; + @Override + protected ImportBedHeight newImportBedHeight(String description) { + return new ImportBedHeightEpoch(description); } - public void parse(File file) throws IOException { - log.info("Parsing bed height epoch file '" + file + "'"); - - LineNumberReader in = null; - try { - in = - new LineNumberReader( - new InputStreamReader( - new FileInputStream(file), ENCODING)); + @Override + protected void handleDataLine(ImportBedHeight obj, String line) { + String[] values = line.split(SEPERATOR_CHAR); - String line = null; - while ((line = in.readLine()) != null) { - if ((line = line.trim()).length() == 0) { - continue; - } - } + if (values == null || values.length < 2 || values[0].length() == 0 || values[1].length() == 0) { + //log.warn("Skip invalid data line: " + line); + return; } - finally { - if (in != null) { - in.close(); - } + + try { + ImportBedHeightEpochValue value = new ImportBedHeightEpochValue( + new BigDecimal(nf.parse(values[0]).doubleValue()), + new BigDecimal(nf.parse(values[1]).doubleValue()) + ); + + obj.addValue(value); + } + catch (ParseException e) { + log.warn("Error while parsing number from data row: " + line); } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightParser.java Fri Apr 13 07:24:55 2012 +0000 @@ -0,0 +1,405 @@ +package de.intevation.flys.importer.parsers; + +import java.io.File; + +import java.math.BigDecimal; + +import java.text.NumberFormat; +import java.text.ParseException; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportBedHeight; +import de.intevation.flys.importer.ImportBedHeightType; +import de.intevation.flys.importer.ImportElevationModel; +import de.intevation.flys.importer.ImportLocationSystem; +import de.intevation.flys.importer.ImportRange; +import de.intevation.flys.importer.ImportTimeInterval; +import de.intevation.flys.importer.ImportUnit; +import de.intevation.flys.model.BedHeightType; + + +public abstract class BedHeightParser { + + private static final Logger log = + Logger.getLogger(BedHeightParser.class); + + public static final String ENCODING = "ISO-8859-1"; + + public static final Locale DEFAULT_LOCALE = Locale.GERMAN; + + public static final String START_META_CHAR = "#"; + public static final String SEPERATOR_CHAR = ";"; + + public static final Pattern META_YEAR = + Pattern.compile("^Jahr: (\\d*).*"); + + public static final Pattern META_TIMEINTERVAL = + Pattern.compile("^Zeitraum: Epoche (\\d*)-(\\d*).*"); + + public static final Pattern META_TYPE = + Pattern.compile("^Aufnahmeart: (.*).*"); + + public static final Pattern META_LOCATION_SYSTEM = + Pattern.compile("^Lagesystem: (.*).*"); + + public static final Pattern META_CUR_ELEVATION_SYSTEM = + Pattern.compile("^H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); + + public static final Pattern META_OLD_ELEVATION_SYSTEM = + Pattern.compile("^urspr.ngliches H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); + + public static final Pattern META_SOUNDING_WIDTH = + Pattern.compile("^ausgewertete Peilbreite: (\\d*).*"); + + public static final Pattern META_RANGE = + Pattern.compile("^Strecke:\\D*(\\d++.\\d*)-(\\d++.\\d*).*"); + + public static final Pattern META_EVALUATION_BY = + Pattern.compile("^Auswerter: (.*).*"); + + public static final Pattern META_COMMENTS = + Pattern.compile("^Weitere Bemerkungen: (.*).*"); + + + protected static NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); + + + protected List<ImportBedHeight> bedHeights; + + + protected abstract ImportBedHeight newImportBedHeight(String description); + + protected abstract void handleDataLine( + ImportBedHeight importBedHeight, + String line + ); + + + + public BedHeightParser() { + this.bedHeights = new ArrayList<ImportBedHeight>(); + } + + + public List<ImportBedHeight> getBedHeights() { + return bedHeights; + } + + + public void parse(File file) throws IOException { + log.info("Parsing bed height single file '" + file + "'"); + + ImportBedHeight obj = newImportBedHeight(file.getName()); + + LineNumberReader in = null; + try { + in = + new LineNumberReader( + new InputStreamReader( + new FileInputStream(file), ENCODING)); + + String line = null; + while ((line = in.readLine()) != null) { + if ((line = line.trim()).length() == 0) { + continue; + } + + if (line.startsWith(START_META_CHAR)) { + handleMetaLine(obj, line); + } + else { + handleDataLine(obj, line); + } + } + + log.info("File contained " + obj.getValueCount() + " values."); + bedHeights.add(obj); + } + finally { + if (in != null) { + in.close(); + } + } + } + + + protected static String stripMetaLine(String line) { + String tmp = line.substring(1, line.length()); + + if (tmp.startsWith(" ")) { + return tmp.substring(1, tmp.length()); + } + else { + return tmp; + } + } + + + public static Date getDateFromYear(int year) { + Calendar cal = Calendar.getInstance(); + cal.set(year, 0, 1); + + return cal.getTime(); + } + + + protected void handleMetaLine(ImportBedHeight obj, String line) { + String meta = stripMetaLine(line); + + if (handleMetaYear(obj, meta)) { + return; + } + else if (handleMetaTimeInterval(obj, meta)) { + return; + } + else if (handleMetaSoundingWidth(obj, meta)) { + return; + } + else if (handleMetaComment(obj, meta)) { + return; + } + else if (handleMetaEvaluationBy(obj, meta)) { + return; + } + else if (handleMetaRange(obj, meta)) { + return; + } + else if (handleMetaType(obj, meta)) { + return; + } + else if (handleMetaLocationSystem(obj, meta)) { + return; + } + else if (handleMetaCurElevationModel(obj, meta)) { + return; + } + else if (handleMetaOldElevationModel(obj, meta)) { + return; + } + else { + log.warn("Meta line did not match any known type: " + line); + } + } + + + protected boolean handleMetaYear(ImportBedHeight obj, String line) { + Matcher m = META_YEAR.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + try { + obj.setYear(Integer.valueOf(tmp)); + return true; + } + catch (NumberFormatException e) { + log.warn("Error while parsing year!", e); + } + } + + return false; + } + + + protected boolean handleMetaTimeInterval(ImportBedHeight obj, String line) { + Matcher m = META_TIMEINTERVAL.matcher(line); + + if (m.matches()) { + String lo = m.group(1); + String up = m.group(2); + + log.debug("Found time interval: " + lo + " - " + up); + + try { + int lower = Integer.valueOf(lo); + int upper = Integer.valueOf(up); + + Date fromYear = getDateFromYear(lower); + Date toYear = getDateFromYear(upper); + + obj.setTimeInterval(new ImportTimeInterval(fromYear, toYear)); + } + catch (NumberFormatException e) { + log.warn("Error while parsing timeinterval!", e); + } + + return true; + } + + return false; + } + + + protected boolean handleMetaSoundingWidth(ImportBedHeight obj, String line) { + Matcher m = META_SOUNDING_WIDTH.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + try { + obj.setSoundingWidth(Integer.valueOf(tmp)); + return true; + } + catch (NumberFormatException e) { + log.warn("Error while parsing sounding width!", e); + } + } + + return false; + } + + + protected boolean handleMetaComment(ImportBedHeight obj, String line) { + Matcher m = META_COMMENTS.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + + obj.setDescription(tmp); + + return true; + } + + return false; + } + + + protected boolean handleMetaEvaluationBy( + ImportBedHeight obj, + String line + ) { + Matcher m = META_EVALUATION_BY.matcher(line); + + if (m.matches()) { + String tmp = m.group(1); + tmp = tmp.replace(";", ""); + + obj.setEvaluationBy(tmp); + + return true; + } + + return false; + } + + + protected boolean handleMetaRange(ImportBedHeight obj, String line) { + Matcher m = META_RANGE.matcher(line); + + if (m.matches() && m.groupCount() >= 2) { + String a = m.group(1).replace(";", ""); + String b = m.group(2).replace(";", ""); + + try { + BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue()); + BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue()); + + obj.setRange(new ImportRange(lower, upper)); + + return true; + } + catch (ParseException e) { + log.warn("Error while parsing range!", e); + } + } + + return false; + } + + + protected boolean handleMetaType(ImportBedHeight obj, String line) { + Matcher m = META_TYPE.matcher(line); + + if (m.matches()) { + String tmp = m.group(1).replace(";", ""); + + obj.setType(new ImportBedHeightType( + BedHeightType.getBedHeightName(tmp), + tmp)); + + return true; + } + + return false; + } + + + protected boolean handleMetaLocationSystem( + ImportBedHeight obj, + String line + ) { + Matcher m = META_LOCATION_SYSTEM.matcher(line); + + if (m.matches()) { + String tmp = m.group(1).replace(";", ""); + + obj.setLocationSystem(new ImportLocationSystem(tmp, tmp)); + + return true; + } + + return false; + } + + + protected boolean handleMetaCurElevationModel( + ImportBedHeight obj, + String line + ) { + Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line); + + if (m.matches()) { + String name = m.group(1); + String num = m.group(2); + String unit = m.group(3); + + obj.setCurElevationModel(new ImportElevationModel( + name + " " + num, + new ImportUnit(unit) + )); + + return true; + } + + return false; + } + + + protected boolean handleMetaOldElevationModel( + ImportBedHeight obj, + String line + ) { + Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line); + + if (m.matches()) { + String name = m.group(1); + String num = m.group(2); + String unit = m.group(3); + + obj.setOldElevationModel(new ImportElevationModel( + name + " " + num, + new ImportUnit(unit) + )); + + return true; + } + + return false; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightSingleParser.java Thu Apr 12 12:50:49 2012 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/BedHeightSingleParser.java Fri Apr 13 07:24:55 2012 +0000 @@ -1,343 +1,36 @@ package de.intevation.flys.importer.parsers; -import java.io.File; import java.math.BigDecimal; -import java.text.NumberFormat; import java.text.ParseException; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.io.IOException; -import java.io.LineNumberReader; -import java.io.FileInputStream; -import java.io.InputStreamReader; import org.apache.log4j.Logger; +import de.intevation.flys.importer.ImportBedHeight; import de.intevation.flys.importer.ImportBedHeightSingle; import de.intevation.flys.importer.ImportBedHeightSingleValue; -import de.intevation.flys.importer.ImportBedHeightType; -import de.intevation.flys.importer.ImportElevationModel; -import de.intevation.flys.importer.ImportLocationSystem; -import de.intevation.flys.importer.ImportRange; -import de.intevation.flys.importer.ImportUnit; -import de.intevation.flys.model.BedHeightType; -public class BedHeightSingleParser { - - public static final String ENCODING = "ISO-8859-1"; - - public static final Locale DEFAULT_LOCALE = Locale.GERMAN; - - public static final String START_META_CHAR = "#"; - public static final String SEPERATOR_CHAR = ";"; - - public static final Pattern META_YEAR = - Pattern.compile("^Jahr: (\\d*).*"); - - public static final Pattern META_TYPE = - Pattern.compile("^Aufnahmeart: (.*).*"); - - public static final Pattern META_LOCATION_SYSTEM = - Pattern.compile("^Lagesystem: (.*).*"); - - public static final Pattern META_CUR_ELEVATION_SYSTEM = - Pattern.compile("^H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); - - public static final Pattern META_OLD_ELEVATION_SYSTEM = - Pattern.compile("^urspr.ngliches H.hensystem:\\s(\\w++) (.* )??\\[(.*)\\].*"); - - public static final Pattern META_SOUNDING_WIDTH = - Pattern.compile("^ausgewertete Peilbreite: (\\d*).*"); - - public static final Pattern META_RANGE = - Pattern.compile("^Strecke:\\D*(\\d++.\\d*)-(\\d++.\\d*).*"); - - public static final Pattern META_EVALUATION_BY = - Pattern.compile("^Auswerter: (.*).*"); - - public static final Pattern META_COMMENTS = - Pattern.compile("^Weitere Bemerkungen: (.*).*"); +public class BedHeightSingleParser extends BedHeightParser { private static final Logger log = Logger.getLogger(BedHeightSingleParser.class); - protected static NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); - - - protected List<ImportBedHeightSingle> bedHeights; - - - public BedHeightSingleParser() { - bedHeights = new ArrayList<ImportBedHeightSingle>(); - } - - - public List<ImportBedHeightSingle> getBedHeights() { - return bedHeights; - } - - - public void parse(File file) throws IOException { - log.info("Parsing bed height single file '" + file + "'"); - - ImportBedHeightSingle obj = new ImportBedHeightSingle(file.getName()); - - LineNumberReader in = null; - try { - in = - new LineNumberReader( - new InputStreamReader( - new FileInputStream(file), ENCODING)); - - String line = null; - while ((line = in.readLine()) != null) { - if ((line = line.trim()).length() == 0) { - continue; - } - - if (line.startsWith(START_META_CHAR)) { - handleMetaLine(obj, line); - } - else { - handleDataLine(obj, line); - } - } - - log.info("File contained " + obj.getValues().size() + " values."); - bedHeights.add(obj); - } - finally { - if (in != null) { - in.close(); - } - } - } - - - protected void handleMetaLine(ImportBedHeightSingle obj, String line) { - String meta = stripMetaLine(line); - if (handleMetaYear(obj, meta)) { - return; - } - else if (handleMetaSoundingWidth(obj, meta)) { - return; - } - else if (handleMetaComment(obj, meta)) { - return; - } - else if (handleMetaEvaluationBy(obj, meta)) { - return; - } - else if (handleMetaRange(obj, meta)) { - return; - } - else if (handleMetaType(obj, meta)) { - return; - } - else if (handleMetaLocationSystem(obj, meta)) { - return; - } - else if (handleMetaCurElevationModel(obj, meta)) { - return; - } - else if (handleMetaOldElevationModel(obj, meta)) { - return; - } - else { - log.warn("Meta line did not match any known type: " + line); - } - } - - - protected boolean handleMetaYear(ImportBedHeightSingle obj, String line) { - Matcher m = META_YEAR.matcher(line); - - if (m.matches()) { - String tmp = m.group(1); - - try { - obj.setYear(Integer.valueOf(tmp)); - return true; - } - catch (NumberFormatException e) { - log.warn("Error while parsing year!", e); - } - } - - return false; - } - - - protected boolean handleMetaSoundingWidth(ImportBedHeightSingle obj, String line) { - Matcher m = META_SOUNDING_WIDTH.matcher(line); - - if (m.matches()) { - String tmp = m.group(1); - - try { - obj.setSoundingWidth(Integer.valueOf(tmp)); - return true; - } - catch (NumberFormatException e) { - log.warn("Error while parsing sounding width!", e); - } - } - - return false; + @Override + protected ImportBedHeight newImportBedHeight(String description) { + return new ImportBedHeightSingle(description); } - protected boolean handleMetaComment(ImportBedHeightSingle obj, String line) { - Matcher m = META_COMMENTS.matcher(line); - - if (m.matches()) { - String tmp = m.group(1); - - obj.setDescription(tmp); - - return true; - } - - return false; - } - - - protected boolean handleMetaEvaluationBy( - ImportBedHeightSingle obj, - String line - ) { - Matcher m = META_EVALUATION_BY.matcher(line); - - if (m.matches()) { - String tmp = m.group(1); - tmp = tmp.replace(";", ""); - - obj.setEvaluationBy(tmp); - - return true; - } - - return false; - } - - - protected boolean handleMetaRange(ImportBedHeightSingle obj, String line) { - Matcher m = META_RANGE.matcher(line); - - if (m.matches() && m.groupCount() >= 2) { - String a = m.group(1).replace(";", ""); - String b = m.group(2).replace(";", ""); - - try { - BigDecimal lower = new BigDecimal(nf.parse(a).doubleValue()); - BigDecimal upper = new BigDecimal(nf.parse(b).doubleValue()); - - obj.setRange(new ImportRange(lower, upper)); - - return true; - } - catch (ParseException e) { - log.warn("Error while parsing range!", e); - } - } - - return false; - } - - - protected boolean handleMetaType(ImportBedHeightSingle obj, String line) { - Matcher m = META_TYPE.matcher(line); - - if (m.matches()) { - String tmp = m.group(1).replace(";", ""); - - obj.setType(new ImportBedHeightType( - BedHeightType.getBedHeightName(tmp), - tmp)); - return true; - } - - return false; - } - - - protected boolean handleMetaLocationSystem( - ImportBedHeightSingle obj, - String line - ) { - Matcher m = META_LOCATION_SYSTEM.matcher(line); - - if (m.matches()) { - String tmp = m.group(1).replace(";", ""); - - obj.setLocationSystem(new ImportLocationSystem(tmp, tmp)); - - return true; - } - - return false; - } - - - protected boolean handleMetaCurElevationModel( - ImportBedHeightSingle obj, - String line - ) { - Matcher m = META_CUR_ELEVATION_SYSTEM.matcher(line); - - if (m.matches()) { - String name = m.group(1); - String num = m.group(2); - String unit = m.group(3); - - obj.setCurElevationModel(new ImportElevationModel( - name + " " + num, - new ImportUnit(unit) - )); - - return true; - } - - return false; - } - - - protected boolean handleMetaOldElevationModel( - ImportBedHeightSingle obj, - String line - ) { - Matcher m = META_OLD_ELEVATION_SYSTEM.matcher(line); - - if (m.matches()) { - String name = m.group(1); - String num = m.group(2); - String unit = m.group(3); - - obj.setOldElevationModel(new ImportElevationModel( - name + " " + num, - new ImportUnit(unit) - )); - - return true; - } - - return false; - } - - - protected void handleDataLine(ImportBedHeightSingle obj, String line) { + @Override + protected void handleDataLine(ImportBedHeight obj, String line) { String[] values = line.split(SEPERATOR_CHAR); if (values == null || values.length < 6) { @@ -345,10 +38,9 @@ return; } - try { ImportBedHeightSingleValue value = new ImportBedHeightSingleValue( - obj, + (ImportBedHeightSingle) obj, new BigDecimal(nf.parse(values[0]).doubleValue()), new BigDecimal(nf.parse(values[1]).doubleValue()), new BigDecimal(nf.parse(values[2]).doubleValue()), @@ -363,17 +55,5 @@ log.warn("Error while parsing data row.", e); } } - - - protected static String stripMetaLine(String line) { - String tmp = line.substring(1, line.length()-1); - - if (tmp.startsWith(" ")) { - return tmp.substring(1, tmp.length()-1); - } - else { - return tmp; - } - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :