Search in sources :

Example 26 with Scriptable

use of org.mozilla.javascript.Scriptable in project pentaho-kettle by pentaho.

the class JavaScriptUtilsTest method jsToNumber_NativeJavaObject_Int.

@Test
public void jsToNumber_NativeJavaObject_Int() throws Exception {
    Scriptable value = getIntValue();
    Number number = JavaScriptUtils.jsToNumber(value, JAVA_OBJECT);
    assertEquals(1.0, number.doubleValue(), 1e-6);
}
Also used : Scriptable(org.mozilla.javascript.Scriptable) Test(org.junit.Test)

Example 27 with Scriptable

use of org.mozilla.javascript.Scriptable in project pentaho-kettle by pentaho.

the class JavaScriptUtilsTest method jsToBigNumber_NativeNumber.

@Test
public void jsToBigNumber_NativeNumber() throws Exception {
    Scriptable value = Context.toObject(1.0, scope);
    BigDecimal number = JavaScriptUtils.jsToBigNumber(value, NATIVE_NUMBER);
    assertEquals(1.0, number.doubleValue(), 1e-6);
}
Also used : Scriptable(org.mozilla.javascript.Scriptable) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 28 with Scriptable

use of org.mozilla.javascript.Scriptable in project pentaho-kettle by pentaho.

the class JavaScriptUtilsTest method jsToInteger_NativeJavaObject_Int.

@Test
public void jsToInteger_NativeJavaObject_Int() throws Exception {
    Scriptable value = getIntValue();
    assertEquals(Long.valueOf(1), JavaScriptUtils.jsToInteger(value, NativeJavaObject.class));
}
Also used : Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Test(org.junit.Test)

Example 29 with Scriptable

use of org.mozilla.javascript.Scriptable in project pentaho-kettle by pentaho.

the class JavaScriptUtilsTest method jsToBigNumber_NativeJavaObject_BigDecimal.

@Test
public void jsToBigNumber_NativeJavaObject_BigDecimal() throws Exception {
    Value value = new Value();
    value.setValue(BigDecimal.ONE);
    Scriptable object = Context.toObject(value, scope);
    assertEquals(1.0, JavaScriptUtils.jsToBigNumber(object, JAVA_OBJECT).doubleValue(), 1e-6);
}
Also used : Value(org.pentaho.di.compatibility.Value) Scriptable(org.mozilla.javascript.Scriptable) Test(org.junit.Test)

Example 30 with Scriptable

use of org.mozilla.javascript.Scriptable in project pentaho-kettle by pentaho.

the class ScriptValuesMetaMod method check.

public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
    boolean error_found = false;
    String error_message = "";
    CheckResult cr;
    Context jscx;
    Scriptable jsscope;
    Script jsscript;
    jscx = ContextFactory.getGlobal().enterContext();
    jsscope = jscx.initStandardObjects(null, false);
    try {
        jscx.setOptimizationLevel(Integer.valueOf(transMeta.environmentSubstitute(optimizationLevel)));
    } catch (NumberFormatException nfe) {
        error_message = "Error with optimization level.  Could not convert the value of " + transMeta.environmentSubstitute(optimizationLevel) + " to an integer.";
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    } catch (IllegalArgumentException iae) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, iae.getMessage(), stepMeta);
        remarks.add(cr);
    }
    String strActiveStartScriptName = "";
    String strActiveEndScriptName = "";
    String strActiveScript = "";
    String strActiveStartScript = "";
    String strActiveEndScript = "";
    // Building the Scripts
    if (jsScripts.length > 0) {
        for (int i = 0; i < jsScripts.length; i++) {
            if (jsScripts[i].isTransformScript()) {
                // strActiveScriptName =jsScripts[i].getScriptName();
                strActiveScript = jsScripts[i].getScript();
            } else if (jsScripts[i].isStartScript()) {
                strActiveStartScriptName = jsScripts[i].getScriptName();
                strActiveStartScript = jsScripts[i].getScript();
            } else if (jsScripts[i].isEndScript()) {
                strActiveEndScriptName = jsScripts[i].getScriptName();
                strActiveEndScript = jsScripts[i].getScript();
            }
        }
    }
    if (prev != null && strActiveScript.length() > 0) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK", String.valueOf(prev.size())), stepMeta);
        remarks.add(cr);
        // Adding the existing Scripts to the Context
        for (int i = 0; i < getNumberOfJSScripts(); i++) {
            Scriptable jsR = Context.toObject(jsScripts[i].getScript(), jsscope);
            jsscope.put(jsScripts[i].getScriptName(), jsscope, jsR);
        }
        // Modification for Additional Script parsing
        try {
            if (getAddClasses() != null) {
                for (int i = 0; i < getAddClasses().length; i++) {
                    Object jsOut = Context.javaToJS(getAddClasses()[i].getAddObject(), jsscope);
                    ScriptableObject.putProperty(jsscope, getAddClasses()[i].getJSName(), jsOut);
                }
            }
        } catch (Exception e) {
            error_message = ("Couldn't add JavaClasses to Context! Error:");
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        // Adding some default JavaScriptFunctions to the System
        try {
            Context.javaToJS(ScriptValuesAddedFunctions.class, jsscope);
            ((ScriptableObject) jsscope).defineFunctionProperties(ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM);
        } catch (Exception ex) {
            error_message = "Couldn't add Default Functions! Error:" + Const.CR + ex.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        // Adding some Constants to the JavaScript
        try {
            jsscope.put("SKIP_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.SKIP_TRANSFORMATION));
            jsscope.put("ABORT_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ABORT_TRANSFORMATION));
            jsscope.put("ERROR_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ERROR_TRANSFORMATION));
            jsscope.put("CONTINUE_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.CONTINUE_TRANSFORMATION));
        } catch (Exception ex) {
            error_message = "Couldn't add Transformation Constants! Error:" + Const.CR + ex.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            ScriptValuesModDummy dummyStep = new ScriptValuesModDummy(prev, transMeta.getStepFields(stepMeta));
            Scriptable jsvalue = Context.toObject(dummyStep, jsscope);
            jsscope.put("_step_", jsscope, jsvalue);
            Object[] row = new Object[prev.size()];
            Scriptable jsRowMeta = Context.toObject(prev, jsscope);
            jsscope.put("rowMeta", jsscope, jsRowMeta);
            for (int i = 0; i < prev.size(); i++) {
                ValueMetaInterface valueMeta = prev.getValueMeta(i);
                Object valueData = null;
                // 
                if (valueMeta.isDate()) {
                    valueData = new Date();
                }
                if (valueMeta.isString()) {
                    valueData = "test value test value test value test value test value " + "test value test value test value test value test value";
                }
                if (valueMeta.isInteger()) {
                    valueData = Long.valueOf(0L);
                }
                if (valueMeta.isNumber()) {
                    valueData = new Double(0.0);
                }
                if (valueMeta.isBigNumber()) {
                    valueData = BigDecimal.ZERO;
                }
                if (valueMeta.isBoolean()) {
                    valueData = Boolean.TRUE;
                }
                if (valueMeta.isBinary()) {
                    valueData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                }
                row[i] = valueData;
                if (isCompatible()) {
                    Value value = valueMeta.createOriginalValue(valueData);
                    Scriptable jsarg = Context.toObject(value, jsscope);
                    jsscope.put(valueMeta.getName(), jsscope, jsarg);
                } else {
                    Scriptable jsarg = Context.toObject(valueData, jsscope);
                    jsscope.put(valueMeta.getName(), jsscope, jsarg);
                }
            }
            // Add support for Value class (new Value())
            Scriptable jsval = Context.toObject(Value.class, jsscope);
            jsscope.put("Value", jsscope, jsval);
            // 
            if (isCompatible()) {
                Row v2Row = RowMeta.createOriginalRow(prev, row);
                Scriptable jsV2Row = Context.toObject(v2Row, jsscope);
                jsscope.put("row", jsscope, jsV2Row);
            } else {
                Scriptable jsRow = Context.toObject(row, jsscope);
                jsscope.put("row", jsscope, jsRow);
            }
        } catch (Exception ev) {
            error_message = "Couldn't add Input fields to Script! Error:" + Const.CR + ev.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            // Checking for StartScript
            if (strActiveStartScript != null && strActiveStartScript.length() > 0) {
                /* Object startScript = */
                jscx.evaluateString(jsscope, strActiveStartScript, "trans_Start", 1, null);
                error_message = "Found Start Script. " + strActiveStartScriptName + " Processing OK";
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
                remarks.add(cr);
            }
        } catch (Exception e) {
            error_message = "Couldn't process Start Script! Error:" + Const.CR + e.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            jsscript = jscx.compileString(strActiveScript, "script", 1, null);
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK"), stepMeta);
            remarks.add(cr);
            try {
                jsscript.exec(jscx, jsscope);
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK2"), stepMeta);
                remarks.add(cr);
                if (fieldname.length > 0) {
                    StringBuilder message = new StringBuilder(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.FailedToGetValues", String.valueOf(fieldname.length)) + Const.CR + Const.CR);
                    if (error_found) {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, message.toString(), stepMeta);
                    } else {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, message.toString(), stepMeta);
                    }
                    remarks.add(cr);
                }
            } catch (JavaScriptException jse) {
                Context.exit();
                error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript") + Const.CR + jse.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            } catch (Exception e) {
                Context.exit();
                error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript2") + Const.CR + e.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            }
            // Checking End Script
            try {
                if (strActiveEndScript != null && strActiveEndScript.length() > 0) {
                    /* Object endScript = */
                    jscx.evaluateString(jsscope, strActiveEndScript, "trans_End", 1, null);
                    error_message = "Found End Script. " + strActiveEndScriptName + " Processing OK";
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
                    remarks.add(cr);
                }
            } catch (Exception e) {
                error_message = "Couldn't process End Script! Error:" + Const.CR + e.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            }
        } catch (Exception e) {
            Context.exit();
            error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotCompileScript") + Const.CR + e.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
    } else {
        Context.exit();
        error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotGetFieldsFromPreviousStep");
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    }
    // See if we have input streams leading to this step!
    if (input.length > 0) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK2"), stepMeta);
        remarks.add(cr);
    } else {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.NoInputReceived"), stepMeta);
        remarks.add(cr);
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) JavaScriptException(org.mozilla.javascript.JavaScriptException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Date(java.util.Date) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) JavaScriptException(org.mozilla.javascript.JavaScriptException) CheckResult(org.pentaho.di.core.CheckResult) Value(org.pentaho.di.compatibility.Value) ScriptableObject(org.mozilla.javascript.ScriptableObject) Row(org.pentaho.di.compatibility.Row)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)151 Context (org.mozilla.javascript.Context)67 ScriptableObject (org.mozilla.javascript.ScriptableObject)47 Test (org.junit.Test)17 ContextAction (org.mozilla.javascript.ContextAction)12 Script (org.mozilla.javascript.Script)10 Function (org.mozilla.javascript.Function)9 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)9 ArrayList (java.util.ArrayList)8 NativeObject (org.mozilla.javascript.NativeObject)8 Date (java.util.Date)7 RhinoException (org.mozilla.javascript.RhinoException)7 IOException (java.io.IOException)6 Map (java.util.Map)6 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 KettleException (org.pentaho.di.core.exception.KettleException)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 EvaluatorException (org.mozilla.javascript.EvaluatorException)4