Search in sources :

Example 1 with Comment

use of org.jsoup.nodes.Comment in project flow by vaadin.

the class DefaultTemplateParser method removeCommentsRecursively.

private static void removeCommentsRecursively(Node node) {
    int i = 0;
    while (i < node.childNodes().size()) {
        Node child = node.childNode(i);
        if (child instanceof Comment) {
            child.remove();
        } else {
            removeCommentsRecursively(child);
            i++;
        }
    }
}
Also used : Comment(org.jsoup.nodes.Comment) Node(org.jsoup.nodes.Node)

Example 2 with Comment

use of org.jsoup.nodes.Comment in project opacclient by opacapp.

the class Bibliotheca method parseSearch.

public static SearchRequestResult parseSearch(String html, int page, JSONObject data) throws OpacErrorException {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(data.optString("baseurl"));
    Elements table = doc.select(".resulttab tr.result_trefferX, .resulttab tr.result_treffer");
    List<SearchResult> results = new ArrayList<>();
    for (int i = 0; i < table.size(); i++) {
        Element tr = table.get(i);
        SearchResult sr = new SearchResult();
        int contentindex = 1;
        if (tr.select("td a img").size() > 0) {
            String[] fparts = tr.select("td a img").get(0).attr("src").split("/");
            String fname = fparts[fparts.length - 1];
            if (data.has("mediatypes")) {
                try {
                    sr.setType(MediaType.valueOf(data.getJSONObject("mediatypes").getString(fname)));
                } catch (JSONException | IllegalArgumentException e) {
                    sr.setType(defaulttypes.get(fname.toLowerCase(Locale.GERMAN).replace(".jpg", "").replace(".gif", "").replace(".png", "")));
                }
            } else {
                sr.setType(defaulttypes.get(fname.toLowerCase(Locale.GERMAN).replace(".jpg", "").replace(".gif", "").replace(".png", "")));
            }
        } else {
            if (tr.children().size() == 3) {
                contentindex = 2;
            }
        }
        sr.setInnerhtml(tr.child(contentindex).child(0).html());
        sr.setNr(i);
        Element link = tr.child(contentindex).select("a").first();
        try {
            if (link != null && link.attr("href").contains("detmediennr")) {
                Map<String, String> params = getQueryParamsFirst(link.attr("abs:href"));
                String nr = params.get("detmediennr");
                if (Integer.parseInt(nr) > i + 1) {
                    // Seems to be an ID…
                    if (params.get("detDB") != null) {
                        sr.setId("&detmediennr=" + nr + "&detDB=" + params.get("detDB"));
                    } else {
                        sr.setId("&detmediennr=" + nr);
                    }
                }
            }
        } catch (Exception e) {
        }
        try {
            if (tr.child(1).childNode(0) instanceof Comment) {
                Comment c = (Comment) tr.child(1).childNode(0);
                String comment = c.getData().trim();
                String id = comment.split(": ")[1];
                sr.setId(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        results.add(sr);
    }
    int results_total = -1;
    if (doc.select(".result_gefunden").size() > 0) {
        try {
            results_total = Integer.parseInt(doc.select(".result_gefunden").text().trim().replaceAll(".*[^0-9]+([0-9]+).*", "$1"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            results_total = -1;
        }
    } else if (doc.select(".resultzeile").size() > 0) {
        throw new OpacErrorException(doc.select(".resultzeile").text());
    }
    return new SearchRequestResult(results, results_total, page);
}
Also used : Comment(org.jsoup.nodes.Comment) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) SearchResult(de.geeksfactory.opacclient.objects.SearchResult) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult)

Aggregations

Comment (org.jsoup.nodes.Comment)2 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)1 SearchRequestResult (de.geeksfactory.opacclient.objects.SearchRequestResult)1 SearchResult (de.geeksfactory.opacclient.objects.SearchResult)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 JSONException (org.json.JSONException)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1 Node (org.jsoup.nodes.Node)1 Elements (org.jsoup.select.Elements)1