use of de.geeksfactory.opacclient.searchfields.DropdownSearchField 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;
}
}
use of de.geeksfactory.opacclient.searchfields.DropdownSearchField 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;
}
Aggregations