Search in sources :

Example 6 with SearchField

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

the class Primo method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, OpacErrorException, JSONException {
    start();
    String html = httpGet(opac_url + "/action/search.do?mode=Advanced&ct=AdvancedSearch&vid=" + vid, getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();
    if (doc.select("select#exlidInput_scope_1").size() < 1) {
        throw new NotReachableException();
    }
    Elements options = doc.select("select#exlidInput_scope_1").first().select("option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.val());
        field.setHint("");
        field.setData(new JSONObject());
        field.getData().put("meaning", option.val());
        fields.add(field);
    }
    if (fields.size() == 0) {
        // Weird JavaScript, e.g. view-source:http://vopac.nlg.gr/Search/Advanced
        Pattern pattern_key = Pattern.compile("searchFields\\[\"([^\"]+)\"\\] = \"([^\"]+)\";");
        for (Element script : doc.select("script")) {
            if (!script.html().contains("searchFields"))
                continue;
            for (String line : script.html().split("\n")) {
                Matcher matcher = pattern_key.matcher(line);
                if (matcher.find()) {
                    TextSearchField field = new TextSearchField();
                    field.setDisplayName(matcher.group(2));
                    field.setId(matcher.group(1));
                    field.setHint("");
                    field.setData(new JSONObject());
                    field.getData().put("meaning", field.getId());
                    fields.add(field);
                }
            }
        }
    }
    Elements selects = doc.select("#exlidInput_mediaType_, #exlidInput_publicationDate_, " + "#exlidInput_language_, #exlidSearchIn");
    for (Element select : selects) {
        DropdownSearchField field = new DropdownSearchField();
        if (select.parent().select("label").size() > 0) {
            field.setDisplayName(select.parent().select("label").first().text());
        } else {
            continue;
        }
        field.setId("#" + select.attr("id"));
        for (Element option : select.select("option")) {
            if (option.val().equals("all_items")) {
                field.addDropdownValue(0, option.val(), option.text());
            } else {
                field.addDropdownValue(option.val(), option.text());
            }
        }
        field.setData(new JSONObject());
        field.getData().put("meaning", field.getId());
        fields.add(field);
    }
    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) 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) JSONObject(org.json.JSONObject)

Example 7 with SearchField

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

the class Bibliotheca method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();
    }
    List<SearchField> fields = new ArrayList<>();
    // Read branches and media types
    FormBody.Builder formData = new FormBody.Builder(Charset.forName(getDefaultEncoding()));
    formData.add("link_profis.x", "0");
    formData.add("link_profis.y", "1");
    String html = httpPost(opac_url + "/index.asp", formData.build(), getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    Elements fieldElems = doc.select(".suchfeldinhalt");
    for (Element fieldElem : fieldElems) {
        String name = fieldElem.select(".suchfeld_inhalt_titel label").text();
        String hint = "";
        if (fieldElem.select(".suchfeld_inhalt_input").size() > 0) {
            List<TextNode> textNodes = fieldElem.select(".suchfeld_inhalt_input").first().textNodes();
            if (textNodes.size() > 0) {
                for (TextNode node : textNodes) {
                    String text = node.getWholeText().replace("\n", "");
                    if (!text.equals("")) {
                        hint = node.getWholeText().replace("\n", "");
                        break;
                    }
                }
            }
        }
        Elements inputs = fieldElem.select(".suchfeld_inhalt_input input[type=text], " + ".suchfeld_inhalt_input select");
        if (inputs.size() == 1) {
            SearchField field = createSearchField(name, hint, inputs.get(0));
            Elements radios = fieldElem.select("input[type=radio]");
            if (field instanceof TextSearchField && radios.size() > 0) {
                TextSearchField tf = (TextSearchField) field;
                if (radios.get(0).attr("value").equals("stich")) {
                    tf.setFreeSearch(true);
                    if (fieldElem.select("label[for=stichtit_sich]").size() > 0) {
                        tf.setHint(fieldElem.select("label[for=stichtit_sich]").text().trim());
                    }
                    JSONObject addData = new JSONObject();
                    JSONObject params = new JSONObject();
                    params.put("stichtit", "stich");
                    addData.put("additional_params", params);
                    tf.setData(addData);
                }
                if (radios.size() == 2 && radios.get(1).attr("value").equals("titel")) {
                    TextSearchField tf2 = new TextSearchField();
                    tf2.setId(tf.getId());
                    if (fieldElem.select("label[for=stichtit_titel]").size() > 0) {
                        tf2.setDisplayName(fieldElem.select("label[for=stichtit_titel]").text().trim());
                    }
                    JSONObject addData = new JSONObject();
                    JSONObject params = new JSONObject();
                    params.put("stichtit", "titel");
                    addData.put("additional_params", params);
                    tf2.setData(addData);
                    fields.add(tf2);
                }
            }
            fields.add(field);
        } else if (inputs.size() == 2 && inputs.select("input[type=text]").size() == 2) {
            // Two text fields, e.g. year from/to or two keywords
            fields.add(createSearchField(name, hint, inputs.get(0)));
            TextSearchField secondField = (TextSearchField) createSearchField(name, hint, inputs.get(1));
            secondField.setHalfWidth(true);
            fields.add(secondField);
        } else if (inputs.size() == 2 && inputs.get(0).tagName().equals("select") && inputs.get(1).tagName().equals("input") && inputs.get(0).attr("name").equals("feld1")) {
            // Break it down into single text fields.
            for (Element option : inputs.get(0).select("option")) {
                TextSearchField field = new TextSearchField();
                field.setHint(hint);
                field.setDisplayName(option.text());
                field.setId(inputs.get(1).attr("name") + "$" + option.attr("value"));
                JSONObject data = new JSONObject();
                JSONObject params = new JSONObject();
                params.put(inputs.get(0).attr("name"), option.attr("value"));
                data.put("additional_params", params);
                field.setData(data);
                fields.add(field);
            }
        }
    }
    if (fields.size() > 0) {
        DropdownSearchField orderField = new DropdownSearchField("orderselect", stringProvider.getString(StringProvider.ORDER), false, null);
        orderField.addDropdownValue("1", stringProvider.getString(StringProvider.ORDER_DEFAULT));
        orderField.addDropdownValue("2:desc", stringProvider.getString(StringProvider.ORDER_YEAR_DESC));
        orderField.addDropdownValue("2:asc", stringProvider.getString(StringProvider.ORDER_YEAR_ASC));
        orderField.addDropdownValue("3:desc", stringProvider.getString(StringProvider.ORDER_CATEGORY_DESC));
        orderField.addDropdownValue("3:asc", stringProvider.getString(StringProvider.ORDER_CATEGORY_ASC));
        orderField.setMeaning(Meaning.ORDER);
        fields.add(orderField);
    }
    return fields;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) FormBody(okhttp3.FormBody) TextNode(org.jsoup.nodes.TextNode) 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)

Example 8 with SearchField

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

the class VuFind method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, OpacErrorException, JSONException {
    start();
    String html = httpGet(opac_url + "/Search/Advanced?mylang = " + languageCode, getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();
    Elements options = doc.select("select#search_type0_0 option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.val());
        field.setHint("");
        field.setData(new JSONObject());
        field.getData().put("meaning", option.val());
        fields.add(field);
    }
    if (fields.size() == 0) {
        // Weird JavaScript, e.g. view-source:http://vopac.nlg.gr/Search/Advanced
        Pattern pattern_key = Pattern.compile("searchFields\\[\"([^\"]+)\"\\] = \"([^\"]+)\";");
        for (Element script : doc.select("script")) {
            if (!script.html().contains("searchFields"))
                continue;
            for (String line : script.html().split("\n")) {
                Matcher matcher = pattern_key.matcher(line);
                if (matcher.find()) {
                    TextSearchField field = new TextSearchField();
                    field.setDisplayName(matcher.group(2));
                    field.setId(matcher.group(1));
                    field.setHint("");
                    field.setData(new JSONObject());
                    field.getData().put("meaning", field.getId());
                    fields.add(field);
                }
            }
        }
    }
    Elements selects = doc.select("select");
    for (Element select : selects) {
        if (!select.attr("name").equals("filter[]"))
            continue;
        DropdownSearchField field = new DropdownSearchField();
        if (select.parent().select("label").size() > 0) {
            field.setDisplayName(select.parent().select("label").first().text());
        } else if (select.parent().parent().select("label").size() == 1) {
            field.setDisplayName(select.parent().parent().select("label").first().text());
        }
        field.setId(select.attr("name") + select.attr("id"));
        String meaning = select.attr("id");
        field.addDropdownValue("", "");
        for (Element option : select.select("option")) {
            if (option.val().contains(":")) {
                meaning = option.val().split(":")[0];
            }
            field.addDropdownValue(option.val(), option.text());
        }
        field.setData(new JSONObject());
        field.getData().put("meaning", meaning);
        fields.add(field);
    }
    return fields;
}
Also used : Pattern(java.util.regex.Pattern) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) 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)

Example 9 with SearchField

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

the class Pica method parseSearchFields.

@Override
public List<SearchField> parseSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();
    }
    String html = httpGet(opac_url + "/LNG=" + getLang() + "/DB=" + db + "/ADVANCED_SEARCHFILTER", getDefaultEncoding());
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();
    Elements options = doc.select("select[name=IKT0] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.attr("value"));
        field.setHint("");
        field.setData(new JSONObject("{\"ADI\": false}"));
        Pattern pattern = Pattern.compile("(?: --- )?(\\[X?[A-Za-z]{2,3}:?\\]|\\(X?[A-Za-z]{2,3}:?\\))");
        Matcher matcher = pattern.matcher(field.getDisplayName());
        if (matcher.find()) {
            field.getData().put("meaning", matcher.group(1).replace(":", "").toUpperCase());
            field.setDisplayName(matcher.replaceFirst("").trim());
        }
        fields.add(field);
    }
    Elements sort = doc.select("select[name=SRT]");
    if (sort.size() > 0) {
        DropdownSearchField field = new DropdownSearchField();
        field.setDisplayName(sort.first().parent().parent().select(".longval").first().text());
        field.setId("SRT");
        for (Element option : sort.select("option")) {
            field.addDropdownValue(option.attr("value"), option.text());
        }
        fields.add(field);
    }
    for (Element input : doc.select("input[type=text][name^=ADI]")) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(input.parent().parent().select(".longkey").text());
        field.setId(input.attr("name"));
        field.setHint(input.parent().select("span").text());
        field.setData(new JSONObject("{\"ADI\": true}"));
        fields.add(field);
    }
    for (Element dropdown : doc.select("select[name^=ADI]")) {
        DropdownSearchField field = new DropdownSearchField();
        field.setDisplayName(dropdown.parent().parent().select(".longkey").text());
        field.setId(dropdown.attr("name"));
        for (Element option : dropdown.select("option")) {
            field.addDropdownValue(option.attr("value"), option.text());
        }
        fields.add(field);
    }
    Elements fuzzy = doc.select("input[name=FUZZY]");
    if (fuzzy.size() > 0) {
        CheckboxSearchField field = new CheckboxSearchField();
        field.setDisplayName(fuzzy.first().parent().parent().select(".longkey").first().text());
        field.setId("FUZZY");
        fields.add(field);
    }
    Elements mediatypes = doc.select("input[name=ADI_MAT]");
    if (mediatypes.size() > 0) {
        DropdownSearchField field = new DropdownSearchField();
        field.setDisplayName("Materialart");
        field.setId("ADI_MAT");
        field.addDropdownValue("", "Alle");
        for (Element mt : mediatypes) {
            field.addDropdownValue(mt.attr("value"), mt.parent().nextElementSibling().text().replace("\u00a0", ""));
        }
        fields.add(field);
    }
    return fields;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) 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) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField) BarcodeSearchField(de.geeksfactory.opacclient.searchfields.BarcodeSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) SearchField(de.geeksfactory.opacclient.searchfields.SearchField) JSONObject(org.json.JSONObject) CheckboxSearchField(de.geeksfactory.opacclient.searchfields.CheckboxSearchField)

Example 10 with SearchField

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

the class TouchPoint method parseSearchFields.

public List<SearchField> parseSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();
    }
    String html = httpGet(opac_url + "/search.do?methodToCall=switchSearchPage&SearchType=2", ENCODING);
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();
    Elements options = doc.select("select[name=searchCategories[0]] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.attr("value"));
        field.setHint("");
        fields.add(field);
    }
    for (Element dropdown : doc.select(".accordion-body select")) {
        parseDropdown(dropdown, fields);
    }
    if (doc.select(".selectDatabase").size() > 0) {
        DropdownSearchField dropdown = new DropdownSearchField();
        dropdown.setId("_database");
        for (Element option : doc.select(".selectDatabase")) {
            String label = option.parent().ownText().trim();
            if (label.equals("")) {
                for (Element a : option.siblingElements()) {
                    label += a.ownText().trim();
                }
            }
            dropdown.addDropdownValue(option.attr("name") + "=" + option.attr("value"), label.trim());
        }
        dropdown.setDisplayName(doc.select(".dbselection h3").first().text().trim());
        fields.add(dropdown);
    }
    return fields;
}
Also used : SearchField(de.geeksfactory.opacclient.searchfields.SearchField) TextSearchField(de.geeksfactory.opacclient.searchfields.TextSearchField) DropdownSearchField(de.geeksfactory.opacclient.searchfields.DropdownSearchField) 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)

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