Crosswind

XmlPullParser

Feb 15th, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.41 KB | None | 0 0
  1. List<SubstitutionDay> parseReturnSubstitution() {
  2.         SubstitutionDay currentSubstitutionDay = new SubstitutionDay();
  3.         List<SubstitutionDay> results = new ArrayList<>();
  4.         Substitution currentSubstitution = new Substitution();
  5.         List<Substitution> currentSubstitutionList = new ArrayList<>();
  6.         int multipleClasses = 0, multiplePeriods = 0;
  7.         String text = "";
  8.  
  9.         try {
  10.             XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
  11.             xmlPullParserFactory.setNamespaceAware(true);
  12.             XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();
  13.             xmlPullParser.setInput(bufferedReader);
  14.  
  15.             int eventType = xmlPullParser.getEventType();
  16.  
  17.             while (eventType != END_DOCUMENT) {
  18.                 String tag;
  19.                 String attributeName = "";
  20.                 String attributeValue = "";
  21.                 String[] tempStringArray;
  22.  
  23.                 tag = xmlPullParser.getName();
  24.                 switch (eventType) {
  25.                     case TEXT:
  26.                         text = xmlPullParser.getText();
  27.                         break;
  28.  
  29.                     case START_TAG:
  30.                         if (xmlPullParser.getAttributeCount() > 0) {
  31.                             attributeName = xmlPullParser.getAttributeName(0);
  32.                             attributeValue = xmlPullParser.getAttributeValue(0);
  33.                         }
  34.                         switch (tag) {
  35.                             case "kopf":
  36.                                 currentSubstitutionDay = new SubstitutionDay();
  37.                                 break;
  38.                             case "haupt":
  39.                                 currentSubstitutionList.clear();
  40.                                 Log.i(TAG, "cleared list - elements: " + currentSubstitutionList.size());
  41.                             case "aktion":
  42.                                 currentSubstitution = new Substitution();
  43.                                 multipleClasses = 0;
  44.                                 multiplePeriods = 0;
  45.                                 break;
  46.                             case "fach":
  47.                                 if (attributeName.equalsIgnoreCase("fageaendert") && attributeValue.equalsIgnoreCase("ae")) {
  48.                                     currentSubstitution.setChanges(currentSubstitution.getChanges() + "subject|");
  49.                                 }
  50.                                 break;
  51.                             case "lehrer":
  52.                                 if (attributeName.equalsIgnoreCase("legeaendert") && attributeValue.equalsIgnoreCase("ae")) {
  53.                                     currentSubstitution.setChanges(currentSubstitution.getChanges() + "teacher|");
  54.                                 }
  55.                                 break;
  56.                             case "raum":
  57.                                 if (attributeName.equalsIgnoreCase("rageaendert") && attributeValue.equalsIgnoreCase("ae")) {
  58.                                     currentSubstitution.setChanges(currentSubstitution.getChanges() + "room|");
  59.                                 }
  60.                                 break;
  61.                         }
  62.                         break;
  63.  
  64.                     case END_TAG:
  65.                         switch (tag) {
  66.                             // all data stored as header data
  67.                             case "titel":
  68.                                 tempStringArray = text.split("[,(]");
  69.                                 String weekString = tempStringArray[2].substring(0, 1);
  70.  
  71.                                 String dateString = tempStringArray[1].trim();
  72.                                 long date = dateFormatterMonthName.parse(dateString).getTime();
  73.  
  74.                                 currentSubstitutionDay.setDate(date);
  75.                                 currentSubstitutionDay.setWeek(weekString);
  76.                                 break;
  77.                             case "schulname":
  78.                                 int id = text.isEmpty() ? -1 : School.findSchoolIdByName(text);
  79.                                 currentSubstitutionDay.setSchool(id);
  80.                                 break;
  81.                             case "datum":
  82.                                 long updated = dateTimeFormatterKomma.parse(text).getTime();
  83.                                 currentSubstitutionDay.setUpdated(updated);
  84.                                 break;
  85.                             // all data that is actual substitution information
  86.                             case "klasse":
  87.                                 String course = "";
  88.  
  89.                                 if (text.length() == 9 || text.length() > 10) {
  90.                                     String[] rangeClasses = {text.substring(0, 4), text.substring(5, 9)};
  91.                                     currentSubstitution.setClassYearLetter(rangeClasses[0]);
  92.                                     String[] rangeClassesLetter = {rangeClasses[0].substring(3, 4), rangeClasses[1].substring(3, 4)};
  93.                                     multipleClasses = (int) rangeClassesLetter[1].charAt(0) - (int) rangeClassesLetter[0].charAt(0);
  94.                                     currentSubstitution.setClassYearLetter(rangeClasses[0]);
  95.                                     if (text.length() > 10) {
  96.                                         course = text.substring(11);
  97.                                     }
  98.                                 } else {
  99.                                     if (text.length() == 10) {
  100.                                         course = text.substring(6);
  101.                                     }
  102.                                     text = text.substring(0, 4);
  103.                                     currentSubstitution.setClassYearLetter(text);
  104.                                 }
  105.                                 currentSubstitution.setClassCourse(course);
  106.                                 break;
  107.                             case "stunde":
  108.                                 int periods = Integer.parseInt(text.replaceAll("-", ""));
  109.                                 if (String.valueOf(periods).length() > 1) {
  110.                                     multiplePeriods = (periods % 10) - (periods / 10);
  111.                                     periods /= 10;
  112.                                 }
  113.                                 currentSubstitution.setPeriod(periods);
  114.                                 break;
  115.                             case "fach":
  116.                                 if (!text.equalsIgnoreCase("")) {
  117.                                     currentSubstitution.setSubject(Subject.getSubjectIdBySubjectShort(text));
  118.                                 }
  119.                                 break;
  120.                             case "lehrer":
  121.                                 if (!text.equalsIgnoreCase("")) {
  122.                                     String teacherString = text.replaceAll("[()]", "");
  123.                                     currentSubstitution.setTeacher(Teacher.getTeacherIdByTeacherShort(teacherString));
  124.                                 } else {
  125.                                     currentSubstitution.setTeacher(-1);
  126.                                 }
  127.                                 break;
  128.                             case "raum":
  129.                                 currentSubstitution.setRoom(text);
  130.                                 break;
  131.                             case "info":
  132.                                 currentSubstitution.setInfo(text);
  133.                                 break;
  134.  
  135.                             case "aktion":
  136.                                 int letterNumber;
  137.                                 String initialLetter = currentSubstitution.getClassYearLetter();
  138.                                 int initialPeriod = currentSubstitution.getPeriod();
  139.                                 //Log.i(TAG, currentSubstitution.getClassYearLetter().substring(0, 2) + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
  140.                                 //Log.i(TAG, "mPeriods: " + multiplePeriods + " -- mClasses: " + multipleClasses);
  141.                                 //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
  142.                                 for (int i = 0; i <= multiplePeriods; i++) {
  143.                                     for (int j = 0; j <= multipleClasses; j++) {
  144.                                         currentSubstitution.setPeriod(initialPeriod + i);
  145.                                         letterNumber = initialLetter.charAt(3) + j;
  146.                                         currentSubstitution.setClassYearLetter(initialLetter.replace(initialLetter.charAt(3), (char) letterNumber));
  147.                                         currentSubstitutionList.add(currentSubstitution);
  148.                                         Log.i(TAG, "Listgröße: " + currentSubstitutionList.size());
  149.                                         //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
  150.                                     }
  151.                                 }
  152.  
  153.                                 //Log.i(TAG, currentSubstitution.getClassYearLetter() + "-" + currentSubstitution.getClassCourse() + "-" + currentSubstitution.getPeriod() + "-" + currentSubstitution.getSubject() + "-" + currentSubstitution.getTeacher() + "-" + currentSubstitution.getRoom() + "-" + currentSubstitution.getInfo() + "-" + currentSubstitution.getChanges());
  154.                                 break;
  155.  
  156.                             case "haupt":
  157.                                 currentSubstitutionDay.setSubstitutionList(currentSubstitutionList);
  158.                                 Log.i(TAG, "Menge der Vertretungen nach dem Hinzufügen: " + currentSubstitutionList.size());
  159.                                 results.add(currentSubstitutionDay);
  160.                                 Log.i(TAG, "Menge der Vertretungen nach dem Hinzufügen(2): " + results.get(results.size()-1).getSubstitutionList().size());
  161.  
  162.                                 break;
  163.                         }
  164.                         break;
  165.                 }
  166.                 eventType = xmlPullParser.next();
  167.             }
  168.         } catch (IOException | XmlPullParserException e) {
  169.             Log.e(TAG, "Fehler beim Parsen/IO: ", e);
  170.         } catch (ParseException e) {
  171.             Log.e(TAG, "ParseException: ", e);
  172.         } catch (IllegalFormatException e) {
  173.             Log.e(TAG, "No school with this name found!", e);
  174.         }
  175.         for (int i = 0; i < results.size(); i++) {
  176.             Log.i(TAG, "Datum: " + results.get(i).getDate() + "\nMenge der Vertretungen: " + results.get(i).getSubstitutionList().size());
  177.         }
  178.         Log.i(TAG, "finished parsing, result size: " + String.valueOf(results.size()));
  179.         return results;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment