Search in sources :

Example 1 with DocumentBuilder

use of de.tblsoft.solr.pipeline.bean.DocumentBuilder in project solr-cmd-utils by tblsoft.

the class JavaScriptFilter method document.

@Override
public void document(Document document) {
    Context cx = Context.enter();
    Scriptable scope = cx.initStandardObjects();
    List<Document> output = new ArrayList<Document>();
    ScriptableObject.putProperty(scope, "documentBuilder", Context.javaToJS(new DocumentBuilder(), scope));
    ScriptableObject.putProperty(scope, "input", Context.javaToJS(document, scope));
    ScriptableObject.putProperty(scope, "output", Context.javaToJS(output, scope));
    cx.evaluateString(scope, script, filename, 1, null);
    for (Document out : output) {
        super.document(out);
    }
}
Also used : Context(org.mozilla.javascript.Context) DocumentBuilder(de.tblsoft.solr.pipeline.bean.DocumentBuilder) ArrayList(java.util.ArrayList) Scriptable(org.mozilla.javascript.Scriptable) Document(de.tblsoft.solr.pipeline.bean.Document)

Example 2 with DocumentBuilder

use of de.tblsoft.solr.pipeline.bean.DocumentBuilder 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)2 DocumentBuilder (de.tblsoft.solr.pipeline.bean.DocumentBuilder)2 ArrayList (java.util.ArrayList)2 Scriptable (org.mozilla.javascript.Scriptable)2 File (java.io.File)1 FileReader (java.io.FileReader)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 JsonPathListener (org.jsfr.json.JsonPathListener)1 JsonSurfer (org.jsfr.json.JsonSurfer)1 ParsingContext (org.jsfr.json.ParsingContext)1 Context (org.mozilla.javascript.Context)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1