teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.wsplgen; ingo@1147: ingo@1147: import java.util.regex.Matcher; ingo@1147: import java.util.regex.Pattern; ingo@1147: ingo@1147: import org.apache.log4j.Logger; ingo@1147: teichmann@5831: import org.dive4elements.river.artifacts.model.map.WSPLGENCalculation; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WSPLGENJob; ingo@1147: ingo@1147: ingo@1147: public class ProblemObserver extends JobObserver { ingo@1147: ingo@1147: private static Logger logger = Logger.getLogger(ProblemObserver.class); ingo@1147: ingo@1147: ingo@1147: public static final Pattern WSPLGEN_ERROR_START = Pattern.compile( ingo@1648: ".*->(.*Fehler)(\\s*\\((\\d+)\\).*)*", ingo@1147: Pattern.DOTALL); ingo@1147: ingo@1147: public static final Pattern WSPLGEN_ERROR_END = Pattern.compile( ingo@1648: ".*<-(.*Fehler).*", ingo@1147: Pattern.DOTALL); ingo@1147: ingo@1147: public static final Pattern WSPLGEN_WARNING_START = Pattern.compile( ingo@1147: ".*->Warnung\\s*\\((\\d+)\\).*", ingo@1147: Pattern.DOTALL); ingo@1147: ingo@1147: public static final Pattern WSPLGEN_WARNING_END = Pattern.compile( ingo@1147: ".*<-Warnung .*", ingo@1147: Pattern.DOTALL); ingo@1147: ingo@1147: ingo@1147: protected int error; ingo@1147: protected int warning; ingo@1147: ingo@1149: protected WSPLGENCalculation calculation; ingo@1147: ingo@1147: ingo@1147: public ProblemObserver(WSPLGENJob job) { ingo@1147: super(job); ingo@1149: error = -1; ingo@1149: warning = -1; ingo@1149: calculation = job.getCalculation(); ingo@1147: } ingo@1147: ingo@1147: ingo@1147: public void run() { ingo@1147: logger.debug("Start observation..."); ingo@1147: ingo@1147: super.run(); ingo@1147: } ingo@1147: ingo@1147: ingo@1147: @Override ingo@1147: protected void prepareRegexes() { ingo@1147: // do nothing ingo@1147: } ingo@1147: ingo@1147: ingo@1147: @Override ingo@1147: protected void update(String log) { ingo@1147: Matcher startError = WSPLGEN_ERROR_START.matcher(log); ingo@1147: if (startError.matches()) { ingo@1648: if (startError.groupCount() >= 2) { ingo@1649: String tmp = startError.group(3); ingo@1648: ingo@1648: if (tmp != null && tmp.length() > 0) { ingo@1648: error = Integer.parseInt(tmp); ingo@1648: } ingo@1648: else error = 1; ingo@1648: } ingo@1648: else { ingo@1648: error = 1; ingo@1648: } ingo@1648: ingo@1147: return; ingo@1147: } ingo@1147: ingo@1147: Matcher endError = WSPLGEN_ERROR_END.matcher(log); ingo@1147: if (endError.matches()) { ingo@1147: error = -1; ingo@1147: } ingo@1147: ingo@1147: if (error > 0) { ingo@1149: calculation.addError(new Integer(error), log); ingo@1147: } ingo@1147: ingo@1147: Matcher startWarning = WSPLGEN_WARNING_START.matcher(log); ingo@1147: if (startWarning.matches()) { ingo@1147: warning = Integer.parseInt(startWarning.group(1)); ingo@1147: return; ingo@1147: } ingo@1147: ingo@1147: Matcher endWarning = WSPLGEN_WARNING_END.matcher(log); ingo@1147: if (endWarning.matches()) { ingo@1147: warning = -1; ingo@1147: } ingo@1147: ingo@1147: if (warning > 0) { ingo@1149: calculation.addWarning(new Integer(warning), log); ingo@1147: } ingo@1147: } ingo@1147: ingo@1147: ingo@1147: public int numErrors() { ingo@1149: return calculation.numErrors(); ingo@1147: } ingo@1147: ingo@1147: ingo@1147: public int numWarnings() { ingo@1149: return calculation.numWarnings(); ingo@1147: } ingo@1147: } ingo@1147: // vim:set ts=4 sw=4 si et sta sts=5 fenc=utf-8 :