Java Code Examples for org.mozilla.javascript.Context#toNumber()

The following examples show how to use org.mozilla.javascript.Context#toNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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." );
  }
}
 
Example 2
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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." );
  }
}
 
Example 5
Source File: JavaScriptUtils.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static BigDecimal jsToBigNumber( Object value, String classType ) {
  if ( classType.equalsIgnoreCase( JS_UNDEFINED ) ) {
    return null;
  } else if ( classType.equalsIgnoreCase( JS_NATIVE_NUM ) ) {
    Number nb = Context.toNumber( value );
    return BigDecimal.valueOf( nb.doubleValue() );
  } else if ( classType.equalsIgnoreCase( JS_NATIVE_JAVA_OBJ ) ) {
    // Is it a BigDecimal class ?
    return convertNativeJavaToBigDecimal( value );
  } else if ( classType.equalsIgnoreCase( "java.lang.Byte" ) ) {
    return BigDecimal.valueOf( ( (Byte) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Short" ) ) {
    return BigDecimal.valueOf( ( (Short) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Integer" ) ) {
    return BigDecimal.valueOf( ( (Integer) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Long" ) ) {
    return BigDecimal.valueOf( ( (Long) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Double" ) ) {
    return BigDecimal.valueOf( ( (Double) value ).doubleValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.String" ) ) {
    return BigDecimal.valueOf( ( new Long( (String) value ) ).longValue() );
  } else {
    throw new UnsupportedOperationException( "JavaScript conversion to BigNumber not implemented for " + classType );
  }
}
 
Example 6
Source File: JavaScriptUtils.java    From hop with Apache License 2.0 6 votes vote down vote up
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 {
        return (Long) Context.jsToJava( value, Long.class );
      } catch ( Exception e2 ) {
        String string = Context.toString( value );
        return Long.parseLong( Const.trim( string ) );
      }
    } else {
      return Long.parseLong( value.toString() );
    }
  }
}
 
Example 7
Source File: JavaScriptUtils.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static Date jsToDate( Object value, String classType ) throws KettleValueException {
  double dbl;
  if ( !classType.equalsIgnoreCase( JS_UNDEFINED ) ) {
    if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeDate" ) ) {
      dbl = Context.toNumber( value );
    } else if ( classType.equalsIgnoreCase( JS_NATIVE_JAVA_OBJ )
      || classType.equalsIgnoreCase( "java.util.Date" ) ) {
      // Is it a java Date() class ?
      try {
        Date dat = (Date) Context.jsToJava( value, java.util.Date.class );
        dbl = dat.getTime();
      } catch ( Exception e ) {
        // Is it a Value?
        //
        return convertValueToDate( value );
      }
    } else if ( classType.equalsIgnoreCase( "java.lang.Double" ) ) {
      dbl = (Double) value;
    } else {
      String string = (String) Context.jsToJava( value, String.class );
      dbl = Double.parseDouble( string );
    }
    long lng = Math.round( dbl );
    return new Date( lng );
  }
  return null;
}
 
Example 8
Source File: JavaScriptUtils.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
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( JS_UNDEFINED ) ) {
      return null;
    } else if ( classType.equalsIgnoreCase( JS_NATIVE_NUM ) ) {
      Number nb = Context.toNumber( value );
      return nb.longValue();
    } else if ( classType.equalsIgnoreCase( JS_NATIVE_JAVA_OBJ ) ) {
      // 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() );
    }
  }
}
 
Example 9
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
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." );
  }
}
 
Example 10
Source File: DataFrameAdapter.java    From joinery with GNU General Public License v3.0 5 votes vote down vote up
public static Scriptable jsFunction_tail(final Context ctx, final Scriptable object, final Object[] args, final Function func) {
    final Number limit = args.length == 1 ? Context.toNumber(args[0]) : null;
    return new DataFrameAdapter(object,
            limit != null ?
                cast(object).df.tail(limit.intValue()) :
                cast(object).df.tail()
        );
}
 
Example 11
Source File: JsGlobal.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
		Object[] args) {
	double num = Context.toNumber(args[0]);
	try {
		Thread.sleep((long) num);
	} catch (InterruptedException e) {
	}
	return super.call(cx, scope, thisObj, args);
}
 
Example 12
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
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." );
  }
}
 
Example 13
Source File: JavaScriptUtils.java    From hop with Apache License 2.0 5 votes vote down vote up
public static BigDecimal jsToBigNumber( Object value, String classType ) {
  if ( classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) {
    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" ) ) {
    // Is it a BigDecimal class ?
    try {
      return (BigDecimal) Context.jsToJava( value, BigDecimal.class );
    } catch ( Exception e ) {
      String string = (String) Context.jsToJava( value, String.class );
      return new BigDecimal( string );
    }
  } else if ( classType.equalsIgnoreCase( "java.lang.Byte" ) ) {
    return new BigDecimal( ( (Byte) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Short" ) ) {
    return new BigDecimal( ( (Short) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Integer" ) ) {
    return new BigDecimal( ( (Integer) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Long" ) ) {
    return new BigDecimal( ( (Long) value ).longValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.Double" ) ) {
    return new BigDecimal( ( (Double) value ).doubleValue() );
  } else if ( classType.equalsIgnoreCase( "java.lang.String" ) ) {
    return new BigDecimal( ( new Long( (String) value ) ).longValue() );
  } else {
    throw new RuntimeException( "JavaScript conversion to BigNumber not implemented for " + classType );
  }
}
 
Example 14
Source File: ExternalArrayTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Override
public void setArrayElement(int index, Object value)
{
    elements[index] = (int)Context.toNumber(value);
}
 
Example 15
Source File: DataFrameAdapter.java    From joinery with GNU General Public License v3.0 4 votes vote down vote up
public static Scriptable jsFunction_diff(final Context ctx, final Scriptable object, final Object[] args, final Function func) {
    final Number period = args.length == 1 ? Context.toNumber(args[0]) : 1;
    return new DataFrameAdapter(object, cast(object).df.diff(period.intValue()));
}
 
Example 16
Source File: DataFrameAdapter.java    From joinery with GNU General Public License v3.0 4 votes vote down vote up
public static Scriptable jsFunction_percentChange(final Context ctx, final Scriptable object, final Object[] args, final Function func) {
    final Number period = args.length == 1 ? Context.toNumber(args[0]) : 1;
    return new DataFrameAdapter(object, cast(object).df.percentChange(period.intValue()));
}
 
Example 17
Source File: DataFrameAdapter.java    From joinery with GNU General Public License v3.0 4 votes vote down vote up
public static Object jsFunction_toString(final Context ctx, final Scriptable object, final Object[] args, final Function func) {
    final Number limit = args.length == 1 ? Context.toNumber(args[0]) : null;
    return limit != null ?
        cast(object).df.toString(limit.intValue()) :
        cast(object).df.toString();
}
 
Example 18
Source File: JavascriptEvalUtil.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Handles a Rhino script evaluation result, converting Javascript object
 * into equivalent Java objects if necessary.
 * @param inputObj Object returned by rhino engine.
 * @return If inputObj is a native Javascript object, its equivalent Java object 
 *   is returned; otherwise inputObj is returned
 */
public static Object convertJavascriptValue(Object inputObj)
{
	if ( inputObj instanceof Undefined )
	{
		return null;
	}
	if ( inputObj instanceof IdScriptableObject ) 
	{
		// Return type is possibly a Javascript native object
		// Convert to Java object with same value
		String jsClass = ((Scriptable) inputObj).getClassName();
		if ( "Date".equals(jsClass) ) 
		{
			return Context.toType( inputObj, Date.class );
		} 
		else if ( "Boolean".equals(jsClass)) 
		{
			return Boolean.valueOf( Context.toBoolean( inputObj ) );
		} 
		else if ( "Number".equals(jsClass)) 
		{
			return new Double(Context.toNumber(inputObj));
		} 
		else if( "String".equals(jsClass) )
		{
			return inputObj.toString();
		}
		else if ( "Array".equals( jsClass ) )
		{
			Object[] obj = new Object[(int) ( (NativeArray) inputObj ).getLength( )];
			for ( int i = 0; i < obj.length; i++ )
			{
				obj[i] = convertJavascriptValue( ( (NativeArray) inputObj ).get( i,
						null ) );
			}
			return obj;
		}
	}
	else if ( inputObj instanceof Wrapper )
	{
	    return ( (Wrapper) inputObj ).unwrap( );
	}
	else if ( inputObj instanceof Scriptable )
	{
		return ((Scriptable) inputObj).getDefaultValue( null );
	}
	
	return inputObj;
}
 
Example 19
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static Object dateAdd( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  if ( ArgList.length == 3 ) {
    try {
      if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
        return Context.getUndefinedValue();
      }
      java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      String strType = Context.toString( ArgList[1] ).toLowerCase();
      int iValue = (int) Context.toNumber( ArgList[2] );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dIn );
      if ( strType.equals( "y" ) ) {
        cal.add( Calendar.YEAR, iValue );
      } else if ( strType.equals( "m" ) ) {
        cal.add( Calendar.MONTH, iValue );
      } else if ( strType.equals( "d" ) ) {
        cal.add( Calendar.DATE, iValue );
      } else if ( strType.equals( "w" ) ) {
        cal.add( Calendar.WEEK_OF_YEAR, iValue );
      } else if ( strType.equals( "wd" ) ) {
        int iOffset = 0;
        while ( iOffset < iValue ) {
          cal.add( Calendar.DATE, 1 );
          int day = cal.get( Calendar.DAY_OF_WEEK );
          if ( ( day != Calendar.SATURDAY ) && ( day != Calendar.SUNDAY ) ) {
            iOffset++;
          }
        }
      } else if ( strType.equals( "hh" ) ) {
        cal.add( Calendar.HOUR, iValue );
      } else if ( strType.equals( "mi" ) ) {
        cal.add( Calendar.MINUTE, iValue );
      } else if ( strType.equals( "ss" ) ) {
        cal.add( Calendar.SECOND, iValue );
      }
      return cal.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call dateAdd requires 3 arguments." );
  }
}
 
Example 20
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 4 votes vote down vote up
public static Object dateAdd( Context actualContext, Scriptable actualObject, Object[] ArgList,
                              Function FunctionContext ) {
  if ( ArgList.length == 3 ) {
    try {
      if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
        return Context.getUndefinedValue();
      }
      Date dIn = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      String strType = Context.toString( ArgList[ 1 ] ).toLowerCase();
      int iValue = (int) Context.toNumber( ArgList[ 2 ] );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dIn );
      if ( strType.equals( "y" ) ) {
        cal.add( Calendar.YEAR, iValue );
      } else if ( strType.equals( "m" ) ) {
        cal.add( Calendar.MONTH, iValue );
      } else if ( strType.equals( "d" ) ) {
        cal.add( Calendar.DATE, iValue );
      } else if ( strType.equals( "w" ) ) {
        cal.add( Calendar.WEEK_OF_YEAR, iValue );
      } else if ( strType.equals( "wd" ) ) {
        int iOffset = 0;
        while ( iOffset < iValue ) {
          cal.add( Calendar.DATE, 1 );
          int day = cal.get( Calendar.DAY_OF_WEEK );
          if ( ( day != Calendar.SATURDAY ) && ( day != Calendar.SUNDAY ) ) {
            iOffset++;
          }
        }
      } else if ( strType.equals( "hh" ) ) {
        cal.add( Calendar.HOUR, iValue );
      } else if ( strType.equals( "mi" ) ) {
        cal.add( Calendar.MINUTE, iValue );
      } else if ( strType.equals( "ss" ) ) {
        cal.add( Calendar.SECOND, iValue );
      }
      return cal.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call dateAdd requires 3 arguments." );
  }
}