Example usage for org.jsoup.nodes Element select

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

Introduction

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

Prototype

public Elements select(String cssQuery) 

Source Link

Document

Find elements that match the Selector CSS query, with this element as the starting context.

Usage

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

@Override
public ProlongAllResult prolongAll(Account account, int useraction, String selection) throws IOException {

    if (!initialised) {
        start();//from w w  w. j ava  2s. c  o  m
    }
    if (System.currentTimeMillis() - logged_in > SESSION_LIFETIME || logged_in_as == null) {
        try {
            account(account);
        } catch (JSONException e) {
            e.printStackTrace();
            return new ProlongAllResult(MultiStepResult.Status.ERROR,
                    stringProvider.getString(StringProvider.CONNECTION_ERROR));
        } catch (OpacErrorException e) {
            return new ProlongAllResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    } else if (logged_in_as.getId() != account.getId()) {
        try {
            account(account);
        } catch (JSONException e) {
            e.printStackTrace();
            return new ProlongAllResult(MultiStepResult.Status.ERROR,
                    stringProvider.getString(StringProvider.CONNECTION_ERROR));
        } catch (OpacErrorException e) {
            return new ProlongAllResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    }
    String html = httpGet(opac_url + "/index.asp?target=alleverl", getDefaultEncoding());
    Document doc = Jsoup.parse(html);

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

    if (doc.select(".kontozeile table").size() == 1) {
        Map<Integer, String> colmap = new HashMap<>();
        List<Map<String, String>> result = new ArrayList<>();
        for (Element tr : doc.select(".kontozeile table tr")) {
            if (tr.select(".tabHeaderKonto").size() > 0) {
                int i = 0;
                for (Element th : tr.select("th")) {
                    if (th.text().contains("Verfasser")) {
                        colmap.put(i, OpacApi.ProlongAllResult.KEY_LINE_AUTHOR);
                    } else if (th.text().contains("Titel")) {
                        colmap.put(i, OpacApi.ProlongAllResult.KEY_LINE_TITLE);
                    } else if (th.text().contains("Neue")) {
                        colmap.put(i, OpacApi.ProlongAllResult.KEY_LINE_NEW_RETURNDATE);
                    } else if (th.text().contains("Frist")) {
                        colmap.put(i, OpacApi.ProlongAllResult.KEY_LINE_OLD_RETURNDATE);
                    } else if (th.text().contains("Status")) {
                        colmap.put(i, OpacApi.ProlongAllResult.KEY_LINE_MESSAGE);
                    }
                    i++;
                }
            } else {
                Map<String, String> line = new HashMap<>();
                for (Entry<Integer, String> entry : colmap.entrySet()) {
                    line.put(entry.getValue(), tr.child(entry.getKey()).text().trim());
                }
                result.add(line);
            }
        }

        if (doc.select("input#make_allvl").size() > 0) {
            List<NameValuePair> nameValuePairs = new ArrayList<>(2);
            nameValuePairs.add(new BasicNameValuePair("target", "make_allvl_flag"));
            nameValuePairs.add(new BasicNameValuePair("make_allvl", "Bestaetigung"));
            httpPost(opac_url + "/index.asp", new UrlEncodedFormEntity(nameValuePairs), getDefaultEncoding());
        }

        return new ProlongAllResult(MultiStepResult.Status.OK, result);
    }

    return new ProlongAllResult(MultiStepResult.Status.ERROR,
            stringProvider.getString(StringProvider.INTERNAL_ERROR));
}

From source file:org.apache.sling.hapi.client.forms.internal.FormValues.java

/**
 * @return//from   w  w w.  j a  va  2s .c  om
 * {@see http://www.w3.org/TR/html5/forms.html#constructing-the-form-data-set}
 */
private FormValues build() {
    for (Element input : form.select("button, input, select, textarea")) {
        String type = input.attr("type");

        if (input.hasAttr("disabled"))
            continue;
        if (input.tagName().equalsIgnoreCase("button") && !type.equals("submit"))
            continue;
        if (input.tagName().equalsIgnoreCase("input") && (type.equals("button") || type.equals("reset")))
            continue;
        if (type.equals("checkbox") && input.hasAttr("checked"))
            continue;
        if (type.equals("radio") && input.hasAttr("checked"))
            continue;
        if (!type.equals("image") && input.attr("name").length() == 0)
            continue;
        if (input.parents().is("datalist"))
            continue;

        if (type.equals("image") || type.equals("file"))
            continue; // don't support files for now
        String name = input.attr("name");

        if (input.tagName().equalsIgnoreCase("select")) {
            for (Element o : input.select("option[selected]")) {
                if (o.hasAttr("disabled"))
                    continue;
                list.add(name, new BasicNameValuePair(name, o.val()));
            }
        } else if (type.equals("checkbox") || type.equals("radio")) {
            String value = input.hasAttr("value") ? input.val() : "on";
            list.add(name, new BasicNameValuePair(name, value));
        } else {
            list.add(name, new BasicNameValuePair(name, input.val()));
        }
    }
    return this;
}

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

private String generateQuery(Element form) throws UnsupportedEncodingException {
    StringBuilder builder = new StringBuilder();
    builder.append(form.attr("action").substring(1));
    int i = 0;/*from  w  w  w  .j a va 2 s.  c om*/
    for (Element input : form.select("input")) {
        builder.append(i == 0 ? "?" : "&");
        builder.append(input.attr("name")).append("=").append(URLEncoder.encode(input.attr("value"), "UTF-8"));
        i++;
    }
    return builder.toString();
}

From source file:com.github.binlee1990.spider.movie.spider.MovieCrawler.java

private List<Actor> createOrGetActorList(Document doc) {
    List<Actor> actorList = Lists.newArrayList();

    Elements keyElements = doc.select(".fm-minfo dt");
    Elements valueElements = doc.select(".fm-minfo dd");
    if (CollectionUtils.isNotEmpty(keyElements) && CollectionUtils.isNotEmpty(valueElements)) {
        int keyI = 0;
        for (; keyI < keyElements.size(); keyI++) {
            Element keyElement = keyElements.get(keyI);
            Element valueElement = valueElements.get(keyI);

            if (null != keyElement && null != valueElement) {
                String key = StringUtils.trimToEmpty(keyElement.text().toString());
                if (StringUtils.isNotBlank(key)) {
                    String value = StringUtils.trimToEmpty(valueElement.text().toString());

                    if (StringUtils.equalsIgnoreCase(key, "")) {
                        Elements actorNameElements = valueElement.select("a");
                        if (CollectionUtils.isNotEmpty(actorNameElements)) {
                            actorNameElements.forEach(actorNameElement -> {
                                String actorName = StringUtils.trimToEmpty(actorNameElement.text().toString());
                                if (StringUtils.isNotBlank(actorName)) {
                                    Actor actor = createOrQueryActor(actorName);
                                    if (null != actor) {
                                        actorList.add(actor);
                                    }// ww  w  .  j a va 2  s  .  c o  m
                                }
                            });
                        }

                        break;
                    }
                }
            }
        }
    }

    return actorList;
}

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

@Override
public AccountData account(Account acc)
        throws IOException, NotReachableException, JSONException, SocketException, OpacErrorException {
    Document login = login(acc);/*from   w ww.  j  a  va2 s  .  co  m*/
    if (login == null)
        return null;

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

    String lent_link = null;
    String res_link = 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")) {
            lent_link = td.select("a").attr("href");
            lent_cnt = Integer.parseInt(td.nextElementSibling().text().trim());
        } else if (section.contains("Vormerkungen")) {
            res_link = 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());
        }
    }
    assert (lent_cnt >= 0);
    assert (res_cnt >= 0);
    if (lent_link == null)
        return null;

    String lent_html = httpGet(opac_url + "/" + lent_link.replace("utf-8?Method", "utf-8&Method"),
            getDefaultEncoding());
    Document lent_doc = Jsoup.parse(lent_html);
    List<Map<String, String>> lent = new ArrayList<Map<String, String>>();

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.GERMAN);
    Pattern id_pat = Pattern.compile("javascript:renewItem\\('[0-9]+','(.*)'\\)");

    for (Element table : lent_doc
            .select(".LoansBrowseItemDetailsCellStripe table, .LoansBrowseItemDetailsCell table")) {
        Map<String, String> item = new HashMap<String, String>();

        for (Element tr : table.select("tr")) {
            String desc = tr.select(".LoanBrowseFieldNameCell").text().trim();
            String value = tr.select(".LoanBrowseFieldDataCell").text().trim();
            if (desc.equals("Titel"))
                item.put(AccountData.KEY_LENT_TITLE, value);
            if (desc.equals("Verfasser"))
                item.put(AccountData.KEY_LENT_AUTHOR, value);
            if (desc.equals("Mediennummer"))
                item.put(AccountData.KEY_LENT_BARCODE, value);
            if (desc.equals("ausgeliehen in"))
                item.put(AccountData.KEY_LENT_BRANCH, value);
            if (desc.matches("F.+lligkeits.*datum")) {
                value = value.split(" ")[0];
                item.put(AccountData.KEY_LENT_DEADLINE, value);
                try {
                    item.put(AccountData.KEY_LENT_DEADLINE_TIMESTAMP,
                            String.valueOf(sdf.parse(value).getTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        if (table.select(".button[Title~=Zum]").size() == 1) {
            Matcher matcher1 = id_pat.matcher(table.select(".button[Title~=Zum]").attr("href"));
            if (matcher1.matches()) {
                item.put(AccountData.KEY_LENT_LINK, matcher1.group(1));
            }
        }
        lent.add(item);
    }
    res.setLent(lent);
    assert (lent_cnt <= lent.size());

    List<Map<String, String>> reservations = new ArrayList<Map<String, String>>();
    String res_html = httpGet(opac_url + "/" + res_link, getDefaultEncoding());
    Document res_doc = Jsoup.parse(res_html);

    for (Element table : res_doc
            .select(".MessageBrowseItemDetailsCell table, .MessageBrowseItemDetailsCellStripe table")) {
        Map<String, String> item = new HashMap<String, String>();

        for (Element tr : table.select("tr")) {
            String desc = tr.select(".MessageBrowseFieldNameCell").text().trim();
            String value = tr.select(".MessageBrowseFieldDataCell").text().trim();
            if (desc.equals("Titel"))
                item.put(AccountData.KEY_RESERVATION_TITLE, value);
            if (desc.equals("Publikationsform"))
                item.put(AccountData.KEY_RESERVATION_FORMAT, value);
            if (desc.equals("Liefern an"))
                item.put(AccountData.KEY_RESERVATION_BRANCH, value);
            if (desc.equals("Status"))
                item.put(AccountData.KEY_RESERVATION_READY, value);
        }
        if ("Gelscht".equals(item.get(AccountData.KEY_RESERVATION_READY))) {
            continue;
        }
        reservations.add(item);
    }
    res.setReservations(reservations);
    assert (reservations.size() >= res_cnt);

    return res;
}

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

/**
 * Better version of JSoup's implementation of this function ({@link
 * org.jsoup.nodes.FormElement#formData()}).
 *
 * @param form       The form to submit/*from  w  w w.  j a va 2s .c  o  m*/
 * @param submitName The name attribute of the button which is clicked to submit the form, or
 *                   null
 * @return A MultipartEntityBuilder containing the data of the form
 */
protected MultipartEntityBuilder formData(FormElement form, String submitName) {
    MultipartEntityBuilder data = MultipartEntityBuilder.create();
    data.setLaxMode();

    // iterate the form control elements and accumulate their values
    for (Element el : form.elements()) {
        if (!el.tag().isFormSubmittable()) {
            continue; // contents are form listable, superset of submitable
        }
        String name = el.attr("name");
        if (name.length() == 0)
            continue;
        String type = el.attr("type");

        if ("select".equals(el.tagName())) {
            Elements options = el.select("option[selected]");
            boolean set = false;
            for (Element option : options) {
                data.addTextBody(name, option.val());
                set = true;
            }
            if (!set) {
                Element option = el.select("option").first();
                if (option != null) {
                    data.addTextBody(name, option.val());
                }
            }
        } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) {
            // only add checkbox or radio if they have the checked attribute
            if (el.hasAttr("checked")) {
                data.addTextBody(name, el.val().length() > 0 ? el.val() : "on");
            }
        } else if ("submit".equalsIgnoreCase(type) || "image".equalsIgnoreCase(type)) {
            if (submitName != null && el.attr("name").contains(submitName)) {
                data.addTextBody(name, el.val());
            }
        } else {
            data.addTextBody(name, el.val());
        }
    }
    return data;
}

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

protected SearchRequestResult parse_search(String html, int page) {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);//  w  ww  .  ja v  a  2 s. c  o m
    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;
        }
    }
    return new SearchRequestResult(results, results_total, page);
}

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

