public static Long jsToInteger( Object value, Class<?> clazz ) { if ( Number.class.isAssignableFrom( clazz ) ) { return ( (Number) value ).longValue(); } else { String classType = clazz.getName(); if ( classType.equalsIgnoreCase( "java.lang.String" ) ) { return ( new Long( (String) value ) ); } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) { return null; } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeNumber" ) ) { Number nb = Context.toNumber( value ); return nb.longValue(); } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) ) { // Is it a Value? // try { Value v = (Value) Context.jsToJava( value, Value.class ); return v.getInteger(); } catch ( Exception e2 ) { String string = Context.toString( value ); return Long.parseLong( Const.trim( string ) ); } } else { return Long.parseLong( value.toString() ); } } }
public static String jsToString( Object value, String classType ) { if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) || classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) { // Is it a java Value class ? try { Value v = (Value) Context.jsToJava( value, Value.class ); return v.toString(); } catch ( Exception ev ) { // convert to a string should work in most cases... // return Context.toString( value ); } } else { // A String perhaps? return Context.toString( value ); } }
public static Number jsToNumber( Object value, String classType ) { if ( classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) { return null; } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) ) { try { // Is it a java Value class ? Value v = (Value) Context.jsToJava( value, Value.class ); return v.getNumber(); } catch ( Exception e ) { String string = Context.toString( value ); return Double.parseDouble( Const.trim( string ) ); } } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeNumber" ) ) { Number nb = Context.toNumber( value ); return nb.doubleValue(); } else { Number nb = (Number) value; return nb.doubleValue(); } }
public static Object year( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar cal = Calendar.getInstance(); cal.setTime( dArg1 ); return new Double( cal.get( Calendar.YEAR ) ); } else { throw Context.reportRuntimeError( "The function call year requires 1 argument." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } }
public static Object week( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar cal = Calendar.getInstance(); cal.setTime( dArg1 ); return new Double( cal.get( Calendar.WEEK_OF_YEAR ) ); } else { throw Context.reportRuntimeError( "The function call week requires 1 argument." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } }
@Override public @Nullable Object evaluate(@NonNull String expression) throws Throwable { Object result; final Context jsContext = enterJsContext(); try { result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null); // Google chrome automatically saves the last expression to `$_`, we do the same Object jsValue = Context.javaToJS(result, mJsScope); ScriptableObject.putProperty(mJsScope, "$_", jsValue); } finally { Context.exit(); } return Context.jsToJava(result, Object.class); }
public static Object month( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar cal = Calendar.getInstance(); cal.setTime( dArg1 ); return new Double( cal.get( Calendar.MONTH ) ); } else { throw Context.reportRuntimeError( "The function call month requires 1 argument." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } }
public static Object convertFromJs( Object value, int type, String fieldName ) throws KettleValueException { String classType = value.getClass().getName(); switch ( type ) { case ValueMetaInterface.TYPE_NUMBER: return jsToNumber( value, classType ); case ValueMetaInterface.TYPE_INTEGER: return jsToInteger( value, value.getClass() ); case ValueMetaInterface.TYPE_STRING: return jsToString( value, classType ); case ValueMetaInterface.TYPE_DATE: return jsToDate( value, classType ); case ValueMetaInterface.TYPE_BOOLEAN: return value; case ValueMetaInterface.TYPE_BIGNUMBER: return jsToBigNumber( value, classType ); case ValueMetaInterface.TYPE_BINARY: { return Context.jsToJava( value, byte[].class ); } case ValueMetaInterface.TYPE_NONE: { throw new RuntimeException( "No data output data type was specified for new field [" + fieldName + "]" ); } default: return Context.jsToJava( value, Object.class ); } }
public static Object getNextWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { // (Date dIn){ if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar startDate = Calendar.getInstance(); startDate.setTime( dIn ); startDate.add( Calendar.DATE, 1 ); while ( startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY || startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SUNDAY ) { startDate.add( Calendar.DATE, 1 ); } return startDate.getTime(); } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } } else { throw Context.reportRuntimeError( "The function call getNextWorkingDay requires 1 argument." ); } }
public static void putRow( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { Object[] newRow = (Object[]) Context.jsToJava( ArgList[0], ( new Object[] {} ).getClass() ); Object scmO = actualObject.get( "_step_", actualObject ); try { ScriptValuesMod step = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class ); step.putRow( step.getOutputRowMeta(), newRow ); } catch ( Exception e ) { ScriptValuesModDummy step = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class ); step.putRow( step.getOutputRowMeta(), newRow ); } } catch ( Exception e ) { throw Context.reportRuntimeError( "Unable to pass the new row to the next step(s) because of an error: " + Const.CR + e.toString() ); } } else { throw Context .reportRuntimeError( "The function call putRow requires 1 argument : the output row data (Object[])" ); } }
public static RowMetaInterface getInputRowMeta( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 0 ) { try { Object scmO = actualObject.get( "_step_", actualObject ); try { ScriptValuesMod scm = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class ); return scm.getInputRowMeta(); } catch ( Exception e ) { ScriptValuesModDummy scm = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class ); return scm.getInputRowMeta(); } } catch ( Exception e ) { throw Context.reportRuntimeError( "Unable to get the input row metadata because of an error: " + Const.CR + e.toString() ); } } else { throw Context.reportRuntimeError( "The function call getInputRowMeta doesn't require arguments." ); } }
public static Object[] createRowCopy( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { int newSize = (int) Math.round( Context.toNumber( ArgList[0] ) ); Object scmO = actualObject.get( "row", actualObject ); Object[] row = (Object[]) Context.jsToJava( scmO, ( new Object[] {} ).getClass() ); return RowDataUtil.createResizedCopy( row, newSize ); } catch ( Exception e ) { throw Context.reportRuntimeError( "Unable to create a row copy: " + Const.CR + e.toString() ); } } else { throw Context .reportRuntimeError( "The function call createRowCopy requires a single arguments : the new size of the row" ); } }
public static RowMetaInterface getOutputRowMeta( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 0 ) { try { Object scmO = actualObject.get( "_step_", actualObject ); try { ScriptValuesMod scm = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class ); return scm.getOutputRowMeta(); } catch ( Exception e ) { ScriptValuesModDummy scm = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class ); return scm.getOutputRowMeta(); } } catch ( Exception e ) { throw Context.reportRuntimeError( "Unable to get the output row metadata because of an error: " + Const.CR + e.toString() ); } } else { throw Context.reportRuntimeError( "The function call getOutputRowMeta doesn't require arguments." ); } }
public static Object isWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } else { java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); Calendar startDate = Calendar.getInstance(); startDate.setTime( dIn ); if ( startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SATURDAY && startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) { return Boolean.TRUE; } return Boolean.FALSE; } } catch ( Exception e ) { return null; } } else { throw Context.reportRuntimeError( "The function call isWorkingDay requires 1 argument." ); } }
public static Object trunc( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { // 1 argument: normal truncation of numbers // if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } // This is the truncation of a number... // Double dArg1 = (Double) Context.jsToJava( ArgList[0], Double.class ); return Double.valueOf( Math.floor( dArg1 ) ); } else { throw Context.reportRuntimeError( "The function call trunc requires 1 argument, a number." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } }
public static String getEnvironmentVar( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { String sArg1 = Context.toString( ArgList[0] ); // PDI-1276 Function getEnvironmentVar() does not work for user defined variables. // check if the system property exists, and if it does not, try getting a Kettle var instead if ( System.getProperties().containsValue( sArg1 ) ) { sRC = System.getProperty( sArg1, "" ); } else { Object scmo = actualObject.get( "_step_", actualObject ); Object scmO = Context.jsToJava( scmo, StepInterface.class ); if ( scmO instanceof StepInterface ) { StepInterface scm = (StepInterface) Context.jsToJava( scmO, StepInterface.class ); sArg1 = Context.toString( ArgList[0] ); sRC = scm.getVariable( sArg1, "" ); } else { // running in test mode, return "" sRC = ""; } } } catch ( Exception e ) { sRC = ""; } } else { throw Context.reportRuntimeError( "The function call getEnvironmentVar requires 1 argument." ); } return sRC; }
@SuppressWarnings( "fallthrough" ) public static Object truncDate( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { // 2 arguments: truncation of dates to a certain precision // if ( ArgList.length == 2 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } // This is the truncation of a date... // The second argument specifies the level: ms, s, min, hour, day, month, year // Date dArg1 = null; Integer level = null; try { dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class ); level = (Integer) Context.jsToJava( ArgList[1], Integer.class ); } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } return truncDate( dArg1, level ); } else { throw Context .reportRuntimeError( "The function call truncDate requires 2 arguments: a date and a level (int)" ); } }
public static String getVariable( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { String sRC = ""; String sArg1 = ""; String sArg2 = ""; if ( ArgList.length == 2 ) { try { Object scmo = actualObject.get( "_step_", actualObject ); Object scmO = Context.jsToJava( scmo, StepInterface.class ); if ( scmO instanceof StepInterface ) { StepInterface scm = (StepInterface) Context.jsToJava( scmO, StepInterface.class ); sArg1 = Context.toString( ArgList[0] ); sArg2 = Context.toString( ArgList[1] ); return scm.getVariable( sArg1, sArg2 ); } else { // running via the Test button in a dialog sArg2 = Context.toString( ArgList[1] ); return sArg2; } } catch ( Exception e ) { sRC = ""; } } else { throw Context.reportRuntimeError( "The function call getVariable requires 2 arguments." ); } return sRC; }
public static Object isDate( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } /* java.util.Date d = (java.util.Date) */Context.jsToJava( ArgList[0], java.util.Date.class ); return Boolean.TRUE; } catch ( Exception e ) { return Boolean.FALSE; } } else { throw Context.reportRuntimeError( "The function call isDate requires 1 argument." ); } }
public static void setVariable( Context actualContext, Scriptable actualObject, Object[] arguments, Function functionContext ) { if ( arguments.length != 3 ) { throw Context.reportRuntimeError( "The function call setVariable requires 3 arguments." ); } Object stepObject = Context.jsToJava( actualObject.get( "_step_", actualObject ), StepInterface.class ); if ( stepObject instanceof StepInterface ) { StepInterface step = (StepInterface) stepObject; Trans trans = step.getTrans(); final String variableName = Context.toString( arguments[ 0 ] ); final String variableValue = Context.toString( arguments[ 1 ] ); final VariableScope variableScope = getVariableScope( Context.toString( arguments[ 2 ] ) ); // Set variable in step's scope so that it can be retrieved in the same step using getVariable step.setVariable( variableName, variableValue ); switch ( variableScope ) { case PARENT: setParentScopeVariable( trans, variableName, variableValue ); break; case GRAND_PARENT: setGrandParentScopeVariable( trans, variableName, variableValue ); break; case ROOT: setRootScopeVariable( trans, variableName, variableValue ); break; case SYSTEM: setSystemScopeVariable( trans, variableName, variableValue ); break; } } }