Example usage for org.jsoup.nodes Element val

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

Introduction

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

Prototype

public Element val(String value) 

Source Link

Document

Set the value of a form element (input, textarea, etc).

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  a v a  2  s . com*/

    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);
}