Search in sources :

Example 1 with JsonPathListener

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);
    }
}
Also used : ParsingContext(org.jsfr.json.ParsingContext) ScriptEngineManager(javax.script.ScriptEngineManager) JsonPathListener(org.jsfr.json.JsonPathListener) ArrayList(java.util.ArrayList) Scriptable(org.mozilla.javascript.Scriptable) Document(de.tblsoft.solr.pipeline.bean.Document) DocumentBuilder(de.tblsoft.solr.pipeline.bean.DocumentBuilder) FileReader(java.io.FileReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) JsonSurfer(org.jsfr.json.JsonSurfer) File(java.io.File)

Aggregations

Document (de.tblsoft.solr.pipeline.bean.Document)1 DocumentBuilder (de.tblsoft.solr.pipeline.bean.DocumentBuilder)1 File (java.io.File)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 JsonPathListener (org.jsfr.json.JsonPathListener)1 JsonSurfer (org.jsfr.json.JsonSurfer)1 ParsingContext (org.jsfr.json.ParsingContext)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1