@SuppressWarnings( "fallthrough" ) public static Object truncDate( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object 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 undefinedValue; } // This is the truncation of a date... // The second argument specifies the level: ms, s, min, hour, day, month, year // java.util.Date dArg1 = null; Integer level = null; try { dArg1 = (java.util.Date) ArgList[0]; level = (Integer) ArgList[1]; } catch ( Exception e ) { throw new RuntimeException( e.toString() ); } return ScriptAddedFunctions.truncDate( dArg1, level ); } else { throw new RuntimeException( "The function call truncDate requires 2 arguments: a date and a level (int)" ); } }
public static void LoadScriptFile( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { for ( int i = 0; i < ArgList.length; i++ ) { // don't worry about "undefined" arguments checkAndLoadJSFile( actualContext, actualObject, (String) ArgList[i] ); } }
public static void println( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { print( actualContext, actualObject, ArgList, FunctionContext ); java.lang.System.out.println(); }
public static Object isDate( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return undefinedValue; } /* java.util.Date d = (java.util.Date) */ return Boolean.TRUE; } catch ( Exception e ) { return Boolean.FALSE; } } else { throw new RuntimeException( "The function call isDate requires 1 argument." ); } }
rtn = ScriptAddedFunctions.truncDate( dateBase, 5 ); c2.setTime( rtn ); Assert.assertEquals( Calendar.JANUARY, c2.get( Calendar.MONTH ) ); rtn = ScriptAddedFunctions.truncDate( dateBase, 4 ); c2.setTime( rtn ); Assert.assertEquals( 1, c2.get( Calendar.DAY_OF_MONTH ) ); rtn = ScriptAddedFunctions.truncDate( dateBase, 3 ); c2.setTime( rtn ); Assert.assertEquals( 0, c2.get( Calendar.HOUR_OF_DAY ) ); rtn = ScriptAddedFunctions.truncDate( dateBase, 2 ); c2.setTime( rtn ); Assert.assertEquals( 0, c2.get( Calendar.MINUTE ) ); rtn = ScriptAddedFunctions.truncDate( dateBase, 1 ); c2.setTime( rtn ); Assert.assertEquals( 0, c2.get( Calendar.SECOND ) ); rtn = ScriptAddedFunctions.truncDate( dateBase, 0 ); c2.setTime( rtn ); Assert.assertEquals( 0, c2.get( Calendar.MILLISECOND ) ); try { ScriptAddedFunctions.truncDate( rtn, 6 ); // Should throw exception Assert.fail( "Expected exception - passed in level > 5 to truncDate" ); } catch ( Exception expected ) { ScriptAddedFunctions.truncDate( rtn, -7 ); // Should throw exception Assert.fail( "Expected exception - passed in level < 0 to truncDate" ); } catch ( Exception expected ) {
private static boolean isUndefined( Object ArgList ) { return isUndefined( new Object[] { ArgList }, new int[] { 0 } ); }
private static boolean isNull( Object ArgList ) { return isNull( new Object[] { ArgList }, new int[] { 0 } ); }
public static int indexOf( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { int returnIndex = -1; if ( ArgList.length == 2 || ArgList.length == 3 ) { if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) { String string = (String) ArgList[0]; String subString = (String) ArgList[1]; int fromIndex = 0; if ( ArgList.length == 3 ) { fromIndex = (int) Math.round( (Double) ArgList[2] ); } returnIndex = string.indexOf( subString, fromIndex ); } } else { throw new RuntimeException( "The function call indexOf requires 2 or 3 arguments" ); } return returnIndex; }
public static String rpad( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object 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) undefinedValue; } String valueToPad = (String) ArgList[0]; String filler = (String) ArgList[1]; int size = (Integer) ArgList[2]; while ( valueToPad.length() < size ) { valueToPad = valueToPad + filler; } return valueToPad; } } catch ( Exception e ) { throw new RuntimeException( "The function call rpad requires 3 arguments." ); } return null; }
public static Object isNum( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return undefinedValue; } double sArg1 = (Double) ArgList[0]; if ( Double.isNaN( sArg1 ) ) { return Boolean.FALSE; } else { return Boolean.TRUE; } } catch ( Exception e ) { return Boolean.FALSE; } } else { throw new RuntimeException( "The function call isNum requires 1 argument." ); } }
public static String ltrim( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) undefinedValue; } String strValueToTrim = (String) ArgList[0]; return strValueToTrim.replaceAll( "^\\s+", "" ); } else { throw new RuntimeException( "The function call ltrim requires 1 argument." ); } } catch ( Exception e ) { throw new RuntimeException( "The function call ltrim is not valid : " + e.getMessage() ); } }
public static String lpad( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object 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) undefinedValue; } String valueToPad = (String) ArgList[0]; String filler = (String) ArgList[1]; int size = (Integer) ArgList[2]; while ( valueToPad.length() < size ) { valueToPad = filler + valueToPad; } return valueToPad; } } catch ( Exception e ) { throw new RuntimeException( "The function call lpad requires 3 arguments." ); } return null; }
public static Object abs( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return undefinedValue; } else { return new Double( Math.abs( (Double) ArgList[0] ) ); } } catch ( Exception e ) { return null; } } else { throw new RuntimeException( "The function call abs requires 1 argument." ); } }
public static Object ceil( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return undefinedValue; } else { return new Double( Math.ceil( (Double) ArgList[0] ) ); } } catch ( Exception e ) { return null; } } else { throw new RuntimeException( "The function call ceil requires 1 argument." ); } }
public static String rtrim( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) undefinedValue; } String strValueToTrim = (String) ArgList[0]; return strValueToTrim.replaceAll( "\\s+$", "" ); } else { throw new RuntimeException( "The function call rtrim requires 1 argument." ); } } catch ( Exception e ) { throw new RuntimeException( "The function call rtrim is not valid : " + e.getMessage() ); } }
public static Object fileExists( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { try { if ( ArgList.length == 1 && !isNull( ArgList[0] ) && !isUndefined( ArgList[0] ) ) { if ( ArgList[0].equals( null ) ) { return null; } File file = new File( (String) ArgList[0] ); return Boolean.valueOf( file.isFile() ); } else { throw new RuntimeException( "The function call fileExists requires 1 valid argument." ); } } catch ( Exception e ) { throw new RuntimeException( e.toString() ); } }
public static Object floor( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return new Double( Double.NaN ); } else if ( isUndefined( ArgList[0] ) ) { return undefinedValue; } else { return new Double( Math.floor( (Double) ArgList[0] ) ); } } catch ( Exception e ) { return null; // throw new RuntimeException(e.toString()); } } else { throw new RuntimeException( "The function call floor requires 1 argument." ); } }
public static String upper( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) undefinedValue; } sRC = (String) ArgList[0]; sRC = sRC.toUpperCase(); } catch ( Exception e ) { throw new RuntimeException( "The function call upper is not valid : " + e.getMessage() ); } } else { throw new RuntimeException( "The function call upper requires 1 argument." ); } return sRC; }
public static void touch( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { try { if ( ArgList.length == 1 && !isNull( ArgList[0] ) && !isUndefined( ArgList[0] ) ) { File file = new File( (String) ArgList[0] ); boolean success = file.createNewFile(); if ( !success ) { file.setLastModified( System.currentTimeMillis() ); } } else { throw new RuntimeException( "The function call touch requires 1 valid argument." ); } } catch ( Exception e ) { throw new RuntimeException( e.toString() ); } }
public static String lower( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) undefinedValue; } sRC = (String) ArgList[0]; sRC = sRC.toLowerCase(); } catch ( Exception e ) { throw new RuntimeException( "The function call lower is not valid : " + e.getMessage() ); } } else { throw new RuntimeException( "The function call lower requires 1 argument." ); } return sRC; }