Search in sources :

Example 31 with SearchField

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

the class Adis method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, JSONException {
    start();
    Document doc = htmlGet(opac_url + ";jsessionid=" + s_sid + "?service=" + s_service + getSpParams());
    List<SearchField> fields = new ArrayList<>();
    // dropdown to select which field you want to search in
    Elements searchoptions = doc.select("#SUCH01_1 option");
    if (searchoptions.size() == 0 && doc.select("input[fld=FELD01_1]").size() > 0) {
        // Hack is needed in Nuernberg
        searchoptions = doc.select("input[fld=FELD01_1]").first().previousElementSibling().select("option");
    }
    Set<String> fieldIds = new HashSet<>();
    for (Element opt : searchoptions) {
        // (bei Stadtbücherei Stuttgart der Fall)
        if (fieldIds.contains(opt.attr("value")))
            continue;
        TextSearchField field = new TextSearchField();
        field.setId(opt.attr("value"));
        field.setDisplayName(opt.text());
        field.setHint("");
        fields.add(field);
        fieldIds.add(field.getId());
    }
    // Save data so that the search() function knows that this
    // is not a selectable search field
    JSONObject selectableData = new JSONObject();
    selectableData.put("selectable", false);
    for (Element row : doc.select("div[id~=F\\d+], .search-adv-source")) {
        if (row.select("input[type=text]").size() == 1 && row.select("input, select").first().tagName().equals("input")) {
            // A single text search field
            Element input = row.select("input[type=text]").first();
            TextSearchField field = new TextSearchField();
            field.setId(input.attr("id"));
            field.setDisplayName(row.select("label").first().text());
            field.setHint("");
            field.setData(selectableData);
            fields.add(field);
        } else if (row.select("select").size() == 1 && row.select("input[type=text]").size() == 0) {
            // Things like language, media type, etc.
            Element select = row.select("select").first();
            DropdownSearchField field = new DropdownSearchField();
            field.setId(select.id());
            field.setDisplayName(row.select("label").first().text());
            for (Element opt : select.select("option")) {
                field.addDropdownValue(opt.attr("value"), opt.text());
            }
            fields.add(field);
        } else if (row.select("select").size() == 0 && row.select("input[type=text]").size() == 3 && row.select("label").size() == 3) {
            // Three text inputs.
            // Year single/from/to or things like Band-/Heft-/Satznummer
            String name1 = row.select("label").get(0).text();
            String name2 = row.select("label").get(1).text();
            String name3 = row.select("label").get(2).text();
            Element input1 = row.select("input[type=text]").get(0);
            Element input2 = row.select("input[type=text]").get(1);
            Element input3 = row.select("input[type=text]").get(2);
            if (name2.contains("von") && name3.contains("bis")) {
                TextSearchField field1 = new TextSearchField();
                field1.setId(input1.id());
                field1.setDisplayName(name1);
                field1.setHint("");
                field1.setData(selectableData);
                fields.add(field1);
                TextSearchField field2 = new TextSearchField();
                field2.setId(input2.id());
                field2.setDisplayName(name2.replace("von", "").trim());
                field2.setHint("von");
                field2.setData(selectableData);
                fields.add(field2);
                TextSearchField field3 = new TextSearchField();
                field3.setId(input3.id());
                field3.setDisplayName(name3.replace("bis", "").trim());
                field3.setHint("bis");
                field3.setHalfWidth(true);
                field3.setData(selectableData);
                fields.add(field3);
            } else {
                TextSearchField field1 = new TextSearchField();
                field1.setId(input1.id());
                field1.setDisplayName(name1);
                field1.setHint("");
                field1.setData(selectableData);
                fields.add(field1);
                TextSearchField field2 = new TextSearchField();
                field2.setId(input2.id());
                field2.setDisplayName(name2);
                field2.setHint("");
                field2.setData(selectableData);
                fields.add(field2);
                TextSearchField field3 = new TextSearchField();
                field3.setId(input3.id());
                field3.setDisplayName(name3);
                field3.setHint("");
                field3.setData(selectableData);
                fields.add(field3);
            }
        }
    }
    for (Iterator<SearchField> iterator = fields.iterator(); iterator.hasNext(); ) {
        SearchField field = iterator.next();
        if (ignoredFieldNames.contains(field.getDisplayName())) {
            iterator.remove();
        }
    }
    return fields;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) JSONObject(org.json.JSONObject) HashSet(java.util.HashSet)

Example 32 with SearchField

use of de.geeksfactory.opacclient.searchfields.SearchField 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

SearchField (de.geeksfactory.opacclient.searchfields.SearchField)32 TextSearchField (de.geeksfactory.opacclient.searchfields.TextSearchField)26 ArrayList (java.util.ArrayList)23 DropdownSearchField (de.geeksfactory.opacclient.searchfields.DropdownSearchField)22 Document (org.jsoup.nodes.Document)16 Element (org.jsoup.nodes.Element)16 Elements (org.jsoup.select.Elements)15 BarcodeSearchField (de.geeksfactory.opacclient.searchfields.BarcodeSearchField)12 JSONObject (org.json.JSONObject)11 CheckboxSearchField (de.geeksfactory.opacclient.searchfields.CheckboxSearchField)10 ViewGroup (android.view.ViewGroup)6 Spinner (android.widget.Spinner)6 SearchQuery (de.geeksfactory.opacclient.searchfields.SearchQuery)6 CheckBox (android.widget.CheckBox)5 EditText (android.widget.EditText)5 IOException (java.io.IOException)4 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)3 JsonSearchFieldDataSource (de.geeksfactory.opacclient.storage.JsonSearchFieldDataSource)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3