org.mozilla.javascript.Context.jsToJava()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 JavaScript  
字(15.2k)|赞(0)|评价(0)|浏览(296)

本文整理了Java中org.mozilla.javascript.Context.jsToJava()方法的一些代码示例,展示了Context.jsToJava()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.jsToJava()方法的具体详情如下:
包路径:org.mozilla.javascript.Context
类名称:Context
方法名:jsToJava

Context.jsToJava介绍

[英]Convert a JavaScript value into the desired type. Uses the semantics defined with LiveConnect3 and throws an Illegal argument exception if the conversion cannot be performed.
[中]将JavaScript值转换为所需类型。使用LiveConnect3定义的语义,如果无法执行转换,则引发非法参数异常。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

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() );
  }
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static String jsToString( Object value, String classType ) {
 if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" )
  || classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) {
  // Is it a java Value class ?
  try {
   Value v = (Value) Context.jsToJava( value, Value.class );
   return v.toString();
  } catch ( Exception ev ) {
   // convert to a string should work in most cases...
   //
   return Context.toString( value );
  }
 } else {
  // A String perhaps?
  return Context.toString( value );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

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();
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

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() );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

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() );
 }
}

代码示例来源:origin: facebook/stetho

@Override
public @Nullable Object evaluate(@NonNull String expression) throws Throwable {
  Object result;
  final Context jsContext = enterJsContext();
  try {
   result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null);
   // Google chrome automatically saves the last expression to `$_`, we do the same
   Object jsValue = Context.javaToJS(result, mJsScope);
   ScriptableObject.putProperty(mJsScope, "$_", jsValue);
  } finally {
   Context.exit();
  }
  return Context.jsToJava(result, Object.class);
}

