# HG changeset patch # User Sascha L. Teichmann # Date 1300383837 0 # Node ID cf8cbcb6a10d08c00e62c64c0ed32105dafe91a4 # Parent a60edcfe5f5314a9786259b5c5727c2a8c13a5e1 Added parser to read *.KM files. flys-backend/trunk@1506 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/ChangeLog --- a/flys-backend/ChangeLog Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/ChangeLog Thu Mar 17 17:43:57 2011 +0000 @@ -1,3 +1,17 @@ +2011-03-17 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/importer/AnnotationsParser.java: + New. Added parser to read *.KM files. + + * src/main/java/de/intevation/flys/importer/ImportPosition.java, + src/main/java/de/intevation/flys/importer/PegelGltParser.java, + src/main/java/de/intevation/flys/importer/ImportRiver.java, + src/main/java/de/intevation/flys/importer/ImportAnnotation.java, + src/main/java/de/intevation/flys/importer/ImportRange.java, + src/main/java/de/intevation/flys/importer/InfoGewParser.java, + src/main/java/de/intevation/flys/importer/ImportAttribute.java: + Adjusted to load the annotations from *.KM files. + 2011-03-17 Sascha L. Teichmann * src/main/java/de/intevation/flys/importer/ImportPosition.java, diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/AnnotationsParser.java Thu Mar 17 17:43:57 2011 +0000 @@ -0,0 +1,136 @@ +package de.intevation.flys.importer; + +import java.util.HashMap; +import java.util.TreeSet; +import java.util.List; +import java.util.ArrayList; + +import java.io.IOException; +import java.io.File; +import java.io.LineNumberReader; +import java.io.InputStreamReader; +import java.io.FileInputStream; + +import org.apache.log4j.Logger; + +import de.intevation.flys.utils.FileTools; + +public class AnnotationsParser +{ + private static Logger log = Logger.getLogger(AnnotationsParser.class); + + public static final String ENCODING = "ISO-8859-1"; + + public static final String [] TO_SCAN = { + "Basisdaten", + "Streckendaten" + }; + + protected HashMap attributes; + protected HashMap positions; + protected TreeSet annotations; + + public AnnotationsParser() { + attributes = new HashMap(); + positions = new HashMap(); + annotations = new TreeSet(); + } + + public void parseFile(File file) throws IOException { + log.info("parsing km file: '" + file + "'"); + 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 + || line.startsWith("*")) { + continue; + } + + String [] parts = line.split("\\s*;\\s*"); + + if (parts.length < 3) { + log.warn("not enough columns in line " + + in.getLineNumber()); + continue; + } + + ImportPosition position = positions.get(parts[0]); + if (position == null) { + position = new ImportPosition(parts[0]); + positions.put(parts[0], position); + } + + ImportAttribute attribute = attributes.get(parts[1]); + if (attribute == null) { + attribute = new ImportAttribute(parts[1]); + attributes.put(parts[1], attribute); + } + + String [] r = parts[2].replace(",", ".").split("\\s*#\\s*"); + + Double from, to; + + try { + from = Double.valueOf(r[0]); + to = r.length < 2 ? null : Double.valueOf(r[1]); + if (to != null && from > to) { + Double t = from; from = to; to = t; + } + } + catch (NumberFormatException nfe) { + log.warn("invalid number in line " + in.getLineNumber()); + continue; + } + + ImportRange range = new ImportRange(from, to); + + ImportAnnotation annotation = new ImportAnnotation( + attribute, position, range); + + if (!annotations.add(annotation)) { + log.debug("duplicated annotation in line " + + in.getLineNumber()); + } + } + } + finally { + if (in != null) { + in.close(); + } + } + } + + public void parse(File root) throws IOException { + + for (String toScan: TO_SCAN) { + File directory = FileTools.repair(new File(root, toScan)); + if (!directory.isDirectory()) { + log.warn("'" + directory + "' is not a directory."); + continue; + } + File [] files = directory.listFiles(); + if (files == null) { + log.warn("cannot list directory '" + directory + "'"); + continue; + } + + for (File file: files) { + if (file.isFile() && file.canRead() + && file.getName().toLowerCase().endsWith(".km")) { + parseFile(file); + } + } + } // for all directories to scan + } + + public List getAnnotations() { + return new ArrayList(annotations); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAnnotation.java Thu Mar 17 17:43:57 2011 +0000 @@ -1,6 +1,7 @@ package de.intevation.flys.importer; public class ImportAnnotation +implements Comparable { protected ImportAttribute attribute; protected ImportPosition position; @@ -9,6 +10,33 @@ public ImportAnnotation() { } + public ImportAnnotation( + ImportAttribute attribute, + ImportPosition position, + ImportRange range + ) { + this.attribute = attribute; + this.position = position; + this.range = range; + } + + public int compareTo(ImportAnnotation other) { + int d = attribute.compareTo(other.attribute); + if (d != 0) { + return d; + } + + if ((d = position.compareTo(other.position)) != 0) { + return d; + } + + if ((d = range.compareTo(other.range)) != 0) { + return d; + } + + return 0; + } + public ImportAttribute getAttribute() { return attribute; } diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportAttribute.java Thu Mar 17 17:43:57 2011 +0000 @@ -1,12 +1,17 @@ package de.intevation.flys.importer; public class ImportAttribute +implements Comparable { protected String value; public ImportAttribute() { } + public ImportAttribute(String value) { + this.value = value; + } + public String getValue() { return value; } @@ -15,6 +20,10 @@ this.value = value; } + public int compareTo(ImportAttribute other) { + return value.compareTo(other.value); + } + @Override public boolean equals(Object other) { if (other == this) return true; diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportPosition.java Thu Mar 17 17:43:57 2011 +0000 @@ -1,6 +1,7 @@ package de.intevation.flys.importer; public class ImportPosition +implements Comparable { protected String value; @@ -11,6 +12,10 @@ this.value = value; } + public int compareTo(ImportPosition other) { + return value.compareTo(other.value); + } + public String getValue() { return value; } @@ -18,18 +23,6 @@ public void setValue(String value) { this.value = value; } - - @Override - public boolean equals(Object other) { - if (other == this) return true; - if (!(other instanceof ImportPosition)) return false; - return value.equals(((ImportPosition)other).value); - } - - @Override - public int hashCode() { - return value.hashCode(); - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRange.java Thu Mar 17 17:43:57 2011 +0000 @@ -1,6 +1,7 @@ package de.intevation.flys.importer; public class ImportRange +implements Comparable { public Double from; public Double to; @@ -9,6 +10,27 @@ } public ImportRange(Double from, Double to) { + this.from = from; + this.to = to; + } + + private static final int compare(Double a, Double b) { + if (a == null && b == null) { + return 0; + } + if (a == null && b != null) { + return -1; + } + if (a != null && b == null) { + return +1; + } + return a.compareTo(b); + } + + public int compareTo(ImportRange other) { + int cmp = compare(from, other.from); + if (cmp != 0) return cmp; + return compare(to, other.to); } public Double getFrom() { diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java Thu Mar 17 17:43:57 2011 +0000 @@ -23,6 +23,8 @@ protected List gauges; + protected List annotations; + public ImportRiver() { } @@ -56,6 +58,11 @@ this.bbInfoFile = bbInfoFile; } + public void parseDependencies() throws IOException { + parseGauges(); + parseAnnotations(); + } + public void parseGauges() throws IOException { File gltFile = new File(wstFile.getParentFile(), PEGEL_GLT); gltFile = FileTools.repair(gltFile); @@ -70,5 +77,13 @@ gauges = pgltp.getGauges(); } + + public void parseAnnotations() throws IOException { + File riverDir = wstFile.getParentFile().getParentFile(); + AnnotationsParser aparser = new AnnotationsParser(); + aparser.parse(riverDir); + + annotations = aparser.getAnnotations(); + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/InfoGewParser.java Thu Mar 17 17:43:57 2011 +0000 @@ -112,7 +112,7 @@ } for (ImportRiver river: rivers) { - river.parseGauges(); + river.parseDependencies(); } } } diff -r a60edcfe5f53 -r cf8cbcb6a10d flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java --- a/flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java Thu Mar 17 16:12:39 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/PegelGltParser.java Thu Mar 17 17:43:57 2011 +0000 @@ -19,7 +19,7 @@ public class PegelGltParser { - private static Logger log = Logger.getLogger(InfoGewParser.class); + private static Logger log = Logger.getLogger(PegelGltParser.class); public static final String ENCODING = "ISO-8859-1"; @@ -94,6 +94,5 @@ } } } - } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :