use of org.jsfr.json.JsonPathListener in project solr-cmd-utils by tblsoft.
the class JsonReader method read.
public void read() {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
engine = mgr.getEngineByName("JavaScript");
String internalFilename = getProperty("filename", null);
filename = IOUtils.getAbsoluteFile(getBaseDir(), internalFilename);
String internalJavaScriptFilename = getProperty("javaScriptFilename", null);
javaScriptFilename = IOUtils.getAbsoluteFile(getBaseDir(), internalJavaScriptFilename);
rootPath = getProperty("rootPath", "$");
script = FileUtils.readFileToString(new File(javaScriptFilename));
cx = Context.enter();
JsonSurfer surfer = JsonSurferGson.INSTANCE;
java.io.Reader sample = new FileReader(filename);
surfer.configBuilder().bind(rootPath, new JsonPathListener() {
public void onValue(Object value, ParsingContext context) {
Scriptable scope = cx.initStandardObjects();
List<Document> output = new ArrayList<Document>();
ScriptableObject.putProperty(scope, "documentBuilder", Context.javaToJS(new DocumentBuilder(), scope));
ScriptableObject.putProperty(scope, "output", Context.javaToJS(output, scope));
String exec = "var input = " + value.toString() + ";" + script;
cx.evaluateString(scope, exec, filename, 1, null);
for (Document out : output) {
executer.document(out);
}
}
}).buildAndSurf(sample);
sample.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations