Package javax.script

Examples of javax.script.ScriptEngine


  }
 
  public String toExport(ItemSimple itemSimple) throws Exception {
    Vector<String> vector = new Vector<String>();
   
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    engine.put("item", itemSimple);
   
    for (ItemSimpleCsvMapping definition : mappings) {
      if (definition == null) {
        vector.add("");
        continue;
      }
     
      boolean isGroup = !Format.isNullOrEmpty(definition.getIeProfileGroupName());
      int index = -1;
      if (isGroup) {
        index = definition.getIeProfileGroupIndex().intValue();
      }
     
      Object object = null;
      if (!Format.isNullOrEmpty(definition.getIeProfileFieldValue())) {
        engine.eval(definition.getIeProfileFieldValue());
        object = engine.get("value");
      }
      else {
        String ieProfileMethodName = "";
        if (!isGroup) {
          ieProfileMethodName = definition.getIeProfileFieldName();
View Full Code Here


      itemImages[i] = new ItemSimpleImage();
    }
    itemSimple.setItemImages(itemImages);
   
    String tokens[] = this.fromCsv(input);
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    engine.put("tokens", tokens);
    engine.put("value", "");
   
    int count = 0;
    for (ItemSimpleCsvMapping definition : mappings) {
      if (definition.getIeProfilePosition() == null && definition.getIeProfileFieldValue() == null) {
        continue;
      }
     
      boolean isGroup = false;
      if (!Format.isNullOrEmpty(definition.getIeProfileGroupName())) {
        isGroup = true;
      }
     
      Object object = itemSimple;
      if (isGroup) {
        String ieProfileGroupName = definition.getIeProfileGroupName();
        String methodName = "get" + ieProfileGroupName.substring(0, 1).toUpperCase() + ieProfileGroupName.substring(1);
        Method method = ItemSimple.class.getMethod(methodName, (Class[]) null);
        Object parameters[] = {};
        Object arrays[] = (Object[]) method.invoke(itemSimple, parameters);
        object = Array.get(arrays, definition.getIeProfileGroupIndex());
      }

      String ieProfileFieldName = definition.getIeProfileFieldName();
      String methodName = "set" + ieProfileFieldName.substring(0, 1).toUpperCase() + ieProfileFieldName.substring(1);
      Class<?> types[] = new Class[1];
      Field f = object.getClass().getDeclaredField(ieProfileFieldName);
      types[0] = f.getType();
     
      Method method = object.getClass().getMethod(methodName, types);
      Object parameters[] = {null};
     
      String fieldValue = "";
      if (definition.getIeProfilePosition() != null) {
        fieldValue = tokens[definition.getIeProfilePosition().intValue() - 1];
      }
      if (!Format.isNullOrEmpty(definition.getIeProfileFieldValue())) {
        try {
          engine.eval(definition.getIeProfileFieldValue());
        }
        catch (javax.script.ScriptException e) {
          String message = "[" + definition.getIeProfileFieldValue() + "] - [" + e.getMessage() + "]";
          throw new ItemCsvTransformationException(message);
        }
        Object result = engine.get("value");
        if (result instanceof String) {
          fieldValue = (String) result;
        }
        else {
          NativeJavaObject javaObject = (NativeJavaObject) result;
View Full Code Here

      throw new OCommandScriptException("Unsupported language: " + language + ". Supported languages are: " + engines);

    if (script == null)
      throw new OCommandScriptException("Invalid script: " + script);

    final ScriptEngine scriptEngine = engines.get(language);

    if (scriptEngine == null)
      throw new OCommandScriptException("Cannot find script engine: " + language);

    final Bindings binding = scriptEngine.createBindings();

    // BIND FIXED VARIABLES
    binding.put("db", database);

    // BIND PARAMETERS INTO THE SCRIPT
    if (iArgs != null)
      for (int i = 0; i < iArgs.size(); ++i) {
        binding.put("$" + i, iArgs.get(i));
      }

    try {
      Object result = null;
      result = scriptEngine.eval(script, binding);

      return result;
    } catch (ScriptException e) {
      throw new OCommandScriptException("Error on execution of the script", request.getText(), 0, e);
    }
View Full Code Here

      throw new OCommandScriptException("Unsupported language: " + language + ". Supported languages are: " + engines);

    if (script == null)
      throw new OCommandScriptException("Invalid script: null");

    final ScriptEngine scriptEngine = engines.get(language);

    if (scriptEngine == null)
      throw new OCommandScriptException("Cannot find script engine: " + language);

    final Bindings binding = scriptEngine.createBindings();

    // BIND FIXED VARIABLES
    binding.put("db", database);

    // BIND PARAMETERS INTO THE SCRIPT
    if (iArgs != null)
      for (int i = 0; i < iArgs.size(); ++i) {
        binding.put("$" + i, iArgs.get(i));
      }

    try {
      Object result = null;
      result = scriptEngine.eval(script, binding);

      return result;
    } catch (ScriptException e) {
      throw new OCommandScriptException("Error on execution of the script", request.getText(), 0, e);
    }
View Full Code Here

        mgr.put("null_Key", null);
        assertNull(mgr.get("null_Key"));
    }

    public void testGetEngineByExtension() {
        ScriptEngine engine;

        engine =  mgr.getEngineByExtension("tEst");
        assertNotNull(engine);
        assertTrue(engine instanceof TestScriptEngine);
View Full Code Here

        assertNotNull(engine);
        assertTrue(engine instanceof TestScriptEngine);
    }

    public void testGetEngineByMimeType() {
        ScriptEngine engine;
        engine =  mgr.getEngineByMimeType("application/junit");
        assertNotNull(engine);
        assertTrue(engine instanceof TestScriptEngine);
    }
View Full Code Here

        assertNotNull(engine);
        assertTrue(engine instanceof TestScriptEngine);
    }

    public void testGetEngineByName() {
        ScriptEngine engine;

        engine =  mgr.getEngineByName("JUnit");
        assertNotNull(engine);
        assertTrue(engine instanceof TestScriptEngine);
    }
View Full Code Here

        mgr.setBindings(bindings);
        assertSame(bindings, mgr.getBindings());
    }

    public void testEvalAndCompile() throws ScriptException{
        ScriptEngine se = mgr.getEngineByName("JUnit");
        assertNotNull(se);
        se.put("key", "value");
        assertEquals("value",se.eval("key"));
        if (se instanceof Compilable){
            Compilable co = (Compilable) se;
            CompiledScript cs = co.compile("key");
            assertNotNull(cs);
            assertEquals("value",cs.eval());
View Full Code Here

    private static final long serialVersionUID = 232L;

    @Override
    public void process() {
        try {
            ScriptEngine scriptEngine = getScriptEngine();
            processFileOrScript(scriptEngine, null);
        } catch (ScriptException e) {
            log.error("Problem in JSR223 script ", e);
        } catch (IOException e) {
            log.error("Problem in JSR223 script ", e);
View Full Code Here

    @Override
    public AssertionResult getResult(SampleResult response) {
        AssertionResult result = new AssertionResult(getName());
        try {
            ScriptEngine scriptEngine = getScriptEngine();
            Bindings bindings = scriptEngine.createBindings();
            bindings.put("SampleResult", response);
            bindings.put("AssertionResult", result);
            processFileOrScript(scriptEngine, bindings);
            result.setError(false);
        } catch (IOException e) {
View Full Code Here

TOP

Related Classes of javax.script.ScriptEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.