Search in sources :

Example 6 with JSONElement

use of com.eden.common.json.JSONElement in project Orchid by JavaEden.

the class OrchidResources method loadLocalFile.

public JSONObject loadLocalFile(String url) {
    try {
        File file = new File(url);
        String s = IOUtils.toString(new FileInputStream(file));
        JSONElement el = context.getTheme().parse("json", s);
        if (OrchidUtils.elementIsObject(el)) {
            return (JSONObject) el.getElement();
        }
    } catch (FileNotFoundException e) {
    // ignore files not being found
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JSONElement(com.eden.common.json.JSONElement) JSONObject(org.json.JSONObject) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with JSONElement

use of com.eden.common.json.JSONElement in project Orchid by JavaEden.

the class CSVParser method parse.

@Override
public JSONElement parse(String extension, String input) {
    List<String[]> allRows;
    if (extension.equalsIgnoreCase("csv")) {
        CsvParserSettings settings = new CsvParserSettings();
        settings.getFormat().setLineSeparator("\n");
        CsvParser parser = new CsvParser(settings);
        allRows = parser.parseAll(org.apache.commons.io.IOUtils.toInputStream(input));
    } else {
        TsvParserSettings settings = new TsvParserSettings();
        settings.getFormat().setLineSeparator("\n");
        TsvParser parser = new TsvParser(settings);
        allRows = parser.parseAll(org.apache.commons.io.IOUtils.toInputStream(input));
    }
    JSONArray array = new JSONArray();
    String[] cols = allRows.get(0);
    for (int i = 1; i < allRows.size(); i++) {
        JSONObject object = new JSONObject();
        for (int j = 0; j < cols.length; j++) {
            object.put(cols[j], allRows.get(i)[j]);
        }
        array.put(object);
    }
    JSONObject object = new JSONObject();
    object.put("list", array);
    return new JSONElement(object);
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) JSONObject(org.json.JSONObject) JSONElement(com.eden.common.json.JSONElement) JSONArray(org.json.JSONArray) CsvParser(com.univocity.parsers.csv.CsvParser) TsvParserSettings(com.univocity.parsers.tsv.TsvParserSettings) TsvParser(com.univocity.parsers.tsv.TsvParser)

Example 8 with JSONElement

use of com.eden.common.json.JSONElement in project Orchid by JavaEden.

the class TOMLParser method parse.

@Override
public JSONElement parse(String extension, String input) {
    Toml toml = new Toml().read(input);
    JSONObject object = new JSONObject(toml.toMap());
    return new JSONElement(object);
}
Also used : JSONObject(org.json.JSONObject) JSONElement(com.eden.common.json.JSONElement) Toml(com.moandjiezana.toml.Toml)

Example 9 with JSONElement

use of com.eden.common.json.JSONElement in project Orchid by JavaEden.

the class SetupEnvironment method parseDataFiles.

private void parseDataFiles(JSONObject options) {
    String resPath = "" + options.optString("resourcesDir");
    JSONObject dataOptions = new JSONObject();
    File dataDir = new File(resPath + "/data");
    if (dataDir.exists() && dataDir.isDirectory()) {
        new ArrayList<File>(FileUtils.listFiles(dataDir, dataExtensions, false)).stream().forEach(file -> {
            JSONElement parsedFile = parseFile(file);
            if (parsedFile != null) {
                dataOptions.put(FilenameUtils.removeExtension(file.getName()), parsedFile.getElement());
            }
        });
    }
    options.put("data", dataOptions);
}
Also used : JSONObject(org.json.JSONObject) JSONElement(com.eden.common.json.JSONElement) ArrayList(java.util.ArrayList) File(java.io.File)

Example 10 with JSONElement

use of com.eden.common.json.JSONElement in project Orchid by JavaEden.

the class OrchidUtils method queryIndex.

public static JSONArray queryIndex(OrchidContext context, String indexName) {
    JSONArray array = new JSONArray();
    JSONElement internalEl = context.query("index." + indexName);
    if (OrchidUtils.elementIsObject(internalEl)) {
        List items = OrchidUtils.walkObject((JSONObject) internalEl.getElement(), "url");
        for (Object item : items) {
            JSONObject object = null;
            if (item instanceof Map) {
                object = new JSONObject((Map) item);
            } else if (item instanceof JSONObject) {
                object = (JSONObject) item;
            }
            if (object != null) {
                array.put(object);
            }
        }
    }
    JSONElement externalEl = context.query("options.externalIndex.keyedIndex." + indexName);
    if (OrchidUtils.elementIsArray(externalEl)) {
        JSONArray externalIndexArray = (JSONArray) externalEl.getElement();
        for (int i = 0; i < externalIndexArray.length(); i++) {
            JSONObject object = null;
            if (externalIndexArray.get(i) instanceof Map) {
                object = new JSONObject((Map) externalIndexArray.get(i));
            } else if (externalIndexArray.get(i) instanceof JSONObject) {
                object = (JSONObject) externalIndexArray.get(i);
            }
            if (object != null) {
                array.put(object);
            }
        }
    }
    return array;
}
Also used : JSONElement(com.eden.common.json.JSONElement) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.JSONObject) Map(java.util.Map)

Aggregations

JSONElement (com.eden.common.json.JSONElement)13 JSONObject (org.json.JSONObject)10 JSONArray (org.json.JSONArray)7 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 OrchidIndex (com.eden.orchid.api.indexing.OrchidIndex)2 File (java.io.File)2 List (java.util.List)2 Clog (com.caseyjbrooks.clog.Clog)1 EdenUtils (com.eden.common.util.EdenUtils)1 OrchidContext (com.eden.orchid.api.OrchidContext)1 Prioritized (com.eden.orchid.api.registration.Prioritized)1 JsonResource (com.eden.orchid.api.resources.resource.JsonResource)1 OrchidResource (com.eden.orchid.api.resources.resource.OrchidResource)1 Theme (com.eden.orchid.api.theme.Theme)1 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)1 OrchidReference (com.eden.orchid.api.theme.pages.OrchidReference)1 OrchidInternalIndex (com.eden.orchid.impl.indexing.OrchidInternalIndex)1 OrchidRootExternalIndex (com.eden.orchid.impl.indexing.OrchidRootExternalIndex)1 OrchidRootInternalIndex (com.eden.orchid.impl.indexing.OrchidRootInternalIndex)1