Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<SubstitutionDay> parseReturnSubstitution() {
- SubstitutionDay currentSubstitutionDay = new SubstitutionDay();
- List<SubstitutionDay> results = new ArrayList<>();
- Substitution currentSubstitution = new Substitution();
- List<Substitution> currentSubstitutionList = new ArrayList<>();
- int multipleClasses = 0, multiplePeriods = 0;
- String text = "";
- try {
- XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
- xmlPullParserFactory.setNamespaceAware(true);
- XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();
- xmlPullParser.setInput(bufferedReader);
- int eventType = xmlPullParser.getEventType();
- while (eventType != END_DOCUMENT) {
- String tag;
- String attributeName = "";
- String attributeValue = "";
- String[] tempStringArray;
- tag = xmlPullParser.getName();
- switch (eventType) {
- case TEXT:
- text = xmlPullParser.getText();
- break;
- case START_TAG:
- if (xmlPullParser.getAttributeCount() > 0) {
- attributeName = xmlPullParser.getAttributeName(0);
- attributeValue = xmlPullParser.getAttributeValue(0);
- }
- switch (tag) {
- case "kopf":
- currentSubstitutionDay = new SubstitutionDay();
- break;
- case "haupt":
- currentSubstitutionList.clear();
- Log.i(TAG, "cleared list - elements: " + currentSubstitutionList.size());
- case "aktion":
- currentSubstitution = new Substitution();
- multipleClasses = 0;
- multiplePeriods = 0;
- break;
- case "fach":
- if (attributeName.equalsIgnoreCase("fageaendert") && attributeValue.equalsIgnoreCase("ae")) {
- currentSubstitution.setChanges(currentSubstitution.getChanges() + "subject|");
- }
- break;
- case "lehrer":
- if (attributeName.equalsIgnoreCase("legeaendert") && attributeValue.equalsIgnoreCase("ae")) {
- currentSubstitution.setChanges(currentSubstitution.getChanges() + "teacher|");
- }
- break;
- case "raum":
- if (attributeName.equalsIgnoreCase("rageaendert") && attributeValue.equalsIgnoreCase("ae")) {
- currentSubstitution.setChanges(currentSubstitution.getChanges() + "room|");
- }
- break;
- }
- break;
- case END_TAG:
- switch (tag) {
- // all data stored as header data
- case "titel":
- tempStringArray = text.split("[,(]");
- String weekString = tempStringArray[2].substring(0, 1);
- String dateString = tempStringArray[1].trim();
- long date = dateFormatterMonthName.parse(dateString).getTime();
- currentSubstitutionDay.setDate(date);
- currentSubstitutionDay.setWeek(weekString);
- break;
- case "schulname":
- int id = text.isEmpty() ? -1 : School.findSchoolIdByName(text);
- currentSubstitutionDay.setSchool(id);
- break;
- case "datum":
- long updated = dateTimeFormatterKomma.parse(text).getTime();
- currentSubstitutionDay.setUpdated(updated);
- break;
- // all data that is actual substitution information
- case "klasse":
- String course = "";
- if (text.length() == 9 || text.length() > 10) {
- String[] rangeClasses = {text.substring(0, 4), text.substring(5, 9)};
- currentSubstitution.setClassYearLetter(rangeClasses[0]);
- String[] rangeClassesLetter = {rangeClasses[0].substring(3, 4), rangeClasses[1].substring(3, 4)};
- multipleClasses = (int) rangeClassesLetter[1].charAt(0) - (int) rangeClassesLetter[0].charAt(0);
- currentSubstitution.setClassYearLetter(rangeClasses[0]);
- if (text.length() > 10) {
- course = text.substring(11);
- }
- } else {
- if (text.length() == 10) {
- course = text.substring(6);
- }
- text = text.substring(0, 4);
- currentSubstitution.setClassYearLetter(text);
- }
- currentSubstitution.setClassCourse(course);
- break;
- case "stunde":
- int periods = Integer.parseInt(text.replaceAll("-", ""));
- if (String.valueOf(periods).length() > 1) {
- multiplePeriods = (periods % 10) - (periods / 10);
- periods /= 10;
- }
- currentSubstitution.setPeriod(periods);
- break;
- case "fach":
- if (!text.equalsIgnoreCase("")) {
- currentSubstitution.setSubject(Subject.getSubjectIdBySubjectShort(text));
- }
- break;
- case "lehrer":
- if (!text.equalsIgnoreCase("")) {
- String teacherString = text.replaceAll("[()]", "");
- currentSubstitution.setTeacher(Teacher.getTeacherIdByTeacherShort(teacherString));
- } else {
- currentSubstitution.setTeacher(-1);
- }
- break;
- case "raum":
- currentSubstitution.setRoom(text);
- break;
- case "info":
- currentSubstitution.setInfo(text);
- break;
- case "aktion":
- int letterNumber;
- String initialLetter = currentSubstitution.getClassYearLetter();
- int initialPeriod = currentSubstitution.getPeriod();
- //Log.i(TAG, currentSubstitution.getClassYearLetter().substring(0, 2) + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
- //Log.i(TAG, "mPeriods: " + multiplePeriods + " -- mClasses: " + multipleClasses);
- //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
- for (int i = 0; i <= multiplePeriods; i++) {
- for (int j = 0; j <= multipleClasses; j++) {
- currentSubstitution.setPeriod(initialPeriod + i);
- letterNumber = initialLetter.charAt(3) + j;
- currentSubstitution.setClassYearLetter(initialLetter.replace(initialLetter.charAt(3), (char) letterNumber));
- currentSubstitutionList.add(currentSubstitution);
- Log.i(TAG, "Listgröße: " + currentSubstitutionList.size());
- //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
- }
- }
- //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
- break;
- case "haupt":
- currentSubstitutionDay.setSubstitutionList(currentSubstitutionList);
- Log.i(TAG, "Menge der Vertretungen nach dem Hinzufügen: " + currentSubstitutionList.size());
- results.add(currentSubstitutionDay);
- Log.i(TAG, "Menge der Vertretungen nach dem Hinzufügen(2): " + results.get(results.size()-1).getSubstitutionList().size());
- break;
- }
- break;
- }
- eventType = xmlPullParser.next();
- }
- } catch (IOException | XmlPullParserException e) {
- Log.e(TAG, "Fehler beim Parsen/IO: ", e);
- } catch (ParseException e) {
- Log.e(TAG, "ParseException: ", e);
- } catch (IllegalFormatException e) {
- Log.e(TAG, "No school with this name found!", e);
- }
- for (int i = 0; i < results.size(); i++) {
- Log.i(TAG, "Datum: " + results.get(i).getDate() + "\nMenge der Vertretungen: " + results.get(i).getSubstitutionList().size());
- }
- Log.i(TAG, "finished parsing, result size: " + String.valueOf(results.size()));
- return results;
- }
Advertisement
Add Comment
Please, Sign In to add comment