sascha@186: package de.intevation.flys.importer; sascha@186: sascha@186: import java.util.HashMap; sascha@186: import java.util.TreeSet; sascha@186: import java.util.List; sascha@186: import java.util.ArrayList; sascha@186: sascha@186: import java.io.IOException; sascha@186: import java.io.File; sascha@186: import java.io.LineNumberReader; sascha@186: import java.io.InputStreamReader; sascha@186: import java.io.FileInputStream; sascha@186: sascha@189: import java.math.BigDecimal; sascha@189: sascha@186: import org.apache.log4j.Logger; sascha@186: sascha@186: import de.intevation.flys.utils.FileTools; sascha@186: sascha@186: public class AnnotationsParser sascha@186: { sascha@186: private static Logger log = Logger.getLogger(AnnotationsParser.class); sascha@186: sascha@186: public static final String ENCODING = "ISO-8859-1"; sascha@186: sascha@186: public static final String [] TO_SCAN = { sascha@186: "Basisdaten", sascha@186: "Streckendaten" sascha@186: }; sascha@186: sascha@186: protected HashMap attributes; sascha@186: protected HashMap positions; sascha@186: protected TreeSet annotations; sascha@186: sascha@186: public AnnotationsParser() { sascha@186: attributes = new HashMap(); sascha@186: positions = new HashMap(); sascha@186: annotations = new TreeSet(); sascha@186: } sascha@186: sascha@186: public void parseFile(File file) throws IOException { sascha@186: log.info("parsing km file: '" + file + "'"); sascha@186: LineNumberReader in = null; sascha@186: try { sascha@186: in = sascha@186: new LineNumberReader( sascha@186: new InputStreamReader( sascha@186: new FileInputStream(file), ENCODING)); sascha@186: sascha@186: String line = null; sascha@186: while ((line = in.readLine()) != null) { sascha@186: if ((line = line.trim()).length() == 0 sascha@186: || line.startsWith("*")) { sascha@186: continue; sascha@186: } sascha@186: sascha@186: String [] parts = line.split("\\s*;\\s*"); sascha@186: sascha@186: if (parts.length < 3) { sascha@186: log.warn("not enough columns in line " sascha@186: + in.getLineNumber()); sascha@186: continue; sascha@186: } sascha@186: sascha@186: ImportPosition position = positions.get(parts[0]); sascha@186: if (position == null) { sascha@186: position = new ImportPosition(parts[0]); sascha@186: positions.put(parts[0], position); sascha@186: } sascha@186: sascha@186: ImportAttribute attribute = attributes.get(parts[1]); sascha@186: if (attribute == null) { sascha@186: attribute = new ImportAttribute(parts[1]); sascha@186: attributes.put(parts[1], attribute); sascha@186: } sascha@186: sascha@186: String [] r = parts[2].replace(",", ".").split("\\s*#\\s*"); sascha@186: sascha@189: BigDecimal from, to; sascha@186: sascha@186: try { sascha@189: from = new BigDecimal(r[0]); sascha@189: to = r.length < 2 ? null : new BigDecimal(r[1]); sascha@189: if (to != null && from.compareTo(to) > 0) { sascha@189: BigDecimal t = from; from = to; to = t; sascha@186: } sascha@186: } sascha@186: catch (NumberFormatException nfe) { sascha@186: log.warn("invalid number in line " + in.getLineNumber()); sascha@186: continue; sascha@186: } sascha@186: sascha@186: ImportRange range = new ImportRange(from, to); sascha@186: sascha@186: ImportAnnotation annotation = new ImportAnnotation( sascha@186: attribute, position, range); sascha@186: sascha@186: if (!annotations.add(annotation)) { sascha@186: log.debug("duplicated annotation in line " sascha@186: + in.getLineNumber()); sascha@186: } sascha@186: } sascha@186: } sascha@186: finally { sascha@186: if (in != null) { sascha@186: in.close(); sascha@186: } sascha@186: } sascha@186: } sascha@186: sascha@186: public void parse(File root) throws IOException { sascha@186: sascha@186: for (String toScan: TO_SCAN) { sascha@186: File directory = FileTools.repair(new File(root, toScan)); sascha@186: if (!directory.isDirectory()) { sascha@186: log.warn("'" + directory + "' is not a directory."); sascha@186: continue; sascha@186: } sascha@186: File [] files = directory.listFiles(); sascha@186: if (files == null) { sascha@186: log.warn("cannot list directory '" + directory + "'"); sascha@186: continue; sascha@186: } sascha@186: sascha@186: for (File file: files) { sascha@186: if (file.isFile() && file.canRead() sascha@186: && file.getName().toLowerCase().endsWith(".km")) { sascha@186: parseFile(file); sascha@186: } sascha@186: } sascha@186: } // for all directories to scan sascha@186: } sascha@186: sascha@186: public List getAnnotations() { sascha@186: return new ArrayList(annotations); sascha@186: } sascha@186: } sascha@186: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :