RegexUtil.java 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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:
Lukas Wiest's avatar
Lukas Wiest committed
35
                pattern = Pattern.compile(UnitTestUpload.TESTCONFIGREGEX);
36
37
38
                break;

            case STUDENT:
Lukas Wiest's avatar
Lukas Wiest committed
39
                pattern = Pattern.compile(UnitTestUpload.SUBMISSIONCONFIGREGEX);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                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;
    }

}