private SearchRequestResult parse_search(String html, int page) throws OpacErrorException {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url + "/APS_PRESENT_BIB");

    if (doc.select("#ErrorAdviceRow").size() > 0) {
        throw new OpacErrorException(doc.select("#ErrorAdviceRow").text().trim());
    }/*from  w  w  w  .  j a  v  a  2  s  . c  o m*/

    int results_total = -1;

    String searchHitsQuery = version18 ? "td:containsOwn(Total)" : ".searchHits";
    if (doc.select(searchHitsQuery).size() > 0) {
        results_total = Integer.parseInt(
                doc.select(searchHitsQuery).first().text().trim().replaceAll(".*\\(([0-9]+)\\).*", "$1"));
    } else if (doc.select("span:matches(\\[\\d+/\\d+\\])").size() > 0) {
        // Zones 1.8 - searchGetPage
        String text = doc.select("span:matches(\\[\\d+/\\d+\\])").text();
        Pattern pattern = Pattern.compile("\\[\\d+/(\\d+)\\]");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            results_total = Integer.parseInt(matcher.group(1));
        }
    }

    if (doc.select(".pageNavLink").size() > 0) {
        // Zones 2.2
        searchobj = doc.select(".pageNavLink").first().attr("href").split("\\?")[0];
    } else if (doc.select("div[targetObject]").size() > 0) {
        // Zones 1.8 - search
        searchobj = doc.select("div[targetObject]").attr("targetObject").split("\\?")[0];
    } else {
        // Zones 1.8 - searchGetPage

        // The page contains a data structure that at first glance seems to be JSON, but uses
        // "=" instead of ":". So we parse it using regex...
        Pattern pattern = Pattern.compile("targetObject = \"([^\\?]+)[^\"]+\"");
        Matcher matcher = pattern.matcher(doc.html());
        if (matcher.find()) {
            searchobj = matcher.group(1);
        }
    }

    Elements table = doc.select("#BrowseList > tbody > tr," // Zones 2.2
            + " .inRoundBox1" // Zones 1.8
    );
    List<SearchResult> results = new ArrayList<>();
    for (int i = 0; i < table.size(); i++) {
        Element tr = table.get(i);
        SearchResult sr = new SearchResult();

        String typetext;
        if (version18) {
            String[] parts = tr.select("img[src^=IMG/MAT]").attr("src").split("/");
            typetext = parts[parts.length - 1].replace(".gif", "");
        } else {
            typetext = tr.select(".SummaryMaterialTypeField").text().replace("\n", " ").trim();
        }

        if (data.has("mediatypes")) {
            try {
                sr.setType(MediaType.valueOf(data.getJSONObject("mediatypes").getString(typetext)));
            } catch (JSONException | IllegalArgumentException e) {
                sr.setType(defaulttypes.get(typetext));
            }
        } else {
            sr.setType(defaulttypes.get(typetext));
        }

        String imgUrl = null;
        if (version18) {
            if (tr.select("a[title=Titelbild]").size() > 0) {
                imgUrl = tr.select("a[title=Titelbild]").attr("href");
            } else if (tr.select("img[width=50]").size() > 0) {
                // TODO: better way to select these cover images? (found in Hannover)
                imgUrl = tr.select("img[width=50]").attr("src");
            }
        } else {
            if (tr.select(".SummaryImageCell img[id^=Bookcover]").size() > 0) {
                imgUrl = tr.select(".SummaryImageCell img[id^=Bookcover]").first().attr("src");
            }
        }
        sr.setCover(imgUrl);

        if (version18) {
            if (tr.select("img[src$=oci_1.gif]").size() > 0) {
                // probably can only appear when searching the catalog on a terminal in
                // the library.
                sr.setStatus(SearchResult.Status.GREEN);
            } else if (tr.select("img[src$=blob_amber.gif]").size() > 0) {
                sr.setStatus(SearchResult.Status.YELLOW);
            }
        }

        String desc = "";
        String childrenQuery = version18 ? "table[cellpadding=1] tr"
                : ".SummaryDataCell tr, .SummaryDataCellStripe tr";
        Elements children = tr.select(childrenQuery);
        int childrennum = children.size();
        boolean haslink = false;

        for (int ch = 0; ch < childrennum; ch++) {
            Element node = children.get(ch);
            if (getName(node).equals("Titel")) {
                desc += "<b>" + getValue(node).trim() + "</b><br />";
            } else if (getName(node).equals("Verfasser") || getName(node).equals("Jahr")) {
                desc += getValue(node).trim() + "<br />";
            }

            String linkSelector = version18 ? "a[href*=ShowStock], a[href*=APS_CAT_IDENTIFY]"
                    : ".SummaryFieldData a.SummaryFieldLink";
            if (node.select(linkSelector).size() > 0 && !haslink) {
                String href = node.select(linkSelector).attr("abs:href");
                Map<String, String> hrefq = getQueryParamsFirst(href);
                if (hrefq.containsKey("no")) {
                    sr.setId(hrefq.get("no"));
                } else if (hrefq.containsKey("Key")) {
                    sr.setId(hrefq.get("Key"));
                }
                haslink = true;
            }
        }
        if (desc.endsWith("<br />")) {
            desc = desc.substring(0, desc.length() - 6);
        }
        sr.setInnerhtml(desc);
        sr.setNr(i);

        results.add(sr);
    }

    return new SearchRequestResult(results, results_total, page);
}

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 www.ja  va  2s. 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.Open.java

