package de.hftstuttgart.utils; import de.hftstuttgart.rest.v1.unittest.UnitTestUpload; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexUtil { public enum ConfigType { PROFESSOR, STUDENT, } private static final Logger LOG = LogManager.getLogger(RegexUtil.class); public static Matcher findModocotStudentConfig(InputStream is) { return findModoctConfig(is, ConfigType.STUDENT); } public static Matcher findModocotProfessorConfig(InputStream is) { return findModoctConfig(is, ConfigType.PROFESSOR); } public static Matcher findModoctConfig(InputStream is, ConfigType configType) { Pattern pattern; switch (configType) { case PROFESSOR: pattern = Pattern.compile(UnitTestUpload.modocotTestConfigRegex); break; case STUDENT: pattern = Pattern.compile(UnitTestUpload.modocotDueConfigRegex); break; default: String msg = String.format("unknown config type: %s", configType.name()); LOG.error(msg); throw new RuntimeException(msg); } Matcher config = null; LOG.debug("reading config file"); // open received file in a try-with try (BufferedReader br = new BufferedReader( new InputStreamReader( is))) { String line; // as long as we haven't found a configuration and have lines left, search while (config == null && (line = br.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (matcher.matches()) { LOG.debug(String.format("found modocot line: %s", line)); config = matcher; } } } catch (IOException e) { LOG.error("Error while reading repo config", e); } finally { if (config == null) { throw new RuntimeException("couldn't find repo config for clone"); } } return config; } }