Search in sources :

Example 36 with TextNode

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

the class Zones method reservation.

@Override
public ReservationResult reservation(DetailedItem item, Account acc, int useraction, String selection) throws IOException {
    String reservation_info = item.getReservation_info();
    String html = httpGet(opac_url + "/" + reservation_info, getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    if (html.contains("Geheimnummer")) {
        List<NameValuePair> params = new ArrayList<>();
        for (Element input : doc.select("#MainForm input")) {
            if (!input.attr("name").equals("BRWR") && !input.attr("name").equals("PIN")) {
                params.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
            }
        }
        params.add(new BasicNameValuePair("BRWR", acc.getName()));
        params.add(new BasicNameValuePair("PIN", acc.getPassword()));
        html = httpGet(opac_url + "/" + doc.select("#MainForm").attr("action") + "?" + URLEncodedUtils.format(params, getDefaultEncoding()), getDefaultEncoding());
        doc = Jsoup.parse(html);
    }
    if (useraction == ReservationResult.ACTION_BRANCH) {
        List<NameValuePair> params = new ArrayList<>();
        for (Element input : doc.select("#MainForm input")) {
            if (!input.attr("name").equals("Confirm")) {
                params.add(new BasicNameValuePair(input.attr("name"), input.attr("value")));
            }
        }
        params.add(new BasicNameValuePair("MakeResTypeDef.Reservation.RecipientLocn", selection));
        params.add(new BasicNameValuePair("Confirm", "1"));
        httpGet(opac_url + "/" + doc.select("#MainForm").attr("action") + "?" + URLEncodedUtils.format(params, getDefaultEncoding()), getDefaultEncoding());
        return new ReservationResult(MultiStepResult.Status.OK);
    }
    if (useraction == 0) {
        ReservationResult res = null;
        for (Node n : doc.select("#MainForm").first().childNodes()) {
            if (n instanceof TextNode) {
                if (((TextNode) n).text().contains("Entgelt")) {
                    res = new ReservationResult(ReservationResult.Status.CONFIRMATION_NEEDED);
                    List<String[]> details = new ArrayList<>();
                    details.add(new String[] { ((TextNode) n).text().trim() });
                    res.setDetails(details);
                    res.setMessage(((TextNode) n).text().trim());
                    res.setActionIdentifier(MultiStepResult.ACTION_CONFIRMATION);
                }
            }
        }
        if (res != null) {
            return res;
        }
    }
    if (doc.select("#MainForm select").size() > 0) {
        ReservationResult res = new ReservationResult(ReservationResult.Status.SELECTION_NEEDED);
        List<Map<String, String>> sel = new ArrayList<>();
        for (Element opt : doc.select("#MainForm select option")) {
            Map<String, String> selopt = new HashMap<>();
            selopt.put("key", opt.attr("value"));
            selopt.put("value", opt.text().trim());
            sel.add(selopt);
        }
        res.setSelection(sel);
        res.setMessage("Bitte Zweigstelle auswählen");
        res.setActionIdentifier(ReservationResult.ACTION_BRANCH);
        return res;
    }
    return new ReservationResult(ReservationResult.Status.ERROR);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) ArrayList(java.util.ArrayList) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with TextNode

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

the class Primo method parse_detail.

protected DetailedItem parse_detail(String id, Document doc) throws OpacErrorException, IOException {
    DetailedItem res = new DetailedItem();
    res.setId(id);
    res.setTitle(doc.select(".EXLResultTitle").text());
    for (Element detrow : doc.select(".EXLDetailsContent li")) {
        String title = null;
        String value = "";
        for (Node node : detrow.childNodes()) {
            if (node instanceof Element && (((Element) node).tagName().equals("strong") || ((Element) node).hasClass("bib-EXLDetailsContent-item-title"))) {
                title = ((Element) node).text();
            } else if (node instanceof Element && title != null) {
                value += ((Element) node).text();
            } else if (node instanceof TextNode && title != null) {
                value += ((TextNode) node).text();
            }
        }
        if (title != null) {
            res.addDetail(new Detail(title, value.trim()));
        }
    }
    String html2 = httpGet(opac_url + "/action/display.do?ct=display&fn=search&vid=" + vid + "&doc=" + id + "&tabs=locationsTab", getDefaultEncoding());
    Document doc2 = Jsoup.parse(html2);
    if (doc2.select(".EXLLocationTitlesRow").size() > 0) {
        Map<Integer, String> copymap = new HashMap<>();
        int i = 0;
        for (Element th : doc2.select(".EXLLocationTitlesRow th")) {
            String title = th.text().toLowerCase(Locale.GERMAN).trim();
            if (title.contains("library") || title.contains("bibliothek") || title.contains("branch")) {
                copymap.put(i, "branch");
            } else if (title.contains("location") || title.contains("ort")) {
                copymap.put(i, "location");
            } else if (title.contains("call number") || title.contains("signatur")) {
                copymap.put(i, "signature");
            } else if (title.contains("due date") || title.contains("llig am") || title.contains("ausgeliehen bis") || title.contains("lligkeit") || title.contains("ausleihstatus")) {
                copymap.put(i, "returndate");
            } else if (title.contains("loan to") || title.contains("bezugsmodalit") || title.contains("ausleihm") || title.contains("status")) {
                copymap.put(i, "status");
            } else if (title.contains("queue") || title.contains("vormerker")) {
                copymap.put(i, "reservations");
            }
            i++;
        }
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        DateTimeFormatter fmt2 = DateTimeFormat.forPattern("dd/MM/yyyy").withLocale(Locale.GERMAN);
        for (Element tr : doc2.select(".EXLLocationTable tr:not(.EXLLocationTitlesRow):not(" + ".EXLAdditionalFieldsRow)")) {
            int j = 0;
            Copy copy = new Copy();
            for (Element td : tr.children()) {
                String value = td.text().replace("\u00a0", " ").trim();
                if (copymap.containsKey(j) && !value.equals("")) {
                    try {
                        copy.set(copymap.get(j), value, fmt);
                    } catch (IllegalArgumentException e) {
                        try {
                            copy.set(copymap.get(j), value, fmt2);
                        } catch (IllegalArgumentException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
                j++;
            }
            res.addCopy(copy);
        }
    } else if (doc2.select(".EXLLocationList").size() > 0) {
        // e.g. University of South Wales
        for (Element row : doc2.select(".EXLLocationList")) {
            Copy copy = new Copy();
            copy.setBranch(row.select(".EXLLocationsTitle").text());
            copy.setDepartment(row.select(".EXLLocationInfo strong").text());
            copy.setShelfmark(row.select(".EXLLocationInfo cite").text());
            copy.setStatus(row.select(".EXLLocationInfo em").text());
            res.addCopy(copy);
        }
    }
    if (res.getCopies().size() == 0) {
        // Online-Medium?
        String html3 = httpGet(opac_url + "/action/display.do?ct=display&fn=search&vid=" + vid + "&doc=" + id + "&tabs=viewOnlineTab", getDefaultEncoding());
        Document doc3 = Jsoup.parse(html3);
        doc3.setBaseUri(opac_url + "/action/display.do");
        if (doc3.select(".EXLTabHeaderContent a").size() > 0) {
            Element link = doc3.select(".EXLTabHeaderContent a").first();
            res.addDetail(new Detail(link.text().trim(), cleanUrl(link.absUrl("href"))));
        }
        for (Element link : doc3.select(".EXLViewOnlineLinksTitle a")) {
            res.addDetail(new Detail(link.text().trim(), cleanUrl(link.absUrl("href"))));
        }
    }
    return res;
}
Also used : HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Copy(de.geeksfactory.opacclient.objects.Copy) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 38 with TextNode

use of org.jsoup.nodes.TextNode in project spring-boot-quick by vector4wang.

the class HTML2Md method getTextContent.

private static String getTextContent(Element element) {
    ArrayList<MDLine> lines = new ArrayList<MDLine>();
    List<Node> children = element.childNodes();
    for (Node child : children) {
        if (child instanceof TextNode) {
            TextNode textNode = (TextNode) child;
            MDLine line = getLastLine(lines);
            if (line.getContent().equals("")) {
                if (!textNode.isBlank()) {
                    line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*"));
                }
            } else {
                line.append(textNode.text().replaceAll("#", "/#").replaceAll("\\*", "/\\*"));
            }
        } else if (child instanceof Element) {
            Element childElement = (Element) child;
            processElement(childElement, lines);
        } else {
            System.out.println();
        }
    }
    int blankLines = 0;
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i).toString().trim();
        if (line.equals("")) {
            blankLines++;
        } else {
            blankLines = 0;
        }
        if (blankLines < 2) {
            result.append(line);
            if (i < lines.size() - 1) {
                result.append("\n");
            }
        }
    }
    return result.toString();
}
Also used : TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode)

Example 39 with TextNode

use of org.jsoup.nodes.TextNode in project zrlog by 94fzb.

the class ParseUtil method autoDigest.

public static String autoDigest(String str, int size) {
    StringBuilder sb = new StringBuilder();
    Document document = Jsoup.parseBodyFragment(str);
    List<Node> allTextNode = new ArrayList<>();
    getAllTextNode(document.childNodes(), allTextNode);
    int tLength = 0;
    for (Node node : allTextNode) {
        if (node instanceof TextNode) {
            sb.append(node.parent().outerHtml());
            tLength += ((TextNode) node).text().length();
            if (tLength > size) {
                sb.append(" ...");
                break;
            }
        }
    }
    String digest = sb.toString();
    Elements elements = Jsoup.parse(str).body().select("video");
    if (elements != null && !elements.isEmpty()) {
        digest = elements.get(0).toString() + "<br/>" + digest;
    }
    return digest.trim();
}
Also used : Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) ArrayList(java.util.ArrayList) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 40 with TextNode

use of org.jsoup.nodes.TextNode in project mylyn.docs by eclipse.

the class DocumentProcessorTest method testNormalizeTextNodes.

@Test
public void testNormalizeTextNodes() {
    Document document = new Document("");
    Element element = document.appendElement("root");
    element.appendText("first ");
    element.appendText("second,");
    element.appendText(" third");
    element.appendElement("break");
    element.appendText("fourth");
    assertEquals(5, element.childNodes().size());
    TestDocumentProcessor.normalizeTextNodes(element);
    assertEquals(3, element.childNodes().size());
    assertTrue(element.childNode(0) instanceof TextNode);
    assertEquals("first second, third", ((TextNode) element.childNode(0)).text());
    assertTrue(element.childNode(2) instanceof TextNode);
    assertEquals("fourth", ((TextNode) element.childNode(2)).text());
}
Also used : Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

TextNode (org.jsoup.nodes.TextNode)52 Element (org.jsoup.nodes.Element)41 Node (org.jsoup.nodes.Node)37 Document (org.jsoup.nodes.Document)19 ArrayList (java.util.ArrayList)16 Elements (org.jsoup.select.Elements)14 IOException (java.io.IOException)6 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)6 JSONException (org.json.JSONException)6 Copy (de.geeksfactory.opacclient.objects.Copy)5 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)5 HashMap (java.util.HashMap)5 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 Test (org.junit.jupiter.api.Test)5 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)4 Detail (de.geeksfactory.opacclient.objects.Detail)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URI (java.net.URI)4 Matcher (java.util.regex.Matcher)4