Saturday, August 25, 2012

Orion BB Start! for BlackBerry Java Development


Start a project on google code for BlackBerry Java Development
http://code.google.com/p/orion-bb/

For BlackBerry Java Development


Features

  • Splash Screen
  • Advanced UI Components such as Accordions, Tab, Tooltip, etc.
  • Barcode scanner
  • More features
Anyone interested in contributing please email me at boylevantz@gmail.com


Check it out!


Monday, August 13, 2012

Executing Javascript code in Java

Here some snippet how to execute Javascript code in Java


final String script = "function test(){ return 'abc' + 'def'; } test();";

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

Object result = engine.eval(script);

Assert.assertEquals("java.lang.String", result.getClass().getName());
Assert.assertEquals("abcdef", result.toString());

And some other code


final String script = "function execute(){ return { 'U.1' : function(){ return { 'u': '2', 'r': '', 'f':'U' }; }, 'R.5': function(){ return 'B' } } } execute();";

ScriptEngine js = new ScriptEngineManager().getEngineByExtension("js");
js.getContext().setAttribute("out", System.out, ScriptContext.ENGINE_SCOPE);

Object jsResult = js.eval(script);
NativeObject obj = (NativeObject) jsResult;
Object objUserRole = NativeObject.getProperty(obj, "U.1");

SimpleBindings bindings = new SimpleBindings();
bindings.put("foomethod", objUserRole);
log.debug("RESULT: " + js.eval("foomethod();", bindings));
log.debug("RESULT: " + js.eval("foomethod().u", bindings));
log.debug("RESULT: " + js.eval("foomethod().r", bindings));
log.debug("RESULT: " + js.eval("foomethod().f", bindings));

Assert.assertEquals("2", js.eval("foomethod().u", bindings).toString());
Assert.assertEquals("", js.eval("foomethod().r", bindings).toString());
Assert.assertEquals("U", js.eval("foomethod().f", bindings).toString());

objUserRole = NativeObject.getProperty(obj, "R.5");
bindings.put("foomethod", objUserRole);
log.debug("RESULT: " + js.eval("foomethod();", bindings));
Assert.assertEquals("B",js.eval("foomethod();", bindings).toString());

Using Expression Language

This is an example using expression language in Java



import javax.el.ValueExpression;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jlego.core.Dto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.odysseus.el.util.SimpleContext;

public class TestExpression extends TestCase {

private static final Logger log = LoggerFactory.getLogger(TestExpression.class);

public void testJuel(){
java.util.Properties properties = new java.util.Properties();
properties.put("javax.el.cacheSize", "5000");
javax.el.ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl(properties);

Map map= new HashMap();
map .put("data", "ali");

SimpleContext context = new SimpleContext();
context.setVariable("request", factory.createValueExpression(map, HashMap.class));
ValueExpression expr = factory.createValueExpression(context, "${request.data == 'ali'}", Object.class);

Assert.assertEquals("true", expr.getValue(context).toString());

ValueExpression expr2 = factory.createValueExpression(context, "${ (1==0) ? 'ali' : ((2==2) ? 'not not ali' : 'not ali') }", Object.class);

Assert.assertEquals("not not ali",expr2.getValue(context));
}
}

I'm using Maven, so make sure you have these dependencies:


<dependency>
<groupId>de.odysseus.juel</groupId>
<artifactId>juel</artifactId>
<version>2.1.3</version>
</dependency>

Of course JUnit as well


<dependency>
        <groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>