Example usage for org.jsoup.nodes Element text

List of usage examples for org.jsoup.nodes Element text

Introduction

In this page you can find the example usage for org.jsoup.nodes Element text.

Prototype

public String text() 

Source Link

Document

Gets the combined text of this element and all its children.

Usage

From source file:me.vertretungsplan.parser.UntisCommonParser.java

/**
 * Parses an Untis substitution schedule table
 *
 * @param table        the <code>table</code> Element from the HTML document
 * @param data         {@link SubstitutionScheduleData#getData()}
 * @param day          the {@link SubstitutionScheduleDay} where the substitutions will be stored
 * @param defaultClass the class that should be set if there is no class column in the table
 *//*from ww  w. j a  va2  s  .co m*/
private void parseSubstitutionScheduleTable(Element table, JSONObject data, SubstitutionScheduleDay day,
        String defaultClass) throws JSONException, CredentialInvalidException {
    if (data.optBoolean(PARAM_CLASS_IN_EXTRA_LINE) || data.optBoolean("class_in_extra_line")) { // backwards compatibility
        for (Element element : table.select("td.inline_header")) {
            String className = getClassName(element.text(), data);
            if (isValidClass(className)) {
                Element zeile = null;
                try {
                    zeile = element.parent().nextElementSibling();
                    if (zeile.select("td") == null) {
                        zeile = zeile.nextElementSibling();
                    }
                    int skipLines = 0;
                    while (zeile != null && !zeile.select("td").attr("class").equals("list inline_header")) {
                        if (skipLines > 0) {
                            skipLines--;
                            zeile = zeile.nextElementSibling();
                            continue;
                        }

                        Substitution v = new Substitution();

                        int i = 0;
                        for (Element spalte : zeile.select("td")) {
                            String text = spalte.text();
                            if (isEmpty(text)) {
                                i++;
                                continue;
                            }

                            int skipLinesForThisColumn = 0;
                            Element nextLine = zeile.nextElementSibling();
                            boolean continueSkippingLines = true;
                            while (continueSkippingLines) {
                                if (nextLine != null && nextLine.children().size() == zeile.children().size()) {
                                    Element columnInNextLine = nextLine.child(spalte.elementSiblingIndex());
                                    if (columnInNextLine.text().replaceAll("\u00A0", "").trim()
                                            .equals(nextLine.text().replaceAll("\u00A0", "").trim())) {
                                        // Continued in the next line
                                        text += " " + columnInNextLine.text();
                                        skipLinesForThisColumn++;
                                        nextLine = nextLine.nextElementSibling();
                                    } else {
                                        continueSkippingLines = false;
                                    }
                                } else {
                                    continueSkippingLines = false;
                                }
                            }
                            if (skipLinesForThisColumn > skipLines)
                                skipLines = skipLinesForThisColumn;

                            String type = data.getJSONArray(PARAM_COLUMNS).getString(i);

                            switch (type) {
                            case "lesson":
                                v.setLesson(text);
                                break;
                            case "subject":
                                handleSubject(v, spalte);
                                break;
                            case "previousSubject":
                                v.setPreviousSubject(text);
                                break;
                            case "type":
                                v.setType(text);
                                v.setColor(colorProvider.getColor(text));
                                break;
                            case "type-entfall":
                                if (text.equals("x")) {
                                    v.setType("Entfall");
                                    v.setColor(colorProvider.getColor("Entfall"));
                                } else {
                                    v.setType("Vertretung");
                                    v.setColor(colorProvider.getColor("Vertretung"));
                                }
                                break;
                            case "room":
                                handleRoom(v, spalte);
                                break;
                            case "teacher":
                                handleTeacher(v, spalte, data);
                                break;
                            case "previousTeacher":
                                v.setPreviousTeachers(splitTeachers(text, data));
                                break;
                            case "desc":
                                v.setDesc(text);
                                break;
                            case "desc-type":
                                v.setDesc(text);
                                String recognizedType = recognizeType(text);
                                v.setType(recognizedType);
                                v.setColor(colorProvider.getColor(recognizedType));
                                break;
                            case "previousRoom":
                                v.setPreviousRoom(text);
                                break;
                            case "substitutionFrom":
                                v.setSubstitutionFrom(text);
                                break;
                            case "teacherTo":
                                v.setTeacherTo(text);
                                break;
                            case "ignore":
                                break;
                            case "date": // used by UntisSubstitutionParser
                                break;
                            default:
                                throw new IllegalArgumentException("Unknown column type: " + type);
                            }
                            i++;
                        }

                        autoDetectType(data, zeile, v);

                        v.getClasses().add(className);

                        if (v.getLesson() != null && !v.getLesson().equals("")) {
                            day.addSubstitution(v);
                        }

                        zeile = zeile.nextElementSibling();

                    }
                } catch (Throwable e) {

                    e.printStackTrace();
                }
            }
        }
    } else {
        boolean hasType = false;
        for (int i = 0; i < data.getJSONArray(PARAM_COLUMNS).length(); i++) {
            if (data.getJSONArray(PARAM_COLUMNS).getString(i).equals("type")) {
                hasType = true;
            }
        }
        int skipLines = 0;
        for (Element zeile : table.select("tr.list.odd:not(:has(td.inline_header)), "
                + "tr.list.even:not(:has(td.inline_header)), " + "tr:has(td[align=center]):gt(0)")) {
            if (skipLines > 0) {
                skipLines--;
                continue;
            }

            Substitution v = new Substitution();
            String klassen = defaultClass != null ? defaultClass : "";
            int i = 0;
            for (Element spalte : zeile.select("td")) {
                String text = spalte.text();

                String type = data.getJSONArray(PARAM_COLUMNS).getString(i);
                if (isEmpty(text) && !type.equals("type-entfall")) {
                    i++;
                    continue;
                }

                int skipLinesForThisColumn = 0;
                Element nextLine = zeile.nextElementSibling();
                boolean continueSkippingLines = true;
                while (continueSkippingLines) {
                    if (nextLine != null && nextLine.children().size() == zeile.children().size()) {
                        Element columnInNextLine = nextLine.child(spalte.elementSiblingIndex());
                        if (columnInNextLine.text().replaceAll("\u00A0", "").trim()
                                .equals(nextLine.text().replaceAll("\u00A0", "").trim())) {
                            // Continued in the next line
                            text += " " + columnInNextLine.text();
                            skipLinesForThisColumn++;
                            nextLine = nextLine.nextElementSibling();
                        } else {
                            continueSkippingLines = false;
                        }
                    } else {
                        continueSkippingLines = false;
                    }
                }
                if (skipLinesForThisColumn > skipLines)
                    skipLines = skipLinesForThisColumn;

                switch (type) {
                case "lesson":
                    v.setLesson(text);
                    break;
                case "subject":
                    handleSubject(v, spalte);
                    break;
                case "previousSubject":
                    v.setPreviousSubject(text);
                    break;
                case "type":
                    v.setType(text);
                    v.setColor(colorProvider.getColor(text));
                    break;
                case "type-entfall":
                    if (text.equals("x")) {
                        v.setType("Entfall");
                        v.setColor(colorProvider.getColor("Entfall"));
                    } else if (!hasType) {
                        v.setType("Vertretung");
                        v.setColor(colorProvider.getColor("Vertretung"));
                    }
                    break;
                case "room":
                    handleRoom(v, spalte);
                    break;
                case "previousRoom":
                    v.setPreviousRoom(text);
                    break;
                case "desc":
                    v.setDesc(text);
                    break;
                case "desc-type":
                    v.setDesc(text);
                    String recognizedType = recognizeType(text);
                    v.setType(recognizedType);
                    v.setColor(colorProvider.getColor(recognizedType));
                    break;
                case "teacher":
                    handleTeacher(v, spalte, data);
                    break;
                case "previousTeacher":
                    v.setPreviousTeachers(splitTeachers(text, data));
                    break;
                case "substitutionFrom":
                    v.setSubstitutionFrom(text);
                    break;
                case "teacherTo":
                    v.setTeacherTo(text);
                    break;
                case "class":
                    klassen = getClassName(text, data);
                    break;
                case "ignore":
                    break;
                case "date": // used by UntisSubstitutionParser
                    break;
                default:
                    throw new IllegalArgumentException("Unknown column type: " + type);
                }
                i++;
            }

            if (v.getLesson() == null || v.getLesson().equals("")) {
                continue;
            }

            autoDetectType(data, zeile, v);

            List<String> affectedClasses;

            // Detect things like "7"
            Pattern singlePattern = Pattern.compile("(\\d+)");
            Matcher singleMatcher = singlePattern.matcher(klassen);

            // Detect things like "5-12"
            Pattern rangePattern = Pattern.compile("(\\d+) ?- ?(\\d+)");
            Matcher rangeMatcher = rangePattern.matcher(klassen);

            Pattern pattern2 = Pattern.compile("^(\\d+).*");

            if (rangeMatcher.matches()) {
                affectedClasses = new ArrayList<>();
                int min = Integer.parseInt(rangeMatcher.group(1));
                int max = Integer.parseInt(rangeMatcher.group(2));
                try {
                    for (String klasse : getAllClasses()) {
                        Matcher matcher2 = pattern2.matcher(klasse);
                        if (matcher2.matches()) {
                            int num = Integer.parseInt(matcher2.group(1));
                            if (min <= num && num <= max)
                                affectedClasses.add(klasse);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (singleMatcher.matches()) {
                affectedClasses = new ArrayList<>();
                int grade = Integer.parseInt(singleMatcher.group(1));
                try {
                    for (String klasse : getAllClasses()) {
                        Matcher matcher2 = pattern2.matcher(klasse);
                        if (matcher2.matches() && grade == Integer.parseInt(matcher2.group(1))) {
                            affectedClasses.add(klasse);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                if (data.optBoolean(PARAM_CLASSES_SEPARATED, true)
                        && data.optBoolean("classes_separated", true)) { // backwards compatibility
                    affectedClasses = Arrays.asList(klassen.split(", "));
                } else {
                    affectedClasses = new ArrayList<>();
                    try {
                        for (String klasse : getAllClasses()) { // TODO: is there a better way?
                            StringBuilder regex = new StringBuilder();
                            for (char character : klasse.toCharArray()) {
                                if (character == '?') {
                                    regex.append("\\?");
                                } else {
                                    regex.append(character);
                                }
                                regex.append(".*");
                            }
                            if (klassen.matches(regex.toString())) {
                                affectedClasses.add(klasse);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            for (String klasse : affectedClasses) {
                if (isValidClass(klasse)) {
                    v.getClasses().add(klasse);
                }
            }

            if (data.optBoolean(PARAM_MERGE_WITH_DIFFERENT_TYPE, false)) {
                boolean found = false;
                for (Substitution subst : day.getSubstitutions()) {
                    if (subst.equalsExcludingType(v)) {
                        found = true;

                        if (v.getType().equals("Vertretung")) {
                            subst.setType("Vertretung");
                            subst.setColor(colorProvider.getColor("Vertretung"));
                        }

                        break;
                    }
                }
                if (!found) {
                    day.addSubstitution(v);
                }
            } else {
                day.addSubstitution(v);
            }
        }
    }
}

From source file:de.geeksfactory.opacclient.apis.Zones.java

@Override
public ReservationResult reservation(DetailledItem 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")));
            }// w w  w  . j av a 2s .  co m
        }
        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 auswhlen");
        res.setActionIdentifier(ReservationResult.ACTION_BRANCH);
        return res;
    }

    return new ReservationResult(ReservationResult.Status.ERROR);
}

From source file:de.geeksfactory.opacclient.apis.Zones.java

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    Document login = login(acc);//from   w w  w  .ja va2s .  co m
    if (login == null) {
        return null;
    }

    AccountData res = new AccountData(acc.getId());

    String lentLink = null;
    String resLink = null;
    int lent_cnt = -1;
    int res_cnt = -1;
    for (Element td : login.select(".AccountSummaryCounterNameCell, .AccountSummaryCounterNameCellStripe, "
            + ".CAccountDetailFieldNameCellStripe, .CAccountDetailFieldNameCell")) {
        String section = td.text().trim();
        if (section.contains("Entliehene Medien")) {
            lentLink = td.select("a").attr("href");
            lent_cnt = Integer.parseInt(td.nextElementSibling().text().trim());
        } else if (section.contains("Vormerkungen")) {
            resLink = td.select("a").attr("href");
            res_cnt = Integer.parseInt(td.nextElementSibling().text().trim());
        } else if (section.contains("Kontostand")) {
            res.setPendingFees(td.nextElementSibling().text().trim());
        } else if (section.matches("Ausweis g.ltig bis")) {
            res.setValidUntil(td.nextElementSibling().text().trim());
        }
    }
    for (Element a : login.select("a.AccountMenuLink")) {
        if (a.text().contains("Ausleihen")) {
            lentLink = a.attr("href");
        } else if (a.text().contains("Vormerkungen")) {
            resLink = a.attr("href");
        }
    }
    if (lentLink == null) {
        return null;
    }

    List<LentItem> lentItems = new ArrayList<>();
    String lentUrl = opac_url + "/" + lentLink.replace("utf-8?Method", "utf-8&Method");
    String lentHtml = httpGet(lentUrl, getDefaultEncoding());
    Document lentDoc = Jsoup.parse(lentHtml);
    lentDoc.setBaseUri(lentUrl);
    loadMediaList(lentDoc, lentItems);
    res.setLent(lentItems);

    // In Koeln, the reservations link only doesn't show on the overview page
    if (resLink == null) {
        for (Element a : lentDoc.select("a.AccountMenuLink")) {
            if (a.text().contains("Vormerkungen")) {
                resLink = a.attr("href");
            }
        }
    }

    List<ReservedItem> reservedItems = new ArrayList<>();
    String resHtml = httpGet(opac_url + "/" + resLink, getDefaultEncoding());
    Document resDoc = Jsoup.parse(resHtml);
    loadResList(resDoc, reservedItems);
    res.setReservations(reservedItems);

    return res;
}

From source file:de.geeksfactory.opacclient.apis.TouchPoint.java

public List<SearchField> getSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();/*from   w ww .  j  av a 2s .c om*/
    }

    String html = httpGet(opac_url + "/search.do?methodToCall=switchSearchPage&SearchType=2", ENCODING);
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();

    Elements options = doc.select("select[name=searchCategories[0]] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.attr("value"));
        field.setHint("");
        fields.add(field);
    }

    for (Element dropdown : doc.select(".accordion-body select")) {
        parseDropdown(dropdown, fields);
    }

    return fields;
}

From source file:de.geeksfactory.opacclient.apis.IOpac.java

protected SearchRequestResult parse_search(String html, int page)
        throws OpacErrorException, NotReachableException {
    Document doc = Jsoup.parse(html);

    if (doc.select("h4").size() > 0) {
        if (doc.select("h4").text().trim().startsWith("0 gefundene Medien")) {
            // nothing found
            return new SearchRequestResult(new ArrayList<SearchResult>(), 0, 1, 1);
        } else if (!doc.select("h4").text().trim().contains("gefundene Medien")
                && !doc.select("h4").text().trim().contains("Es wurden mehr als")) {
            // error
            throw new OpacErrorException(doc.select("h4").text().trim());
        }/* ww w  .jav a 2 s.  c o m*/
    } else if (doc.select("h1").size() > 0) {
        if (doc.select("h1").text().trim().contains("RUNTIME ERROR")) {
            // Server Error
            throw new NotReachableException("IOPAC RUNTIME ERROR");
        } else {
            throw new OpacErrorException(stringProvider.getFormattedString(
                    StringProvider.UNKNOWN_ERROR_WITH_DESCRIPTION, doc.select("h1").text().trim()));
        }
    } else {
        return null;
    }

    updateRechnr(doc);

    reusehtml = html;

    results_total = -1;

    if (doc.select("h4").text().trim().contains("Es wurden mehr als")) {
        results_total = 200;
    } else {
        String resultnumstr = doc.select("h4").first().text();
        resultnumstr = resultnumstr.substring(0, resultnumstr.indexOf(" ")).trim();
        results_total = Integer.parseInt(resultnumstr);
    }

    List<SearchResult> results = new ArrayList<>();

    Elements tables = doc.select("table").first().select("tr:has(td)");

    Map<String, Integer> colmap = new HashMap<>();
    Element thead = doc.select("table").first().select("tr:has(th)").first();
    int j = 0;
    for (Element th : thead.select("th")) {
        String text = th.text().trim().toLowerCase(Locale.GERMAN);
        if (text.contains("cover")) {
            colmap.put("cover", j);
        } else if (text.contains("titel")) {
            colmap.put("title", j);
        } else if (text.contains("verfasser")) {
            colmap.put("author", j);
        } else if (text.contains("mtyp")) {
            colmap.put("category", j);
        } else if (text.contains("jahr")) {
            colmap.put("year", j);
        } else if (text.contains("signatur")) {
            colmap.put("shelfmark", j);
        } else if (text.contains("info")) {
            colmap.put("info", j);
        } else if (text.contains("abteilung")) {
            colmap.put("department", j);
        } else if (text.contains("verliehen") || text.contains("verl.")) {
            colmap.put("returndate", j);
        } else if (text.contains("anz.res")) {
            colmap.put("reservations", j);
        }
        j++;
    }
    if (colmap.size() == 0) {
        colmap.put("cover", 0);
        colmap.put("title", 1);
        colmap.put("author", 2);
        colmap.put("publisher", 3);
        colmap.put("year", 4);
        colmap.put("department", 5);
        colmap.put("shelfmark", 6);
        colmap.put("returndate", 7);
        colmap.put("category", 8);
    }

    for (int i = 0; i < tables.size(); i++) {
        Element tr = tables.get(i);
        SearchResult sr = new SearchResult();

        if (tr.select("td").get(colmap.get("cover")).select("img").size() > 0) {
            String imgUrl = tr.select("td").get(colmap.get("cover")).select("img").first().attr("src");
            sr.setCover(imgUrl);
        }

        // Media Type
        if (colmap.get("category") != null) {
            String mType = tr.select("td").get(colmap.get("category")).text().trim().replace("\u00a0", "");
            if (data.has("mediatypes")) {
                try {
                    sr.setType(MediaType.valueOf(
                            data.getJSONObject("mediatypes").getString(mType.toLowerCase(Locale.GERMAN))));
                } catch (JSONException | IllegalArgumentException e) {
                    sr.setType(defaulttypes.get(mType.toLowerCase(Locale.GERMAN)));
                }
            } else {
                sr.setType(defaulttypes.get(mType.toLowerCase(Locale.GERMAN)));
            }
        }

        // Title and additional info
        String title;
        String additionalInfo = "";
        if (colmap.get("info") != null) {
            Element info = tr.select("td").get(colmap.get("info"));
            title = info.select("a[title=Details-Info]").text().trim();
            String authorIn = info.text().substring(0, info.text().indexOf(title));
            if (authorIn.contains(":")) {
                authorIn = authorIn.replaceFirst("^([^:]*):(.*)$", "$1");
                additionalInfo += " - " + authorIn;
            }
        } else {
            title = tr.select("td").get(colmap.get("title")).text().trim().replace("\u00a0", "");
            if (title.contains("(") && title.indexOf("(") > 0) {
                additionalInfo += title.substring(title.indexOf("("));
                title = title.substring(0, title.indexOf("(") - 1).trim();
            }

            // Author
            if (colmap.containsKey("author")) {
                String author = tr.select("td").get(colmap.get("author")).text().trim().replace("\u00a0", "");
                additionalInfo += " - " + author;
            }
        }

        // Publisher
        if (colmap.containsKey("publisher")) {
            String publisher = tr.select("td").get(colmap.get("publisher")).text().trim().replace("\u00a0", "");
            additionalInfo += " (" + publisher;
        }

        // Year
        if (colmap.containsKey("year")) {
            String year = tr.select("td").get(colmap.get("year")).text().trim().replace("\u00a0", "");
            additionalInfo += ", " + year + ")";
        }

        sr.setInnerhtml("<b>" + title + "</b><br>" + additionalInfo);

        // Status
        String status = tr.select("td").get(colmap.get("returndate")).text().trim().replace("\u00a0", "");
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
        try {
            df.parse(status);
            // this is a return date
            sr.setStatus(Status.RED);
            sr.setInnerhtml(sr.getInnerhtml() + "<br><i>" + stringProvider.getString(StringProvider.LENT_UNTIL)
                    + " " + status + "</i>");
        } catch (ParseException e) {
            // this is a different status text
            String lc = status.toLowerCase(Locale.GERMAN);
            if ((lc.equals("") || lc.toLowerCase(Locale.GERMAN).contains("onleihe") || lc.contains("verleihbar")
                    || lc.contains("entleihbar") || lc.contains("ausleihbar")) && !lc.contains("nicht")) {
                sr.setStatus(Status.GREEN);
            } else {
                sr.setStatus(Status.YELLOW);
                sr.setInnerhtml(sr.getInnerhtml() + "<br><i>" + status + "</i>");
            }
        }

        // In some libraries (for example search for "atelier" in Preetz)
        // the results are sorted differently than their numbers suggest, so
        // we need to detect the number ("recno") from the link
        String link = tr.select("a[href^=/cgi-bin/di.exe?page=]").attr("href");
        Map<String, String> params = getQueryParamsFirst(link);
        if (params.containsKey("recno")) {
            int recno = Integer.valueOf(params.get("recno"));
            sr.setNr(recno - 1);
        } else {
            // the above should work, but fall back to this if it doesn't
            sr.setNr(10 * (page - 1) + i);
        }

        // In some libraries (for example Preetz) we can detect the media ID
        // here using another link present in the search results
        Elements idLinks = tr.select("a[href^=/cgi-bin/di.exe?cMedNr]");
        if (idLinks.size() > 0) {
            Map<String, String> idParams = getQueryParamsFirst(idLinks.first().attr("href"));
            String id = idParams.get("cMedNr");
            sr.setId(id);
        } else {
            sr.setId(null);
        }

        results.add(sr);
    }
    return new SearchRequestResult(results, results_total, page);
}

From source file:de.geeksfactory.opacclient.apis.Bibliotheca.java

protected DetailledItem parse_result(String html) {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);/*from   w ww. j  av  a  2s . c o  m*/

    DetailledItem result = new DetailledItem();

    if (doc.select(".detail_cover img").size() == 1) {
        result.setCover(doc.select(".detail_cover img").get(0).attr("src"));
    }

    result.setTitle(doc.select(".detail_titel").text());

    Elements detailtrs = doc.select(".detailzeile table tr");
    for (int i = 0; i < detailtrs.size(); i++) {
        Element tr = detailtrs.get(i);
        if (tr.child(0).hasClass("detail_feld")) {
            String title = tr.child(0).text();
            String content = tr.child(1).text();
            if (title.equals("Gesamtwerk:") || title.equals("Erschienen in:")) {
                try {
                    if (tr.child(1).select("a").size() > 0) {
                        Element link = tr.child(1).select("a").first();
                        List<NameValuePair> query = URLEncodedUtils.parse(new URI(link.absUrl("href")),
                                "UTF-8");
                        for (NameValuePair q : query) {
                            if (q.getName().equals("MedienNr")) {
                                result.setCollectionId(q.getValue());
                            }
                        }
                    }
                } catch (URISyntaxException e) {
                }
            } else {

                if (content.contains("hier klicken") && tr.child(1).select("a").size() > 0) {
                    content += " " + tr.child(1).select("a").first().attr("href");
                }

                result.addDetail(new Detail(title, content));
            }
        }
    }

    Elements detailcenterlinks = doc.select(".detailzeile_center a.detail_link");
    for (int i = 0; i < detailcenterlinks.size(); i++) {
        Element a = detailcenterlinks.get(i);
        result.addDetail(new Detail(a.text().trim(), a.absUrl("href")));
    }

    try {
        JSONObject copymap = new JSONObject();
        if (data.has("copiestable")) {
            copymap = data.getJSONObject("copiestable");
        } else {
            Elements ths = doc.select(".exemplartab .exemplarmenubar th");
            for (int i = 0; i < ths.size(); i++) {
                Element th = ths.get(i);
                String head = th.text().trim();
                if (head.equals("Zweigstelle")) {
                    copymap.put("branch", i);
                } else if (head.equals("Abteilung")) {
                    copymap.put("department", i);
                } else if (head.equals("Bereich") || head.equals("Standort")) {
                    copymap.put("location", i);
                } else if (head.equals("Signatur")) {
                    copymap.put("signature", i);
                } else if (head.equals("Barcode") || head.equals("Medien-Nummer")) {
                    copymap.put("barcode", i);
                } else if (head.equals("Status")) {
                    copymap.put("status", i);
                } else if (head.equals("Frist") || head.matches("Verf.+gbar")) {
                    copymap.put("returndate", i);
                } else if (head.equals("Vorbestellungen") || head.equals("Reservierungen")) {
                    copymap.put("reservations", i);
                }
            }
        }
        Elements exemplartrs = doc.select(".exemplartab .tabExemplar, .exemplartab .tabExemplar_");
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        for (int i = 0; i < exemplartrs.size(); i++) {
            Element tr = exemplartrs.get(i);

            Copy copy = new Copy();

            Iterator<?> keys = copymap.keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                int index;
                try {
                    index = copymap.has(key) ? copymap.getInt(key) : -1;
                } catch (JSONException e1) {
                    index = -1;
                }
                if (index >= 0) {
                    try {
                        copy.set(key, tr.child(index).text(), fmt);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                }
            }

            result.addCopy(copy);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Elements bandtrs = doc.select("table .tabBand a");
        for (int i = 0; i < bandtrs.size(); i++) {
            Element tr = bandtrs.get(i);

            Volume volume = new Volume();
            volume.setId(tr.attr("href").split("=")[1]);
            volume.setTitle(tr.text());
            result.addVolume(volume);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (doc.select(".detail_vorbest a").size() == 1) {
        result.setReservable(true);
        result.setReservation_info(doc.select(".detail_vorbest a").attr("href"));
    }
    return result;
}

From source file:de.geeksfactory.opacclient.apis.Bibliotheca.java

@Override
public ReservationResult reservation(DetailledItem item, Account acc, int useraction, String selection)
        throws IOException {
    String reservation_info = item.getReservation_info();

    Document doc = null;//from  ww  w . ja va  2 s.co m

    if (useraction == MultiStepResult.ACTION_CONFIRMATION) {
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair("make_allvl", "Bestaetigung"));
        nameValuePairs.add(new BasicNameValuePair("target", "makevorbest"));
        httpPost(opac_url + "/index.asp", new UrlEncodedFormEntity(nameValuePairs), getDefaultEncoding());
        return new ReservationResult(MultiStepResult.Status.OK);
    } else if (selection == null || useraction == 0) {
        String html = httpGet(opac_url + "/" + reservation_info, getDefaultEncoding());
        doc = Jsoup.parse(html);

        if (doc.select("input[name=AUSWEIS]").size() > 0) {
            // Needs login
            List<NameValuePair> nameValuePairs = new ArrayList<>(2);
            nameValuePairs.add(new BasicNameValuePair("AUSWEIS", acc.getName()));
            nameValuePairs.add(new BasicNameValuePair("PWD", acc.getPassword()));
            if (data.has("db")) {
                try {
                    nameValuePairs.add(new BasicNameValuePair("vkontodb", data.getString("db")));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            nameValuePairs.add(new BasicNameValuePair("B1", "weiter"));
            nameValuePairs.add(new BasicNameValuePair("target", doc.select("input[name=target]").val()));
            nameValuePairs.add(new BasicNameValuePair("type", "VT2"));
            html = httpPost(opac_url + "/index.asp", new UrlEncodedFormEntity(nameValuePairs),
                    getDefaultEncoding());
            doc = Jsoup.parse(html);
        }
        if (doc.select("select[name=" + branch_inputfield + "]").size() == 0) {
            if (doc.select("select[name=VZST]").size() > 0) {
                branch_inputfield = "VZST";
            }
        }
        if (doc.select("select[name=" + branch_inputfield + "]").size() > 0) {
            List<Map<String, String>> branches = new ArrayList<>();
            for (Element option : doc.select("select[name=" + branch_inputfield + "]").first().children()) {
                String value = option.text().trim();
                String key;
                if (option.hasAttr("value")) {
                    key = option.attr("value");
                } else {
                    key = value;
                }
                Map<String, String> selopt = new HashMap<>();
                selopt.put("key", key);
                selopt.put("value", value);
                branches.add(selopt);
            }
            _res_target = doc.select("input[name=target]").attr("value");
            ReservationResult result = new ReservationResult(MultiStepResult.Status.SELECTION_NEEDED);
            result.setActionIdentifier(ReservationResult.ACTION_BRANCH);
            result.setSelection(branches);
            return result;
        }
    } else if (useraction == ReservationResult.ACTION_BRANCH) {
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair(branch_inputfield, selection));
        nameValuePairs.add(new BasicNameValuePair("button2", "weiter"));
        nameValuePairs.add(new BasicNameValuePair("target", _res_target));
        String html = httpPost(opac_url + "/index.asp", new UrlEncodedFormEntity(nameValuePairs),
                getDefaultEncoding());
        doc = Jsoup.parse(html);
    }

    if (doc == null) {
        return new ReservationResult(MultiStepResult.Status.ERROR);
    }

    if (doc.select("input[name=target]").size() > 0) {
        if (doc.select("input[name=target]").attr("value").equals("makevorbest")) {
            List<String[]> details = new ArrayList<>();

            if (doc.getElementsByClass("kontomeldung").size() == 1) {
                details.add(new String[] { doc.getElementsByClass("kontomeldung").get(0).text().trim() });
            }
            Pattern p = Pattern.compile("geb.hr", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
            for (Element div : doc.select(".kontozeile_center")) {
                for (String text : Jsoup.parse(div.html().replaceAll("(?i)<br[^>]*>", "br2n")).text()
                        .split("br2n")) {
                    if (p.matcher(text).find() && !text.contains("usstehend")
                            && text.contains("orbestellung")) {
                        details.add(new String[] { text.trim() });
                    }
                }
            }

            if (doc.select("#vorbest").size() > 0 && doc.select("#vorbest").val().contains("(")) {
                // Erlangen uses "Kostenpflichtige Vorbestellung (1 Euro)"
                // as the label of its reservation button
                details.add(new String[] { doc.select("#vorbest").val().trim() });
            }

            for (Element row : doc.select(".kontozeile_center table tr")) {
                if (row.select(".konto_feld").size() == 1 && row.select(".konto_feldinhalt").size() == 1) {
                    details.add(new String[] { row.select(".konto_feld").text().trim(),
                            row.select(".konto_feldinhalt").text().trim() });
                }
            }
            ReservationResult result = new ReservationResult(MultiStepResult.Status.CONFIRMATION_NEEDED);
            result.setDetails(details);
            return result;
        }
    }

    if (doc.getElementsByClass("kontomeldung").size() == 1) {
        return new ReservationResult(MultiStepResult.Status.ERROR,
                doc.getElementsByClass("kontomeldung").get(0).text());
    }

    return new ReservationResult(MultiStepResult.Status.ERROR,
            stringProvider.getString(StringProvider.UNKNOWN_ERROR));
}

From source file:de.geeksfactory.opacclient.apis.TouchPoint.java

protected DetailledItem parse_result(String html) throws IOException {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);//w  ww  .  j a v  a  2s .  c  o  m

    DetailledItem result = new DetailledItem();

    if (doc.select("#cover script").size() > 0) {
        String js = doc.select("#cover script").first().html();
        String isbn = matchJSVariable(js, "isbn");
        String ajaxUrl = matchJSVariable(js, "ajaxUrl");
        if (ajaxUrl == null) {
            ajaxUrl = matchJSParameter(js, "url");
        }
        if (ajaxUrl != null && !"".equals(ajaxUrl)) {
            if (!"".equals(isbn) && isbn != null) {
                String url = new URL(new URL(opac_url + "/"), ajaxUrl).toString();
                String coverUrl = httpGet(url + "?isbn=" + isbn + "&size=medium", ENCODING);
                if (!"".equals(coverUrl)) {
                    result.setCover(coverUrl.replace("\r\n", "").trim());
                }
            } else {
                String url = new URL(new URL(opac_url + "/"), ajaxUrl).toString();
                String coverJs = httpGet(url, ENCODING);
                result.setCover(matchHTMLAttr(coverJs, "src"));
            }
        }
    }

    result.setTitle(doc.select("h1").first().text());
    for (Element tr : doc.select(".titleinfo tr")) {
        // Sometimes there is one th and one td, sometimes two tds
        String detailName = tr.select("th, td").first().text().trim();
        String detailValue = tr.select("td").last().text().trim();
        result.addDetail(new Detail(detailName, detailValue));
        if (detailName.contains("ID in diesem Katalog")) {
            result.setId(detailValue);
        }
    }
    if (result.getDetails().size() == 0 && doc.select("#details").size() > 0) {
        // e.g. Bayreuth_Uni
        String dname = "";
        String dval = "";
        boolean in_value = true;
        for (Node n : doc.select("#details").first().childNodes()) {
            if (n instanceof Element && ((Element) n).tagName().equals("strong")) {
                if (in_value) {
                    if (dname.length() > 0 && dval.length() > 0) {
                        result.addDetail(new Detail(dname, dval));
                    }
                    dname = ((Element) n).text();
                    in_value = false;
                } else {
                    dname += ((Element) n).text();
                }
            } else {
                String t = null;
                if (n instanceof TextNode) {
                    t = ((TextNode) n).text();
                } else if (n instanceof Element) {
                    t = ((Element) n).text();
                }
                if (t != null) {
                    if (in_value) {
                        dval += t;
                    } else {
                        in_value = true;
                        dval = t;
                    }
                }
            }
        }

    }

    // Copies
    String copiesParameter = doc.select("div[id^=ajax_holdings_url").attr("ajaxParameter").replace("&amp;", "");
    if (!"".equals(copiesParameter)) {
        String copiesHtml = httpGet(opac_url + "/" + copiesParameter, ENCODING);
        Document copiesDoc = Jsoup.parse(copiesHtml);
        List<String> table_keys = new ArrayList<>();
        for (Element th : copiesDoc.select(".data tr th")) {
            if (th.text().contains("Zweigstelle")) {
                table_keys.add("branch");
            } else if (th.text().contains("Status")) {
                table_keys.add("status");
            } else if (th.text().contains("Signatur")) {
                table_keys.add("signature");
            } else {
                table_keys.add(null);
            }
        }
        for (Element tr : copiesDoc.select(".data tr:has(td)")) {
            Copy copy = new Copy();
            int i = 0;
            for (Element td : tr.select("td")) {
                if (table_keys.get(i) != null) {
                    copy.set(table_keys.get(i), td.text().trim());
                }
                i++;
            }
            result.addCopy(copy);
        }
    }

    // Reservation Info, only works if the code above could find a URL
    if (!"".equals(copiesParameter)) {
        String reservationParameter = copiesParameter.replace("showHoldings", "showDocument");
        try {
            String reservationHtml = httpGet(opac_url + "/" + reservationParameter, ENCODING);
            Document reservationDoc = Jsoup.parse(reservationHtml);
            reservationDoc.setBaseUri(opac_url);
            if (reservationDoc.select("a").size() == 1) {
                result.setReservable(true);
                result.setReservation_info(reservationDoc.select("a").first().attr("abs:href"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            // fail silently
        }
    }

    // TODO: Volumes

    try {
        Element isvolume = null;
        Map<String, String> volume = new HashMap<>();
        Elements links = doc.select(".data td a");
        int elcount = links.size();
        for (int eli = 0; eli < elcount; eli++) {
            List<NameValuePair> anyurl = URLEncodedUtils.parse(new URI(links.get(eli).attr("href")), "UTF-8");
            for (NameValuePair nv : anyurl) {
                if (nv.getName().equals("methodToCall") && nv.getValue().equals("volumeSearch")) {
                    isvolume = links.get(eli);
                } else if (nv.getName().equals("catKey")) {
                    volume.put("catKey", nv.getValue());
                } else if (nv.getName().equals("dbIdentifier")) {
                    volume.put("dbIdentifier", nv.getValue());
                }
            }
            if (isvolume != null) {
                volume.put("volume", "true");
                result.setVolumesearch(volume);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:de.geeksfactory.opacclient.apis.Bibliotheca.java

@Override
public List<SearchField> getSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();/*from   w w w .  ja  v  a2 s  . c  o m*/
    }

    List<SearchField> fields = new ArrayList<>();
    // Read branches and media types
    List<NameValuePair> nameValuePairs = new ArrayList<>(2);
    nameValuePairs.add(new BasicNameValuePair("link_profis.x", "0"));
    nameValuePairs.add(new BasicNameValuePair("link_profis.y", "1"));
    String html = httpPost(opac_url + "/index.asp", new UrlEncodedFormEntity(nameValuePairs),
            getDefaultEncoding());
    Document doc = Jsoup.parse(html);

    Elements fieldElems = doc.select(".suchfeldinhalt");
    for (Element fieldElem : fieldElems) {
        String name = fieldElem.select(".suchfeld_inhalt_titel label").text();
        String hint = "";
        if (fieldElem.select(".suchfeld_inhalt_input").size() > 0) {
            List<TextNode> textNodes = fieldElem.select(".suchfeld_inhalt_input").first().textNodes();
            if (textNodes.size() > 0) {
                for (TextNode node : textNodes) {
                    String text = node.getWholeText().replace("\n", "");
                    if (!text.equals("")) {
                        hint = node.getWholeText().replace("\n", "");
                        break;
                    }
                }
            }
        }

        Elements inputs = fieldElem
                .select(".suchfeld_inhalt_input input[type=text], " + ".suchfeld_inhalt_input select");
        if (inputs.size() == 1) {
            fields.add(createSearchField(name, hint, inputs.get(0)));
        } else if (inputs.size() == 2 && inputs.select("input[type=text]").size() == 2) {
            // Two text fields, e.g. year from/to or two keywords
            fields.add(createSearchField(name, hint, inputs.get(0)));
            TextSearchField secondField = (TextSearchField) createSearchField(name, hint, inputs.get(1));
            secondField.setHalfWidth(true);
            fields.add(secondField);
        } else if (inputs.size() == 2 && inputs.get(0).tagName().equals("select")
                && inputs.get(1).tagName().equals("input") && inputs.get(0).attr("name").equals("feld1")) {
            // A dropdown to select from different search field types.
            // Break it down into single text fields.
            for (Element option : inputs.get(0).select("option")) {
                TextSearchField field = new TextSearchField();
                field.setHint(hint);
                field.setDisplayName(option.text());
                field.setId(inputs.get(1).attr("name") + "$" + option.attr("value"));

                JSONObject data = new JSONObject();
                JSONObject params = new JSONObject();
                params.put(inputs.get(0).attr("name"), option.attr("value"));
                data.put("additional_params", params);
                field.setData(data);

                fields.add(field);
            }
        }
    }

    DropdownSearchField orderField = new DropdownSearchField("orderselect",
            stringProvider.getString(StringProvider.ORDER), false, null);
    orderField.addDropdownValue("1", stringProvider.getString(StringProvider.ORDER_DEFAULT));
    orderField.addDropdownValue("2:desc", stringProvider.getString(StringProvider.ORDER_YEAR_DESC));
    orderField.addDropdownValue("2:asc", stringProvider.getString(StringProvider.ORDER_YEAR_ASC));
    orderField.addDropdownValue("3:desc", stringProvider.getString(StringProvider.ORDER_CATEGORY_DESC));
    orderField.addDropdownValue("3:asc", stringProvider.getString(StringProvider.ORDER_CATEGORY_ASC));
    orderField.setMeaning(Meaning.ORDER);
    fields.add(orderField);

    return fields;
}

From source file:de.geeksfactory.opacclient.apis.TouchPoint.java

@Override
public ReservationResult reservation(DetailledItem item, Account acc, int useraction, String selection)
        throws IOException {
    if (System.currentTimeMillis() - logged_in > SESSION_LIFETIME || logged_in_as == null) {
        try {/*from w w  w  . ja  v  a 2s .co m*/
            login(acc);
        } catch (OpacErrorException e) {
            return new ReservationResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    } else if (logged_in_as.getId() != acc.getId()) {
        try {
            login(acc);
        } catch (OpacErrorException e) {
            return new ReservationResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    }
    String html;
    if (reusehtml_reservation != null) {
        html = reusehtml_reservation;
    } else {
        html = httpGet(item.getReservation_info(), ENCODING);
    }
    Document doc = Jsoup.parse(html);
    if (doc.select(".message-error").size() > 0) {
        return new ReservationResult(MultiStepResult.Status.ERROR, doc.select(".message-error").first().text());
    }
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    nameValuePairs.add(new BasicNameValuePair("methodToCall", "requestItem"));
    if (doc.select("#newNeedBeforeDate").size() > 0) {
        nameValuePairs.add(new BasicNameValuePair("newNeedBeforeDate", doc.select("#newNeedBeforeDate").val()));
    }
    if (doc.select("select[name=location] option").size() > 0 && selection == null) {
        Elements options = doc.select("select[name=location] option");
        ReservationResult res = new ReservationResult(MultiStepResult.Status.SELECTION_NEEDED);
        List<Map<String, String>> optionsMap = new ArrayList<>();
        for (Element option : options) {
            Map<String, String> selopt = new HashMap<>();
            selopt.put("key", option.attr("value"));
            selopt.put("value", option.text());
            optionsMap.add(selopt);
        }
        res.setSelection(optionsMap);
        res.setMessage(doc.select("label[for=location]").text());
        reusehtml_reservation = html;
        return res;
    } else if (selection != null) {
        nameValuePairs.add(new BasicNameValuePair("location", selection));
        reusehtml_reservation = null;
    }

    html = httpPost(opac_url + "/requestItem.do", new UrlEncodedFormEntity(nameValuePairs), ENCODING);
    doc = Jsoup.parse(html);
    if (doc.select(".message-confirm").size() > 0) {
        return new ReservationResult(MultiStepResult.Status.OK);
    } else if (doc.select(".alert").size() > 0) {
        return new ReservationResult(MultiStepResult.Status.ERROR, doc.select(".alert").text());
    } else {
        return new ReservationResult(MultiStepResult.Status.ERROR);
    }
}