public Number getAsNumber() { return Double.valueOf(Context.toNumber(scriptable)); }
@Override public double apply(Object[] args) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } return Context.toNumber(fn.call(cx, scope, scope, args)); } };
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 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(); } }
return null; } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeNumber" ) ) { Number nb = Context.toNumber( value ); return new BigDecimal( nb.doubleValue() ); } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) ) {
int iValue = (int) Context.toNumber( ArgList[2] ); Calendar cal = Calendar.getInstance(); cal.setTime( dIn );
public static Object abs( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } else { return new Double( Math.abs( Context.toNumber( ArgList[0] ) ) ); } } catch ( Exception e ) { return null; } } else { throw Context.reportRuntimeError( "The function call abs requires 1 argument." ); } }
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 Object floor( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } else { return new Double( Math.floor( Context.toNumber( ArgList[0] ) ) ); } } catch ( Exception e ) { return null; // throw Context.reportRuntimeError(e.toString()); } } else { throw Context.reportRuntimeError( "The function call floor requires 1 argument." ); } }
public static Object ceil( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return Double.NaN; } else if ( isUndefined( ArgList[0] ) ) { return Context.getUndefinedValue(); } else { return Math.ceil( Context.toNumber( ArgList[ 0 ] ) ); } } catch ( Exception e ) { return null; } } else { throw Context.reportRuntimeError( "The function call ceil requires 1 argument." ); } }
public static int indexOf( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { int returnIndex = -1; if ( ArgList.length == 2 || ArgList.length == 3 ) { if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) { String string = Context.toString( ArgList[0] ); String subString = Context.toString( ArgList[1] ); int fromIndex = 0; if ( ArgList.length == 3 ) { fromIndex = (int) Math.round( Context.toNumber( ArgList[2] ) ); } returnIndex = string.indexOf( subString, fromIndex ); } } else { throw Context.reportRuntimeError( "The function call indexOf requires 2 or 3 arguments" ); } return returnIndex; }
} else { if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeDate" ) ) { dbl = Context.toNumber( value ); } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) || classType.equalsIgnoreCase( "java.util.Date" ) ) {
public static Object isNum( 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(); } double sArg1 = Context.toNumber( ArgList[0] ); if ( Double.isNaN( sArg1 ) ) { return Boolean.FALSE; } else { return Boolean.TRUE; } } catch ( Exception e ) { return Boolean.FALSE; } } else { throw Context.reportRuntimeError( "The function call isNum requires 1 argument." ); } }
public static String fillString( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length == 2 ) { try { if ( isNull( ArgList, new int[] { 0, 1 } ) ) { return null; } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) { return (String) Context.getUndefinedValue(); } String fillChar = Context.toString( ArgList[0] ); int count = (int) Context.toNumber( ArgList[1] ); if ( fillChar.length() != 1 ) { throw Context.reportRuntimeError( "Please provide a valid Char to the fillString" ); } else { char[] chars = new char[count]; while ( count > 0 ) { chars[--count] = fillChar.charAt( 0 ); } return new String( chars ); } } catch ( Exception e ) { throw Context.reportRuntimeError( e.toString() ); } } else { throw Context.reportRuntimeError( "The function call fillString requires 2 arguments." ); } }
return (String) Context.getUndefinedValue(); double sArg1 = Context.toNumber( ArgList[0] ); if ( Double.isNaN( sArg1 ) ) { throw Context.reportRuntimeError( "The first Argument must be a Number." ); return (String) Context.getUndefinedValue(); double sArg1 = Context.toNumber( ArgList[0] ); if ( Double.isNaN( sArg1 ) ) { throw Context.reportRuntimeError( "The first Argument must be a Number." ); return (String) Context.getUndefinedValue(); double sArg1 = Context.toNumber( ArgList[0] ); if ( Double.isNaN( sArg1 ) ) { throw Context.reportRuntimeError( "The first Argument must be a Number." );
public static String lpad( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { // (String valueToPad, String filler, int size) { try { if ( ArgList.length == 3 ) { if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) { return null; } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) { return (String) Context.getUndefinedValue(); } String valueToPad = Context.toString( ArgList[0] ); String filler = Context.toString( ArgList[1] ); int size = (int) Context.toNumber( ArgList[2] ); while ( valueToPad.length() < size ) { valueToPad = filler + valueToPad; } return valueToPad; } } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call lpad requires 3 arguments." ); } return null; }
public static String rpad( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 3 ) { if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) { return null; } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) { return (String) Context.getUndefinedValue(); } String valueToPad = Context.toString( ArgList[0] ); String filler = Context.toString( ArgList[1] ); int size = (int) Context.toNumber( ArgList[2] ); while ( valueToPad.length() < size ) { valueToPad = valueToPad + filler; } return valueToPad; } } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call rpad requires 3 arguments." ); } return null; }
} else { if ( classname.equalsIgnoreCase( "org.mozilla.javascript.NativeDate" ) ) { dbl = Context.toNumber( result ); } else if ( classname.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) ) {
iTranStat = (int) Context.toNumber( data.scope.get( "trans_Status", data.scope ) ); } else { iTranStat = CONTINUE_TRANSFORMATION;