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

The following examples show how to use org.mozilla.javascript.Context#reportRuntimeError() . 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: QueryResults.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public Object get( String name, Scriptable scope )
{
	if( ScriptConstants.OUTER_RESULT_KEYWORD.equalsIgnoreCase( name ) )
	{
		if( this.parentHelper == null )
		{
			throw Context.reportRuntimeError( DataResourceHandle.getInstance( ).getMessage( ResourceConstants.NO_OUTER_RESULTS_EXIST ) );
		}
		else
			return this.parentHelper.getScriptable( );
	}
	try
	{
		return this.currentIterator.getValue( name );
	}
	catch ( BirtException e )
	{
		return e;
	}
}
 
Example 2
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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() );
  }
}
 
Example 3
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static void touch( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 && !isNull( ArgList[0] ) && !isUndefined( ArgList[0] ) ) {
      File file = new File( Context.toString( ArgList[0] ) );
      boolean success = file.createNewFile();
      if ( !success ) {
        file.setLastModified( System.currentTimeMillis() );
      }
    } else {
      throw Context.reportRuntimeError( "The function call touch requires 1 valid argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example 4
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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() );
  }
}
 
Example 6
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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();
      }
      Date dArg1 = (Date) Context.jsToJava( ArgList[ 0 ], 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() );
  }
}
 
Example 7
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static int getOcuranceString( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  int nr = 0;
  if ( ArgList.length == 2 ) {
    try {
      if ( isNull( ArgList[0] ) ) {
        return 0;
      } else if ( isUndefined( ArgList[0] ) ) {
        return (Integer) Context.getUndefinedValue();
      }
      if ( isNull( ArgList[1] ) ) {
        return 0;
      } else if ( isUndefined( ArgList[1] ) ) {
        return (Integer) Context.getUndefinedValue();
      }
      String string = Context.toString( ArgList[0] );
      String searchFor = Context.toString( ArgList[1] );
      nr = Const.getOcuranceString( string, searchFor );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "The function call getOcuranceString is not valid" );
    }
  } else {
    throw Context.reportRuntimeError( "The function call getOcuranceString is not valid" );
  }
  return nr;
}
 
Example 8
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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." );
  }
}
 
Example 9
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
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." );
  }
}
 
Example 10
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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() );
  }
}
 
Example 11
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static void appendToFile( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                 Function FunctionContext ) {

  if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {
    try {
      FileOutputStream file = new FileOutputStream( Context.toString( ArgList[ 0 ] ), true );
      DataOutputStream out = new DataOutputStream( file );
      out.writeBytes( Context.toString( ArgList[ 1 ] ) );
      out.flush();
      out.close();
    } catch ( Exception er ) {
      throw Context.reportRuntimeError( er.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call appendToFile requires arguments." );
  }
}
 
Example 12
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 13
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
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." );
  }
}
 
Example 14
Source File: JSOutputParams.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public Object get( String name, Scriptable scope )
{
	try
	{
		Object paramValue = dataSet.getOutputParameterValue(name);
		return JavascriptEvalUtil.convertToJavascriptValue( paramValue,
				dataSet.getSharedScope( ) );
	}
	catch ( BirtException e )
	{
		throw Context.reportRuntimeError( e.getLocalizedMessage( ) );
	}

}
 
Example 15
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Date truncDate( Date dArg1, Integer level ) {
  Calendar cal = Calendar.getInstance();
  cal.setTime( dArg1 );

  switch ( level.intValue() ) {
    // MONTHS
    case 5:
      cal.set( Calendar.MONTH, 0 );
      // DAYS
    case 4:
      cal.set( Calendar.DAY_OF_MONTH, 1 );
      // HOURS
    case 3:
      cal.set( Calendar.HOUR_OF_DAY, 0 );
      // MINUTES
    case 2:
      cal.set( Calendar.MINUTE, 0 );
      // SECONDS
    case 1:
      cal.set( Calendar.SECOND, 0 );
      // MILI-SECONDS
    case 0:
      cal.set( Calendar.MILLISECOND, 0 );
      break;
    default:
      throw Context.reportRuntimeError( "Argument of TRUNC of date has to be between 0 and 5" );
  }
  return cal.getTime();
}
 
Example 16
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
static VariableScope getVariableScope( String codeOfScope ) {
  switch ( codeOfScope ) {
    case "s":
      return VariableScope.SYSTEM;
    case "r":
      return VariableScope.ROOT;
    case "p":
      return VariableScope.PARENT;
    case "g":
      return VariableScope.GRAND_PARENT;
    default:
      throw Context.reportRuntimeError( "The argument type of function call "
        + "setVariable should either be \"s\", \"r\", \"p\", or \"g\"." );
  }
}
 
Example 17
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static String escapeHtml( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    return Const.escapeHtml( Context.toString( ArgList[0] ) );
  } else {
    throw Context.reportRuntimeError( "The function call escapeHtml requires 1 argument." );
  }
}
 
Example 18
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
public static String unEscapeHtml( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                   Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    return Const.unEscapeHtml( Context.toString( ArgList[ 0 ] ) );
  } else {
    throw Context.reportRuntimeError( "The function call unEscapeHtml requires 1 argument." );
  }
}
 
Example 19
Source File: JSCubeBindingObject.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public Object get( String arg0, Scriptable scope )
{
	try
	{
		if( ScriptConstants.OUTER_RESULT_KEYWORD.equals( arg0 ))
			return cursor.getObject( ScriptConstants.OUTER_RESULT_KEYWORD );
		return cursor.getObject( arg0 );
	}
	catch ( OLAPException e )
	{
		throw Context.reportRuntimeError( e.getLocalizedMessage( ) );
	}
}
 
Example 20
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static boolean LuhnCheck( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {

  boolean returnCode = false;

  if ( ArgList.length == 1 ) {
    if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {
      try {
        int sum = 0;
        int digit = 0;
        int addend = 0;
        boolean timesTwo = false;
        String argstring = Context.toString( ArgList[0] );

        for ( int i = argstring.length() - 1; i >= 0; i-- ) {
          digit = Integer.parseInt( argstring.substring( i, i + 1 ) );
          if ( timesTwo ) {
            addend = digit * 2;
            if ( addend > 9 ) {
              addend -= 9;
            }
          } else {
            addend = digit;
          }
          sum += addend;
          timesTwo = !timesTwo;
        }

        int modulus = sum % 10;
        if ( modulus == 0 ) {
          returnCode = true;
        }
      } catch ( Exception e ) {
        // No Need to throw exception
        // This means that input can not be parsed to Integer
      }

    }
  } else {
    throw Context.reportRuntimeError( "The function call LuhnCheck requires 1 argument." );

  }
  return returnCode;
}