Example usage for org.jsoup.nodes FormElement attr

List of usage examples for org.jsoup.nodes FormElement attr

Introduction

In this page you can find the example usage for org.jsoup.nodes FormElement attr.

Prototype

public String attr(String attributeKey) 

Source Link

Document

Get an attribute's value by its key.

Usage

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

@Override
public SearchRequestResult search(List<SearchQuery> queries)
        throws IOException, OpacErrorException, JSONException {
    String url = opac_url + "/" + data.getJSONObject("urls").getString("advanced_search") + NO_MOBILE;
    Document doc = Jsoup.parse(httpGet(url, getDefaultEncoding()));
    doc.setBaseUri(url);//from  ww  w  .  j  av  a 2  s  .  c o m

    int selectableCount = 0;
    for (SearchQuery query : queries) {
        if (query.getValue().equals("") || query.getValue().equals("false"))
            continue;

        if (query.getSearchField() instanceof TextSearchField) {
            TextSearchField field = (TextSearchField) query.getSearchField();
            if (field.getData().getBoolean("selectable")) {
                selectableCount++;
                if (selectableCount > 3) {
                    throw new OpacErrorException(
                            stringProvider.getQuantityString(StringProvider.LIMITED_NUM_OF_CRITERIA, 3, 3));
                }
                String number = numberToText(selectableCount);
                Element searchField = doc.select("select[name$=" + number + "SearchField]").first();
                Element searchValue = doc.select("input[name$=" + number + "SearchValue]").first();
                searchField.val(field.getId());
                searchValue.val(query.getValue());
            } else {
                Element input = doc.select("input[name=" + field.getId() + "]").first();
                input.val(query.getValue());
            }
        } else if (query.getSearchField() instanceof DropdownSearchField) {
            DropdownSearchField field = (DropdownSearchField) query.getSearchField();
            Element input = doc.select("select[name=" + field.getId() + "]").first();
            input.val(query.getValue());
        } else if (query.getSearchField() instanceof CheckboxSearchField) {
            CheckboxSearchField field = (CheckboxSearchField) query.getSearchField();
            Element input = doc.select("input[name=" + field.getId() + "]").first();
            input.attr("checked", query.getValue());
        }
    }

    // Submit form
    FormElement form = (FormElement) doc.select("form").first();
    HttpEntity data = formData(form, "BtnSearch").build();
    String postUrl = form.attr("abs:action");

    String html = httpPost(postUrl, data, "UTF-8");
    Document doc2 = Jsoup.parse(html);
    doc2.setBaseUri(postUrl);
    return parse_search(doc2, 0);
}

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

@Override
public SearchRequestResult searchGetPage(int page) throws IOException, OpacErrorException, JSONException {
    /*/*from  w w w  . j ava2s.  co  m*/
    When there are many pages of results, there will only be links to the next 4 and
    previous 4 pages, so we will click links until it gets to the correct page.
     */

    if (searchResultDoc == null)
        throw new NotReachableException();

    Document doc = searchResultDoc;

    Elements pageLinks = doc.select("span[id$=DataPager1]").first().select("a[id*=LinkButtonPageN");
    int from = Integer.valueOf(pageLinks.first().text());
    int to = Integer.valueOf(pageLinks.last().text());
    Element linkToClick;
    boolean willBeCorrectPage;

    if (page < from) {
        linkToClick = pageLinks.first();
        willBeCorrectPage = false;
    } else if (page > to) {
        linkToClick = pageLinks.last();
        willBeCorrectPage = false;
    } else {
        linkToClick = pageLinks.get(page - from);
        willBeCorrectPage = true;
    }

    Pattern pattern = Pattern.compile("javascript:__doPostBack\\('([^,]*)','([^\\)]*)'\\)");
    Matcher matcher = pattern.matcher(linkToClick.attr("href"));
    if (!matcher.find())
        throw new OpacErrorException(StringProvider.INTERNAL_ERROR);

    FormElement form = (FormElement) doc.select("form").first();
    HttpEntity data = formData(form, null).addTextBody("__EVENTTARGET", matcher.group(1))
            .addTextBody("__EVENTARGUMENT", matcher.group(2)).build();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    data.writeTo(stream);

    String postUrl = form.attr("abs:action");

    String html = httpPost(postUrl, data, "UTF-8");
    if (willBeCorrectPage) {
        // We clicked on the correct link
        Document doc2 = Jsoup.parse(html);
        doc2.setBaseUri(postUrl);
        return parse_search(doc2, page);
    } else {
        // There was no correct link, so try to find one again
        searchResultDoc = Jsoup.parse(html);
        searchResultDoc.setBaseUri(postUrl);
        return searchGetPage(page);
    }
}