UntisCommonParser extends BaseParser { static void handleClasses(JSONObject data, Substitution v, String klassen, List<String> allClasses) throws JSONException, CredentialInvalidException { List<String> affectedClasses; if (data.has(PARAM_ALL_CLASSES_COURSES)) { JSONArray arr = data.getJSONArray(PARAM_ALL_CLASSES_COURSES); for (int i = 0; i < arr.length(); i++) { String s = arr.getString(i); if (klassen.equals(s)) { v.getClasses().addAll(allClasses); return; } } } Pattern singlePattern = Pattern.compile("(\\d{1,2})"); Matcher singleMatcher = singlePattern.matcher(klassen); Pattern rangePattern = Pattern.compile("(\\d+) ?- ?(\\d+)"); Matcher rangeMatcher = rangePattern.matcher(klassen); boolean singleClassLooksLikeRange = false; if (allClasses != null) { for (String klass : allClasses) { if (rangePattern.matcher(klass).matches()) { singleClassLooksLikeRange = true; break; } } } Pattern pattern2 = Pattern.compile("^(\\d+).*"); if (rangeMatcher.matches() && !singleClassLooksLikeRange) { affectedClasses = new ArrayList<>(); int min = Integer.parseInt(rangeMatcher.group(1)); int max = Integer.parseInt(rangeMatcher.group(2)); for (String klasse : allClasses) { Matcher matcher2 = pattern2.matcher(klasse); if (matcher2.matches()) { int num = Integer.parseInt(matcher2.group(1)); if (min <= num && num <= max) affectedClasses.add(klasse); } } } else if (singleMatcher.matches()) { affectedClasses = new ArrayList<>(); int grade = Integer.parseInt(singleMatcher.group(1)); for (String klasse : allClasses) { Matcher matcher2 = pattern2.matcher(klasse); if (matcher2.matches() && grade == Integer.parseInt(matcher2.group(1))) { affectedClasses.add(klasse); } } } else { if (data.optBoolean(PARAM_CLASSES_SEPARATED, true) && data.optBoolean("classes_separated", true)) { affectedClasses = Arrays.asList(klassen.split(", ")); } else { affectedClasses = new ArrayList<>(); if (allClasses.contains(klassen)) { affectedClasses.add(klassen); } if (klassen.matches("([\\d]{1,2}[a-zA-Z]+)+")) { Pattern pattern = Pattern.compile("([\\d]{1,2})([a-zA-Z]+)"); Matcher matcher = pattern.matcher(klassen); while (matcher.find()) { String base = matcher.group(1); for (char letter : matcher.group(2).toCharArray()) { if (allClasses.contains(base + letter)) { affectedClasses.add(base + letter); } } } } else if (singleClassLooksLikeRange && klassen.matches("(\\d{1,2})-(\\d+)")) { Pattern pattern = Pattern.compile("(\\d{1,2})-(\\d+)"); Matcher matcher = pattern.matcher(klassen); if (matcher.find()) { String base = matcher.group(1); for (char number : matcher.group(2).toCharArray()) { if (allClasses.contains(base + "-" + number)) { affectedClasses.add(base + "-" + number); } } } else { affectedClasses.add(klassen); } } else { for (String klasse : allClasses) { StringBuilder regex = new StringBuilder(); for (char character : klasse.toCharArray()) { if (character == '?') { regex.append("\\?"); } else { regex.append(character); } regex.append(".*"); } if (klassen.matches(regex.toString())) { affectedClasses.add(klasse); } } } } } for (String klasse : affectedClasses) { String name = getClassName(klasse, data); if (isValidClass(name, data)) { v.getClasses().add(name); } } } UntisCommonParser(SubstitutionScheduleData scheduleData, CookieProvider cookieProvider); @Override List<String> getAllClasses();  }
@Test public void testHandleClasses() throws JSONException, CredentialInvalidException { JSONObject data = new JSONObject("{\"classesSeparated\": false}"); Substitution subst = new Substitution(); UntisCommonParser.handleClasses(data, subst, "11a", Arrays.asList("11a", "11b", "12a")); assertEquals(new HashSet<>(Arrays.asList("11a")), subst.getClasses()); subst = new Substitution(); UntisCommonParser.handleClasses(data, subst, "1112", Arrays.asList("11", "12")); assertEquals(new HashSet<>(Arrays.asList("11", "12")), subst.getClasses()); subst = new Substitution(); UntisCommonParser.handleClasses(data, subst, "11abc12b", Arrays.asList("11a", "11b", "11c", "12a", "12b", "12c")); assertEquals(new HashSet<>(Arrays.asList("11a", "11b", "11c", "12b")), subst.getClasses()); subst = new Substitution(); UntisCommonParser.handleClasses(data, subst, "1abc2b", Arrays.asList("1a", "1b", "1c", "2a", "2b", "2c")); assertEquals(new HashSet<>(Arrays.asList("1a", "1b", "1c", "2b")), subst.getClasses()); subst = new Substitution(); UntisCommonParser.handleClasses(data, subst, "5-6", Arrays.asList("5a", "5b", "5c", "6a", "6b", "6c", "7a")); assertEquals(new HashSet<>(Arrays.asList("5a", "5b", "5c", "6a", "6b", "6c")), subst.getClasses()); }