/** * This methods checks whether elements have a child element of with a given attribute. * * @param elements * @param testSolutionHandler */ private void checkChildElementWithAttributePresence( Elements elements, TestSolutionHandler testSolutionHandler) { if (elements.isEmpty()) { testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE); return; } TestSolution testSolution = TestSolution.PASSED; for (Element el : elements) { if (!el.getElementsByAttribute(attributeName).isEmpty()) { testSolution = setTestSolution(testSolution, getSuccessSolution()); addSourceCodeRemark(getSuccessSolution(), el, getSuccessMsgCode()); } else { testSolution = setTestSolution(testSolution, getFailureSolution()); addSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode()); } } testSolutionHandler.addTestSolution(testSolution); }
String parseImageURL(Element element) { try { if (element.classNames().contains("m-hero__slot")) { Element a = element.getElementsByClass("m-hero__slot-link").first(); Element imgDiv = a.getElementsByAttribute("data-original").first(); return imgDiv.attr("data-original"); } else if (element.classNames().contains("m-entry-slot")) { Element imgDiv = element.getElementsByAttribute("data-original").first(); return imgDiv.attr("data-original"); } else throw new NullPointerException(); } catch (NullPointerException e) { e.printStackTrace(); // Return an image that says "No image" //noinspection SpellCheckingInspection return "http://best-classic-cars.com/images/no_image_available.png.pagespeed.ce.NRX39FjzIc.png"; } }
public static List<CaoImg> getImgData(String url) throws Exception { String response = HttpUtils.getString(url); Document parse = Jsoup.parse(response); Elements allElements = parse.getAllElements(); List<CaoImg> caoImgs = new ArrayList<CaoImg>(); for (int i = 0; i < allElements.size(); i++) { Element element = allElements.get(i); // <table class="wikitable" // style="width: 22em; position: absolute; top: 0px; left: 0px;"> String nodeName = element.nodeName(); String attrClass = element.attr("class"); if (nodeName.equals("table") && "wikitable".equals(attrClass + "")) { String title = element.getElementsByAttribute("title").get(0).attr("title"); Elements imgElement = element.getElementsByTag("img"); String src = imgElement.attr("src"); Elements styleElements = element.getElementsByAttributeValueContaining("style", "font-size"); String otherName = null; String intro = null; if (styleElements.size() == 1) { intro = styleElements.get(0).text(); } else { otherName = styleElements.get(0).text(); intro = styleElements.get(1).text(); } CaoImg caoImg = new CaoImg(); caoImg.setName(title); caoImg.setImg(src); caoImg.setOtherName(otherName); caoImg.setIntro(intro); caoImgs.add(caoImg); } } return caoImgs; }
public static void getLessons(Document doc, Schedule s) throws ParseException { final SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH); format.setTimeZone(TimeZone.getTimeZone("Europe/Moscow")); Elements lessonCells = doc.getElementsByAttribute("number"); for (Element lessonCell : lessonCells) { Lesson l, lPrev = null; // lPrev to handle duplicate lesson int sameLesson = 0; // Also to handle duplicate lesson int number = Integer.parseInt(lessonCell.attr("number")); String time = ""; Elements timeDetails = lessonCell.getElementsByClass("cell-header2"); for (Element timeDetail : timeDetails) { if (timeDetail.hasAttr("style")) time = timeDetail.text(); } Elements lessonCellDetails = lessonCell.getElementsByAttribute("jsdate"); for (Element lessonCellDetail : lessonCellDetails) { String date = lessonCellDetail.attr("jsdate"); int index = 0; sameLesson = 0; for (Element subject : lessonCellDetail.getElementsByAttributeValue("class", "lesson-subject")) { if (subject == null || subject.text() == null || subject.text().length() <= 0) { // No lesson scheduled continue; } Date start = format.parse(date + " " + time.substring(0, time.indexOf("-") - 1)); if ((l = s.getLessonByNumber(start, number)) == null) { if (BuildConfig.DEBUG) Log.d("GshisHTMLParser", TS.get() + " getLessons() not found in db, will insert"); l = new Lesson(); sameLesson = 0; l.setStart(start); l.setStop( format.parse(date + " " + time.substring(time.indexOf("-") + 2, time.length()))); l.setFormId(subject.attr("id")); l.setFormText(subject.text()); l.setTeacher( lessonCellDetail .getElementsByAttributeValue("class", "lesson-teacher") .get(sameLesson) .text()); l.setNumber(number); s.addLesson(l); } else { if (BuildConfig.DEBUG) Log.d("GshisHTMLParser", TS.get() + " getLessons() found in db, will update"); l.setFormId(subject.attr("id")); if (lPrev != null && lPrev.getStart().equals(start) && lPrev.getNumber() == number) { if (BuildConfig.DEBUG) Log.d( "GshisHTMLParser", TS.get() + " getLessons() dup = " + subject.text() + " index = " + index + " sameLesson = " + sameLesson); sameLesson++; if (!lPrev.getFormText().equals(subject.text())) l.setFormText(fixDuplicateString(subject.text(), lPrev.getFormText(), sameLesson)); String teacher = lessonCellDetail .getElementsByAttributeValue("class", "lesson-teacher") .get(index) .text(); if (!lPrev.getTeacher().equals(teacher)) l.setTeacher(fixDuplicateString(teacher, lPrev.getTeacher(), sameLesson)); } else { l.setNumber(number); l.setFormText(subject.text()); l.setTeacher( lessonCellDetail .getElementsByAttributeValue("class", "lesson-teacher") .get(index) .text()); } l.update(); } lPrev = l; index++; } } } }