private static boolean isUndefined( Object[] ArgList, int[] iArrToCheck ) { for ( int i = 0; i < iArrToCheck.length; i++ ) { if ( ArgList[iArrToCheck[i]].equals( Context.getUndefinedValue() ) ) { return true; } } return false; }
public JsRuntimeReplFactoryBuilder(@NonNull android.content.Context context) { mContext = context; // We import the app's package name by default mPackages.add(context.getPackageName()); // Predefine $_ which holds the value of the last expression evaluated mVariables.put("$_", Context.getUndefinedValue()); }
public static Object isRegExp( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { if ( ArgList.length >= 2 ) { if ( isNull( ArgList, new int[] { 0, 1 } ) ) { return null; } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) { return Context.getUndefinedValue(); } String strToMatch = Context.toString( ArgList[0] ); for ( int i = 1; i < ArgList.length; i++ ) { Pattern p = Pattern.compile( Context.toString( ArgList[i] ) ); Matcher m = p.matcher( strToMatch ); if ( m.matches() ) { return new Double( i ); } } } return new Double( -1 ); }
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() ); } }
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 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 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 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 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 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 String ltrim( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) Context.getUndefinedValue(); } String strValueToTrim = Context.toString( ArgList[0] ); return strValueToTrim.replaceAll( "^\\s+", "" ); } else { throw Context.reportRuntimeError( "The function call ltrim requires 1 argument." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call ltrim is not valid : " + e.getMessage() ); } }
public static String rtrim( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length == 1 ) { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) Context.getUndefinedValue(); } String strValueToTrim = Context.toString( ArgList[0] ); return strValueToTrim.replaceAll( "\\s+$", "" ); } else { throw Context.reportRuntimeError( "The function call rtrim requires 1 argument." ); } } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call rtrim is not valid : " + e.getMessage() ); } }
public static String lower( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) Context.getUndefinedValue(); } sRC = Context.toString( ArgList[0] ); sRC = sRC.toLowerCase(); } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call lower is not valid : " + e.getMessage() ); } } else { throw Context.reportRuntimeError( "The function call lower requires 1 argument." ); } return sRC; }
public static String upper( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) Context.getUndefinedValue(); } sRC = Context.toString( ArgList[0] ); sRC = sRC.toUpperCase(); } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call upper is not valid : " + e.getMessage() ); } } else { throw Context.reportRuntimeError( "The function call upper requires 1 argument." ); } return sRC; }
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 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 String replace( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { try { if ( ArgList.length >= 2 && ( ArgList.length - 1 ) % 2 == 0 ) { if ( isNull( ArgList, new int[] { 0, 1 } ) ) { return null; } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) { return (String) Context.getUndefinedValue(); } String objForReplace = Context.toString( ArgList[0] ); for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) { objForReplace = objForReplace.replaceAll( Context.toString( ArgList[i] ), Context.toString( ArgList[i + 1] ) ); } return objForReplace; } else { throw Context.reportRuntimeError( "The function call replace is not valid (wrong number of arguments)" ); } } catch ( Exception e ) { throw Context.reportRuntimeError( "Function call replace is not valid : " + e.getMessage() ); } }
public static String trim( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) { String sRC = ""; if ( ArgList.length == 1 ) { try { if ( isNull( ArgList[0] ) ) { return null; } else if ( isUndefined( ArgList[0] ) ) { return (String) Context.getUndefinedValue(); } sRC = Context.toString( ArgList[0] ); sRC = Const.trim( sRC ); } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call trim is not valid : " + e.getMessage() ); } } else { throw Context.reportRuntimeError( "The function call trim requires 1 argument." ); } return sRC; }
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 removeCRLF( 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 (String) Context.getUndefinedValue(); } return Const.removeCRLF( Context.toString( ArgList[0] ) ); } catch ( Exception e ) { throw Context.reportRuntimeError( "The function call removeCRLF is not valid" ); } } else { throw Context.reportRuntimeError( "The function call removeCRLF is not valid" ); } } }