代码示例来源:origin: pentaho/pentaho-kettle

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() );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static Object convertFromJs( Object value, int type, String fieldName ) throws KettleValueException {
 String classType = value.getClass().getName();
 switch ( type ) {
  case ValueMetaInterface.TYPE_NUMBER:
   return jsToNumber( value, classType );
  case ValueMetaInterface.TYPE_INTEGER:
   return jsToInteger( value, value.getClass() );
  case ValueMetaInterface.TYPE_STRING:
   return jsToString( value, classType );
  case ValueMetaInterface.TYPE_DATE:
   return jsToDate( value, classType );
  case ValueMetaInterface.TYPE_BOOLEAN:
   return value;
  case ValueMetaInterface.TYPE_BIGNUMBER:
   return jsToBigNumber( value, classType );
  case ValueMetaInterface.TYPE_BINARY: {
   return Context.jsToJava( value, byte[].class );
  }
  case ValueMetaInterface.TYPE_NONE: {
   throw new RuntimeException( "No data output data type was specified for new field ["
    + fieldName + "]" );
  }
  default:
   return Context.jsToJava( value, Object.class );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static Object getNextWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList,
 Function FunctionContext ) {
 // (Date dIn){
 if ( ArgList.length == 1 ) {
  try {
   if ( isNull( ArgList[0] ) ) {
    return null;
   } else if ( isUndefined( ArgList[0] ) ) {
    return Context.getUndefinedValue();
   }
   java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
   Calendar startDate = Calendar.getInstance();
   startDate.setTime( dIn );
   startDate.add( Calendar.DATE, 1 );
   while ( startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY
    || startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SUNDAY ) {
    startDate.add( Calendar.DATE, 1 );
   }
   return startDate.getTime();
  } catch ( Exception e ) {
   throw Context.reportRuntimeError( e.toString() );
  }
 } else {
  throw Context.reportRuntimeError( "The function call getNextWorkingDay requires 1 argument." );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static void putRow( Context actualContext, Scriptable actualObject, Object[] ArgList,
 Function FunctionContext ) {
 if ( ArgList.length == 1 ) {
  try {
   Object[] newRow = (Object[]) Context.jsToJava( ArgList[0], ( new Object[] {} ).getClass() );
   Object scmO = actualObject.get( "_step_", actualObject );
   try {
    ScriptValuesMod step = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class );
    step.putRow( step.getOutputRowMeta(), newRow );
   } catch ( Exception e ) {
    ScriptValuesModDummy step = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class );
    step.putRow( step.getOutputRowMeta(), newRow );
   }
  } catch ( Exception e ) {
   throw Context.reportRuntimeError( "Unable to pass the new row to the next step(s) because of an error: "
    + Const.CR + e.toString() );
  }
 } else {
  throw Context
   .reportRuntimeError( "The function call putRow requires 1 argument : the output row data (Object[])" );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static RowMetaInterface getInputRowMeta( Context actualContext, Scriptable actualObject,
 Object[] ArgList, Function FunctionContext ) {
 if ( ArgList.length == 0 ) {
  try {
   Object scmO = actualObject.get( "_step_", actualObject );
   try {
    ScriptValuesMod scm = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class );
    return scm.getInputRowMeta();
   } catch ( Exception e ) {
    ScriptValuesModDummy scm = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class );
    return scm.getInputRowMeta();
   }
  } catch ( Exception e ) {
   throw Context.reportRuntimeError( "Unable to get the input row metadata because of an error: "
    + Const.CR + e.toString() );
  }
 } else {
  throw Context.reportRuntimeError( "The function call getInputRowMeta doesn't require arguments." );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

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" );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static RowMetaInterface getOutputRowMeta( Context actualContext, Scriptable actualObject,
 Object[] ArgList, Function FunctionContext ) {
 if ( ArgList.length == 0 ) {
  try {
   Object scmO = actualObject.get( "_step_", actualObject );
   try {
    ScriptValuesMod scm = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class );
    return scm.getOutputRowMeta();
   } catch ( Exception e ) {
    ScriptValuesModDummy scm = (ScriptValuesModDummy) Context.jsToJava( scmO, ScriptValuesModDummy.class );
    return scm.getOutputRowMeta();
   }
  } catch ( Exception e ) {
   throw Context.reportRuntimeError( "Unable to get the output row metadata because of an error: "
    + Const.CR + e.toString() );
  }
 } else {
  throw Context.reportRuntimeError( "The function call getOutputRowMeta doesn't require arguments." );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static Object isWorkingDay( 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();
   } else {
    java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
    Calendar startDate = Calendar.getInstance();
    startDate.setTime( dIn );
    if ( startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SATURDAY
     && startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) {
     return Boolean.TRUE;
    }
    return Boolean.FALSE;
   }
  } catch ( Exception e ) {
   return null;
  }
 } else {
  throw Context.reportRuntimeError( "The function call isWorkingDay requires 1 argument." );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

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() );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static String getEnvironmentVar( Context actualContext, Scriptable actualObject, Object[] ArgList,
 Function FunctionContext ) {
 String sRC = "";
 if ( ArgList.length == 1 ) {
  try {
   String sArg1 = Context.toString( ArgList[0] );
   // PDI-1276 Function getEnvironmentVar() does not work for user defined variables.
   // check if the system property exists, and if it does not, try getting a Kettle var instead
   if ( System.getProperties().containsValue( sArg1 ) ) {
    sRC = System.getProperty( sArg1, "" );
   } else {
    Object scmo = actualObject.get( "_step_", actualObject );
    Object scmO = Context.jsToJava( scmo, StepInterface.class );
    if ( scmO instanceof StepInterface ) {
     StepInterface scm = (StepInterface) Context.jsToJava( scmO, StepInterface.class );
     sArg1 = Context.toString( ArgList[0] );
     sRC = scm.getVariable( sArg1, "" );
    } else {
     // running in test mode, return ""
     sRC = "";
    }
   }
  } catch ( Exception e ) {
   sRC = "";
  }
 } else {
  throw Context.reportRuntimeError( "The function call getEnvironmentVar requires 1 argument." );
 }
 return sRC;
}

代码示例来源:origin: pentaho/pentaho-kettle

@SuppressWarnings( "fallthrough" )
public static Object truncDate( Context actualContext, Scriptable actualObject, Object[] ArgList, Function 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 Context.getUndefinedValue();
  }
  // This is the truncation of a date...
  // The second argument specifies the level: ms, s, min, hour, day, month, year
  //
  Date dArg1 = null;
  Integer level = null;
  try {
   dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
   level = (Integer) Context.jsToJava( ArgList[1], Integer.class );
  } catch ( Exception e ) {
   throw Context.reportRuntimeError( e.toString() );
  }
  return truncDate( dArg1, level );
 } else {
  throw Context
   .reportRuntimeError( "The function call truncDate requires 2 arguments: a date and a level (int)" );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static String getVariable( Context actualContext, Scriptable actualObject, Object[] ArgList,
 Function FunctionContext ) {
 String sRC = "";
 String sArg1 = "";
 String sArg2 = "";
 if ( ArgList.length == 2 ) {
  try {
   Object scmo = actualObject.get( "_step_", actualObject );
   Object scmO = Context.jsToJava( scmo, StepInterface.class );
   if ( scmO instanceof StepInterface ) {
    StepInterface scm = (StepInterface) Context.jsToJava( scmO, StepInterface.class );
    sArg1 = Context.toString( ArgList[0] );
    sArg2 = Context.toString( ArgList[1] );
    return scm.getVariable( sArg1, sArg2 );
   } else {
    // running via the Test button in a dialog
    sArg2 = Context.toString( ArgList[1] );
    return sArg2;
   }
  } catch ( Exception e ) {
   sRC = "";
  }
 } else {
  throw Context.reportRuntimeError( "The function call getVariable requires 2 arguments." );
 }
 return sRC;
}

代码示例来源:origin: pentaho/pentaho-kettle

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." );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static void setVariable( Context actualContext, Scriptable actualObject, Object[] arguments,
 Function functionContext ) {
 if ( arguments.length != 3 ) {
  throw Context.reportRuntimeError( "The function call setVariable requires 3 arguments." );
 }
 Object stepObject = Context.jsToJava( actualObject.get( "_step_", actualObject ), StepInterface.class );
 if ( stepObject instanceof StepInterface ) {
  StepInterface step = (StepInterface) stepObject;
  Trans trans = step.getTrans();
  final String variableName = Context.toString( arguments[ 0 ] );
  final String variableValue = Context.toString( arguments[ 1 ] );
  final VariableScope variableScope = getVariableScope( Context.toString( arguments[ 2 ] ) );
  // Set variable in step's scope so that it can be retrieved in the same step using getVariable
  step.setVariable( variableName, variableValue );
  switch ( variableScope ) {
   case PARENT:
    setParentScopeVariable( trans, variableName, variableValue );
    break;
   case GRAND_PARENT:
    setGrandParentScopeVariable( trans, variableName, variableValue );
    break;
   case ROOT:
    setRootScopeVariable( trans, variableName, variableValue );
    break;
   case SYSTEM:
    setSystemScopeVariable( trans, variableName, variableValue );
    break;
  }
 }
}

相关文章

Context类方法