public static void getLessonsDetails(Document doc, Schedule s) throws ParseException { final SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); fmt.setTimeZone(TimeZone.getTimeZone("Europe/Moscow")); Elements tableCells = doc.getElementsByAttributeValue("class", "table diary"); for (Element tableCell : tableCells) { int tdCount = 0; Date date = null; Lesson l, lPrev = null; // lPrev to handle duplicate lesson int sameLesson = 0; // Also to handle duplicate lesson Elements trs = tableCell.getElementsByTag("tr"); for (Element tr : trs) { if (tr.hasAttr("class") && tr.attr("class").equals("table-header")) continue; l = null; sameLesson = 0; // assume no bug here Elements tds = tr.getElementsByTag("td"); for (Element td : tds) { if (td.hasAttr("class") && td.attr("class").equals("date")) { date = fmt.parse(td.getElementsByTag("div").first().text()); tdCount = 1; } else if (td.hasAttr("class") && td.attr("class").equals("diary-mark")) { String marks = fetchLongCellStringNoWhitespaces(td); if (l != null && marks != null) { if (sameLesson > 0 && lPrev != null) { l.setMarks(fixDuplicateString(marks, lPrev.getMarks(), sameLesson)); } else l.setMarks(marks); } tdCount++; } else if (td.hasAttr("class") && td.attr("class").equals("diary-comment")) { String comment = fetchLongCellStringNoWhitespaces(td); if (l != null && comment != null) { if (sameLesson > 0 && lPrev != null) { l.setComment(fixDuplicateString(comment, lPrev.getComment(), sameLesson)); } else l.setComment(comment); } tdCount++; } else if (tdCount == 2) { String theme = fetchLongCellStringNoWhitespaces(td); if (l != null && theme != null) { if (sameLesson > 0 && lPrev != null) { l.setTheme(fixDuplicateString(theme, lPrev.getTheme(), sameLesson)); } else l.setTheme(theme); } tdCount++; } else if (tdCount == 3) { String homework = fetchLongCellStringNoWhitespaces(td); if (l != null && homework != null) { if (sameLesson > 0 && lPrev != null) { l.setHomework(fixDuplicateString(homework, lPrev.getHomework(), sameLesson)); } else l.setHomework(homework); } tdCount++; } else if (SUBJECT_NAME.matcher(td.text()).find()) { tdCount = 2; int number = Integer.parseInt(td.text().substring(0, 1)); l = s.getLessonByNumber(date, number); if (lPrev != null && l != null && l.getStart().equals(lPrev.getStart()) && l.getNumber() == lPrev.getNumber()) { // We hit the same lesson bug sameLesson++; } } else { tdCount++; } } if (l != null) { lPrev = l; l.update(); } } } }