@Override
public List<SearchField> getSearchFields() throws IOException, OpacErrorException, JSONException {
    String url = opac_url + "/" + data.getJSONObject("urls").getString("advanced_search") + NO_MOBILE;
    Document doc = Jsoup.parse(httpGet(url, getDefaultEncoding()));

    Element table = doc.select(".ModOPENExtendedSearchModuleC table").first();

    List<SearchField> fields = new ArrayList<>();

    JSONObject selectable = new JSONObject();
    selectable.put("selectable", true);

    JSONObject notSelectable = new JSONObject();
    notSelectable.put("selectable", false);

    // Selectable search criteria
    Elements options = table.select("select[id$=FirstSearchField] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setId(option.val());
        field.setDisplayName(option.text());
        field.setData(selectable);//from ww w.jav a2 s . c  o  m
        fields.add(field);
    }

    // More criteria
    Element moreHeader = table.select("span[id$=LblMoreCriterias]").parents().select("tr").first();
    if (moreHeader != null) {
        Elements siblings = moreHeader.siblingElements();
        int startIndex = moreHeader.elementSiblingIndex();
        for (int i = startIndex; i < siblings.size(); i++) {
            Element tr = siblings.get(i);
            if (tr.select("input, select").size() == 0)
                continue;

            if (tr.select("input[type=text]").size() == 1) {
                Element input = tr.select("input[type=text]").first();
                TextSearchField field = new TextSearchField();
                field.setId(input.attr("name"));
                field.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field.setData(notSelectable);
                if (tr.text().contains("nur Ziffern"))
                    field.setNumber(true);
                fields.add(field);
            } else if (tr.select("input[type=text]").size() == 2) {
                Element input1 = tr.select("input[type=text]").get(0);
                Element input2 = tr.select("input[type=text]").get(1);

                TextSearchField field1 = new TextSearchField();
                field1.setId(input1.attr("name"));
                field1.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field1.setData(notSelectable);
                if (tr.text().contains("nur Ziffern"))
                    field1.setNumber(true);
                fields.add(field1);

                TextSearchField field2 = new TextSearchField();
                field2.setId(input2.attr("name"));
                field2.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                field2.setData(notSelectable);
                field2.setHalfWidth(true);
                if (tr.text().contains("nur Ziffern"))
                    field2.setNumber(true);
                fields.add(field2);
            } else if (tr.select("select").size() == 1) {
                Element select = tr.select("select").first();
                DropdownSearchField dropdown = new DropdownSearchField();
                dropdown.setId(select.attr("name"));
                dropdown.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                List<DropdownSearchField.Option> values = new ArrayList<>();
                for (Element option : select.select("option")) {
                    DropdownSearchField.Option opt = new DropdownSearchField.Option(option.val(),
                            option.text());
                    values.add(opt);
                }
                dropdown.setDropdownValues(values);
                fields.add(dropdown);
            } else if (tr.select("input[type=checkbox]").size() == 1) {
                Element checkbox = tr.select("input[type=checkbox]").first();
                CheckboxSearchField field = new CheckboxSearchField();
                field.setId(checkbox.attr("name"));
                field.setDisplayName(tr.select("span[id*=Lbl]").first().text());
                fields.add(field);
            }
        }
    }
    return fields;
}