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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations