sascha@177: package de.intevation.flys.importer; sascha@177: sascha@177: import java.io.File; sascha@177: sascha@177: import java.util.List; sascha@177: import java.util.ArrayList; sascha@177: sascha@177: import java.util.regex.Pattern; sascha@177: import java.util.regex.Matcher; sascha@177: sascha@177: import java.io.IOException; sascha@177: import java.io.LineNumberReader; sascha@177: import java.io.FileInputStream; sascha@177: import java.io.InputStreamReader; sascha@177: sascha@177: import org.apache.log4j.Logger; sascha@177: sascha@177: import de.intevation.flys.utils.FileTools; sascha@177: sascha@177: public class InfoGewParser sascha@177: { sascha@177: private static Logger log = Logger.getLogger(InfoGewParser.class); sascha@177: sascha@177: public static final String ENCODING = "ISO-8859-1"; sascha@177: sascha@177: public static final Pattern GEWAESSER = sascha@177: Pattern.compile("^\\s*Gew\u00e4sser\\s*:\\s*(.+)"); sascha@177: sascha@177: public static final Pattern WST_DATEI = sascha@177: Pattern.compile("^\\s*WSTDatei\\s*:\\s*(.+)"); sascha@177: sascha@177: public static final Pattern BB_INFO = sascha@177: Pattern.compile("^\\s*B\\+B-Info\\s*:\\s*(.+)"); sascha@177: sascha@177: protected ArrayList rivers; sascha@177: sascha@765: protected AnnotationClassifier annotationClassifier; sascha@765: sascha@177: public InfoGewParser() { sascha@765: this(null); sascha@765: } sascha@765: sascha@765: public InfoGewParser(AnnotationClassifier annotationClassifier) { sascha@177: rivers = new ArrayList(); sascha@765: this.annotationClassifier = annotationClassifier; sascha@177: } sascha@177: sascha@180: public List getRivers() { sascha@180: return rivers; sascha@180: } sascha@180: sascha@177: public static final String normalize(String f) { sascha@177: return f.replace("\\", "/").replace("/", File.separator); sascha@177: } sascha@177: sascha@177: public void parse(File file) throws IOException { sascha@177: sascha@177: LineNumberReader in = null; sascha@177: sascha@177: File root = file.getParentFile(); sascha@177: sascha@177: try { sascha@177: in = sascha@177: new LineNumberReader( sascha@177: new InputStreamReader( sascha@177: new FileInputStream(file), ENCODING)); sascha@177: sascha@177: String line = null; sascha@177: sascha@177: String riverName = null; sascha@177: File wstFile = null; sascha@177: File bbInfoFile = null; sascha@177: sascha@177: while ((line = in.readLine()) != null) { sascha@177: if ((line = line.trim()).length() == 0) { sascha@177: continue; sascha@177: } sascha@177: Matcher m = GEWAESSER.matcher(line); sascha@177: sascha@177: if (m.matches()) { sascha@177: String river = m.group(1); sascha@177: log.info("Found river '" + river + "'"); sascha@177: if (riverName != null) { sascha@766: rivers.add(new ImportRiver( sascha@766: riverName, sascha@766: wstFile, sascha@766: bbInfoFile, sascha@766: annotationClassifier)); sascha@177: } sascha@177: riverName = river; sascha@177: wstFile = null; sascha@177: bbInfoFile = null; sascha@177: } sascha@177: else if ((m = WST_DATEI.matcher(line)).matches()) { sascha@177: String wstFilename = m.group(1); sascha@177: File wst = new File(wstFilename = normalize(wstFilename)); sascha@177: if (!wst.isAbsolute()) { sascha@177: wst = new File(root, wstFilename); sascha@177: } sascha@177: wst = FileTools.repair(wst); sascha@177: log.info("Found wst file '" + wst + "'"); sascha@177: if (!wst.isFile() || !wst.canRead()) { sascha@177: log.warn("cannot access WST file '" + wstFilename + "'"); sascha@177: continue; sascha@177: } sascha@177: wstFile = wst; sascha@177: } sascha@177: else if ((m = BB_INFO.matcher(line)).matches()) { sascha@177: //TODO: Make it relative to the wst file. sascha@177: String bbInfo = m.group(1); sascha@177: bbInfoFile = new File(normalize(bbInfo)); sascha@177: } sascha@177: } sascha@177: if (riverName != null) { sascha@766: rivers.add(new ImportRiver( sascha@766: riverName, sascha@766: wstFile, sascha@766: bbInfoFile, sascha@766: annotationClassifier)); sascha@177: } sascha@177: } sascha@177: finally { sascha@177: if (in != null) { sascha@177: in.close(); sascha@177: } sascha@177: } sascha@184: sascha@184: for (ImportRiver river: rivers) { sascha@186: river.parseDependencies(); sascha@184: } sascha@177: } sascha@177: } sascha@177: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :