Search in sources :

Example 1 with JsonKeyIterator

use of de.geeksfactory.opacclient.utils.JsonKeyIterator in project opacclient by opacapp.

the class MeaningDetectorImpl method loadFile.

private void loadFile(InputStream is) {
    try {
        String jsonStr = readFile(is);
        JSONObject json = new JSONObject(jsonStr);
        // Detect layout of the JSON entries. Can be "field name":
        // "meaning" or "meaning": [ "field name", "field name", ... ]
        Iterator<String> iter = new JsonKeyIterator(json);
        if (!iter.hasNext()) {
            // No entries
            return;
        }
        String firstKey = iter.next();
        Object firstValue = json.get(firstKey);
        boolean arrayLayout = firstValue instanceof JSONArray;
        if (arrayLayout) {
            for (int i = 0; i < ((JSONArray) firstValue).length(); i++) {
                meanings.put(((JSONArray) firstValue).getString(i), firstKey);
            }
            while (iter.hasNext()) {
                String key = iter.next();
                JSONArray val = json.getJSONArray(key);
                for (int i = 0; i < val.length(); i++) {
                    meanings.put(val.getString(i), key);
                }
            }
        } else {
            meanings.put(firstKey, (String) firstValue);
            while (iter.hasNext()) {
                String key = iter.next();
                String val = json.getString(key);
                meanings.put(key, val);
            }
        }
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonKeyIterator(de.geeksfactory.opacclient.utils.JsonKeyIterator) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 2 with JsonKeyIterator

use of de.geeksfactory.opacclient.utils.JsonKeyIterator in project opacclient by opacapp.

the class Main method saveMeaning.

private static void saveMeaning(String name, Meaning meaning) throws IOException, JSONException {
    String filename = MEANINGS_DIR + "general.json";
    JSONObject json = new JSONObject(readFile(filename));
    // Detect layout of the JSON entries. Can be "field name":
    // "meaning" or "meaning": [ "field name", "field name", ... ]
    Iterator<String> iter = new JsonKeyIterator(json);
    if (!iter.hasNext()) {
        // No entries
        return;
    }
    String firstKey = iter.next();
    Object firstValue = json.get(firstKey);
    boolean arrayLayout = firstValue instanceof JSONArray;
    if (arrayLayout) {
        json.getJSONArray(meaning.toString()).put(name);
    } else {
        json.put(name, meaning.toString());
    }
    writeFile(filename, json.toString(4));
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonKeyIterator(de.geeksfactory.opacclient.utils.JsonKeyIterator) JSONObject(org.json.JSONObject)

Example 3 with JsonKeyIterator

use of de.geeksfactory.opacclient.utils.JsonKeyIterator in project opacclient by opacapp.

the class JSONFilesTest method testMeanings.

@Test
public void testMeanings() throws JSONException {
    for (JSONObject json : meaningsData) {
        // Detect layout of the JSON entries. Can be "field name":
        // "meaning" or "meaning": [ "field name", "field name", ... ]
        Iterator<String> iter = new JsonKeyIterator(json);
        if (!iter.hasNext()) {
            // No entries
            return;
        }
        String firstKey = iter.next();
        Object firstValue = json.get(firstKey);
        boolean arrayLayout = firstValue instanceof JSONArray;
        if (arrayLayout) {
            assertThat(firstKey, isIn(keys_meaning));
            while (iter.hasNext()) {
                String key = iter.next();
                assertThat(key, isIn(keys_meaning));
            }
        } else {
            assertThat((String) firstValue, isIn(keys_meaning));
            while (iter.hasNext()) {
                String key = iter.next();
                String val = json.getString(key);
                assertThat(val, isIn(keys_meaning));
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JsonKeyIterator(de.geeksfactory.opacclient.utils.JsonKeyIterator) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 4 with JsonKeyIterator

use of de.geeksfactory.opacclient.utils.JsonKeyIterator in project opacclient by opacapp.

the class Bibliotheca method search.

@Override
public SearchRequestResult search(List<SearchQuery> queries) throws IOException, JSONException, OpacErrorException {
    if (!initialised) {
        start();
    }
    FormBody.Builder formData = new FormBody.Builder(Charset.forName(getDefaultEncoding()));
    boolean stichtitSet = false;
    int ifeldCount = 0;
    String order = "asc";
    for (SearchQuery query : queries) {
        if (query.getValue().equals("")) {
            continue;
        }
        String key = query.getKey();
        if (key.contains("$")) {
            key = key.substring(0, key.indexOf("$"));
        }
        if (key.contains("ifeld")) {
            ifeldCount++;
            if (ifeldCount > 1) {
                throw new OpacErrorException(stringProvider.getString(StringProvider.COMBINATION_NOT_SUPPORTED));
            }
        }
        if (key.equals("orderselect") && query.getValue().contains(":")) {
            formData.add("orderselect", query.getValue().split(":")[0]);
            order = query.getValue().split(":")[1];
        } else {
            formData.add(key, query.getValue());
        }
        if (query.getSearchField().getData() != null) {
            JSONObject data = query.getSearchField().getData();
            if (data.has("additional_params")) {
                JSONObject params = data.getJSONObject("additional_params");
                Iterator<String> keys = new JsonKeyIterator(params);
                while (keys.hasNext()) {
                    String additionalKey = keys.next();
                    if (additionalKey.equals("stichtit")) {
                        if (stichtitSet) {
                            throw new OpacErrorException(stringProvider.getString(StringProvider.COMBINATION_NOT_SUPPORTED));
                        }
                        stichtitSet = true;
                    }
                    formData.add(additionalKey, params.getString(additionalKey));
                }
            }
        }
    }
    if (!stichtitSet) {
        formData.add("stichtit", "stich");
    }
    formData.add("suche_starten.x", "1");
    formData.add("suche_starten.y", "1");
    if (data.has("db")) {
        try {
            formData.add("dbase", data.getString("db"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    String html = httpPost(opac_url + "/index.asp", formData.build(), getDefaultEncoding());
    if (html.contains("<a href=\"index.asp?order=" + order + "\">")) {
        html = httpGet(opac_url + "/index.asp?order=" + order, getDefaultEncoding());
    }
    return parseSearch(html, 1, data);
}
Also used : SearchQuery(de.geeksfactory.opacclient.searchfields.SearchQuery) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) JsonKeyIterator(de.geeksfactory.opacclient.utils.JsonKeyIterator) JSONException(org.json.JSONException)

Aggregations

JsonKeyIterator (de.geeksfactory.opacclient.utils.JsonKeyIterator)4 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)2 SearchQuery (de.geeksfactory.opacclient.searchfields.SearchQuery)1 IOException (java.io.IOException)1 FormBody (okhttp3.FormBody)1 Test (org.junit.Test)1