Search in sources :

Example 31 with Substitution

use of me.vertretungsplan.objects.Substitution in project substitution-schedule-parser by vertretungsplanme.

the class UntisCommonParser method parseWithExtraLine.

private void parseWithExtraLine(JSONObject data, SubstitutionScheduleDay day, List<String> columns, Element element, String className, String teacherName) {
    Element zeile = null;
    try {
        zeile = element.parent().nextElementSibling();
        if (zeile.select("td") == null) {
            zeile = zeile.nextElementSibling();
        }
        int skipLines = 0;
        while (zeile != null && !zeile.select("td").attr("class").equals("list inline_header")) {
            if (skipLines > 0) {
                skipLines--;
                zeile = zeile.nextElementSibling();
                continue;
            }
            Substitution v = new Substitution();
            String klassen = null;
            String course = null;
            int i = 0;
            for (Element spalte : zeile.select("td")) {
                String text = spalte.text();
                String type = columns.get(i);
                if (isEmpty(text) && !type.equals("teacher")) {
                    i++;
                    continue;
                }
                int skipLinesForThisColumn = 0;
                Element nextLine = zeile.nextElementSibling();
                boolean continueSkippingLines = true;
                while (continueSkippingLines) {
                    if (nextLine != null && nextLine.children().size() == zeile.children().size()) {
                        Element columnInNextLine = nextLine.child(spalte.elementSiblingIndex());
                        if (columnInNextLine.text().replaceAll("\u00A0", "").trim().equals(nextLine.text().replaceAll("\u00A0", "").trim())) {
                            // Continued in the next line
                            text += " " + columnInNextLine.text();
                            skipLinesForThisColumn++;
                            nextLine = nextLine.nextElementSibling();
                        } else {
                            continueSkippingLines = false;
                        }
                    } else {
                        continueSkippingLines = false;
                    }
                }
                if (skipLinesForThisColumn > skipLines)
                    skipLines = skipLinesForThisColumn;
                switch(type) {
                    case "lesson":
                        v.setLesson(text);
                        break;
                    case "subject":
                        handleSubject(v, spalte);
                        if (course != null) {
                            v.setSubject((v.getSubject() != null ? v.getSubject() + " " : "") + course);
                            course = null;
                        }
                        break;
                    case "course":
                        if (v.getSubject() != null) {
                            v.setSubject(v.getSubject() + " " + text);
                        } else {
                            course = text;
                        }
                        break;
                    case "previousSubject":
                        v.setPreviousSubject(text);
                        break;
                    case "type":
                        v.setType(text);
                        v.setColor(colorProvider.getColor(text));
                        break;
                    case "type-entfall":
                        if (text.equals("x")) {
                            v.setType("Entfall");
                            v.setColor(colorProvider.getColor("Entfall"));
                        } else if (v.getType() == null) {
                            v.setType("Vertretung");
                            v.setColor(colorProvider.getColor("Vertretung"));
                        }
                        break;
                    case "room":
                        handleRoom(v, spalte);
                        break;
                    case "teacher":
                        if (text.equals("+")) {
                            v.setType("Eigenverantw. Arbeiten");
                            v.setColor(colorProvider.getColor(v.getType()));
                        } else if (!isEmpty(text) && teacherName == null) {
                            handleTeacher(v, spalte, data);
                        }
                        // otherwise ignore - teacher is in extra line
                        break;
                    case "previousTeacher":
                        v.setPreviousTeachers(splitTeachers(text, data));
                        break;
                    case "desc":
                        v.setDesc(text);
                        break;
                    case "desc-type":
                        v.setDesc(text);
                        String recognizedType = recognizeType(text);
                        v.setType(recognizedType);
                        v.setColor(colorProvider.getColor(recognizedType));
                        break;
                    case "previousRoom":
                        v.setPreviousRoom(text);
                        break;
                    case "substitutionFrom":
                        v.setSubstitutionFrom(text);
                        break;
                    case "teacherTo":
                        v.setTeacherTo(text);
                        break;
                    case "ignore":
                        break;
                    case // used by UntisSubstitutionParser
                    "date":
                        break;
                    case "class":
                        if (className == null) {
                            klassen = getClassName(text, data);
                        }
                        // otherwise ignore - class is in extra line
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown column type: " + type);
                }
                i++;
            }
            if (course != null) {
                v.setSubject(course);
            }
            autoDetectType(data, zeile, v);
            if (className != null) {
                v.getClasses().add(className);
            } else {
                handleClasses(data, v, klassen, getAllClasses());
            }
            if (teacherName != null) {
                v.setTeacher(teacherName);
            }
            if (v.getLesson() != null && !v.getLesson().equals("")) {
                day.addSubstitution(v);
            }
            zeile = zeile.nextElementSibling();
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Substitution(me.vertretungsplan.objects.Substitution)

Example 32 with Substitution

use of me.vertretungsplan.objects.Substitution in project substitution-schedule-parser by vertretungsplanme.

the class UntisCommonParserTest method testHandleRoomBoth.

@Test
public void testHandleRoomBoth() throws Exception {
    Substitution subst = new Substitution();
    Element cell = Jsoup.parse("<table><td><s>248</s>?236</td></table>").select("td").first();
    UntisCommonParser.handleRoom(subst, cell);
    assertEquals("248", subst.getPreviousRoom());
    assertEquals("236", subst.getRoom());
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) Element(org.jsoup.nodes.Element) Test(org.junit.Test)

Example 33 with Substitution

use of me.vertretungsplan.objects.Substitution in project substitution-schedule-parser by vertretungsplanme.

the class UntisCommonParserTest method testHandleClasses.

@Test
public void testHandleClasses() throws JSONException, CredentialInvalidException {
    JSONObject data = new JSONObject("{\"classesSeparated\": false}");
    Substitution 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());
}
Also used : JSONObject(org.json.JSONObject) Substitution(me.vertretungsplan.objects.Substitution) Test(org.junit.Test)

Example 34 with Substitution

use of me.vertretungsplan.objects.Substitution in project substitution-schedule-parser by vertretungsplanme.

the class SubstitutionTextUtilsTest2 method testGetTeachers2.

@Test
public void testGetTeachers2() {
    Substitution s = new Substitution();
    s.setTeacher("B");
    s.setPreviousTeacher(null);
    assertEquals("B", SubstitutionTextUtils.getTeachers(s));
    SubstitutionTextUtils.getText(s);
}
Also used : Substitution(me.vertretungsplan.objects.Substitution) Test(org.junit.Test)

Example 35 with Substitution

use of me.vertretungsplan.objects.Substitution in project substitution-schedule-parser by vertretungsplanme.

the class ESchoolDemoTest method demoTest.

@Test
public void demoTest() throws IOException, JSONException {
    SubstitutionSchedule schedule = parser.parseESchoolSchedule(Jsoup.parse(html));
    assertEquals(new LocalDateTime(2016, 9, 4, 14, 53), schedule.getLastChange());
    assertEquals(1, schedule.getDays().size());
    SubstitutionScheduleDay day = schedule.getDays().get(0);
    assertEquals(new LocalDate(2016, 9, 5), day.getDate());
    assertEquals(6, day.getSubstitutions().size());
    assertEquals(0, day.getMessages().size());
    for (Substitution subst : day.getSubstitutions()) {
        assertTrue(subst.getClasses().size() == 1);
        assertNotEmpty(subst.getLesson());
        assertNullOrNotEmpty(subst.getPreviousSubject());
        assertNotEmpty(subst.getSubject());
        assertNullOrNotEmpty(subst.getRoom());
        assertNullOrNotEmpty(subst.getTeacher());
        assertNullOrNotEmpty(subst.getPreviousTeacher());
        assertNullOrNotEmpty(subst.getDesc());
        assertNotEmpty(subst.getType());
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Substitution(me.vertretungsplan.objects.Substitution) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test)

Aggregations

Substitution (me.vertretungsplan.objects.Substitution)35 Test (org.junit.Test)15 SubstitutionScheduleDay (me.vertretungsplan.objects.SubstitutionScheduleDay)14 Element (org.jsoup.nodes.Element)11 LocalDate (org.joda.time.LocalDate)9 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)8 LocalDateTime (org.joda.time.LocalDateTime)7 Elements (org.jsoup.select.Elements)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Document (org.jsoup.nodes.Document)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 HashSet (java.util.HashSet)1 SubstitutionDiff (me.vertretungsplan.objects.diff.SubstitutionDiff)1 NotNull (org.jetbrains.annotations.NotNull)1