Search in sources :

Example 31 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class IOpac method createSearchField.

private SearchField createSearchField(Element descTd, Element inputTd) {
    String name = descTd.select("span, blockquote").text().replace(":", "").trim().replace("\u00a0", "");
    if (inputTd.select("select").size() > 0 && !name.equals("Treffer/Seite") && !name.equals("Medientypen") && !name.equals("Medientyp") && !name.equals("Treffer pro Seite")) {
        Element select = inputTd.select("select").first();
        DropdownSearchField field = new DropdownSearchField();
        field.setDisplayName(name);
        field.setId(select.attr("name"));
        for (Element option : select.select("option")) {
            field.addDropdownValue(option.attr("value"), option.text());
        }
        return field;
    } else if (inputTd.select("input").size() > 0) {
        TextSearchField field = new TextSearchField();
        Element input = inputTd.select("input").first();
        field.setDisplayName(name);
        field.setId(input.attr("name"));
        field.setHint("");
        return field;
    } else {
        return null;
    }
}
Also used : Element(org.jsoup.nodes.Element) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField)

Example 32 with TextSearchField

use of de.geeksfactory.opacclient.searchfields.TextSearchField in project opacclient by opacapp.

the class IOpac method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException {
    List<SearchField> fields = new ArrayList<>();
    // Extract all search fields, except media types
    String html;
    try {
        html = httpGet(opac_url + dir + "/search_expert.htm", getDefaultEncoding());
    } catch (NotReachableException e) {
        html = httpGet(opac_url + dir + "/iopacie.htm", getDefaultEncoding());
    }
    Document doc = Jsoup.parse(html);
    Elements trs = doc.select("form tr:has(input:not([type=submit], [type=reset])), form tr:has(select)");
    for (Element tr : trs) {
        Elements tds = tr.children();
        if (tds.size() == 4) {
            // Two search fields next to each other in one row
            SearchField field1 = createSearchField(tds.get(0), tds.get(1));
            SearchField field2 = createSearchField(tds.get(2), tds.get(3));
            if (field1 != null) {
                fields.add(field1);
            }
            if (field2 != null) {
                fields.add(field2);
            }
        } else if (tds.size() == 2 || (tds.size() == 3 && tds.get(2).children().size() == 0)) {
            SearchField field = createSearchField(tds.get(0), tds.get(1));
            if (field != null) {
                fields.add(field);
            }
        }
    }
    if (fields.size() == 0 && doc.select("[name=sleStichwort]").size() > 0) {
        TextSearchField field = new TextSearchField();
        Element input = doc.select("input[name=sleStichwort]").first();
        field.setDisplayName(stringProvider.getString(StringProvider.FREE_SEARCH));
        field.setId(input.attr("name"));
        field.setHint("");
        fields.add(field);
    }
    // Extract available media types.
    // We have to parse JavaScript. Doing this with RegEx is evil.
    // But not as evil as including a JavaScript VM into the app.
    // And I honestly do not see another way.
    Pattern pattern_key = Pattern.compile("mtyp\\[[0-9]+\\]\\[\"typ\"\\] = \"([^\"]+)\";");
    Pattern pattern_value = Pattern.compile("mtyp\\[[0-9]+\\]\\[\"bez\"\\] = \"([^\"]+)\";");
    DropdownSearchField mtyp = new DropdownSearchField();
    try {
        try {
            html = httpGet(opac_url + dir + "/mtyp.js", getDefaultEncoding());
        } catch (NotReachableException e) {
            html = httpGet(opac_url + "/mtyp.js", getDefaultEncoding());
        }
        String[] parts = html.split("new Array\\(\\);");
        for (String part : parts) {
            Matcher matcher1 = pattern_key.matcher(part);
            String key = "";
            String value = "";
            if (matcher1.find()) {
                key = matcher1.group(1);
            }
            Matcher matcher2 = pattern_value.matcher(part);
            if (matcher2.find()) {
                value = matcher2.group(1);
            }
            if (!value.equals("")) {
                mtyp.addDropdownValue(key, value);
            }
        }
    } catch (IOException e) {
        try {
            html = httpGet(opac_url + dir + "/frames/search_form.php?bReset=1?bReset=1", getDefaultEncoding());
            doc = Jsoup.parse(html);
            for (Element opt : doc.select("#imtyp option")) {
                mtyp.addDropdownValue(opt.attr("value"), opt.text());
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    if (mtyp.getDropdownValues() != null && !mtyp.getDropdownValues().isEmpty()) {
        mtyp.setDisplayName("Medientypen");
        mtyp.setId("Medientyp");
        fields.add(mtyp);
    }
    return fields;
}
Also used : Pattern(java.util.regex.Pattern) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField)

Aggregations

TextSearchField (de.geeksfactory.opacclient.searchfields.TextSearchField)32 DropdownSearchField (de.geeksfactory.opacclient.searchfields.DropdownSearchField)27 SearchField (de.geeksfactory.opacclient.searchfields.SearchField)23 Element (org.jsoup.nodes.Element)21 ArrayList (java.util.ArrayList)19 Document (org.jsoup.nodes.Document)18 Elements (org.jsoup.select.Elements)15 BarcodeSearchField (de.geeksfactory.opacclient.searchfields.BarcodeSearchField)13 CheckboxSearchField (de.geeksfactory.opacclient.searchfields.CheckboxSearchField)10 JSONObject (org.json.JSONObject)9 SearchQuery (de.geeksfactory.opacclient.searchfields.SearchQuery)6 ViewGroup (android.view.ViewGroup)5 CheckBox (android.widget.CheckBox)5 EditText (android.widget.EditText)5 Spinner (android.widget.Spinner)5 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)3 HashMap (java.util.HashMap)3