0

I have following task for my project: I need to validate a java object, based on rules in a script (for example javascript).

Why in javascript? Because in javascript I can be flexible and create rules as I want (e.g. validating a combination of fields like validate(tax,recipient))

Now here is what I have:

1) I have validation rules defined in a javascript file, Rules.js

function checkPrice(price){
  if(price < 0){
    return false;
  }
}

2) I have a plain Java Object (Invoice). And it must stay plain!

public class Invoice implements Serializable {

  private String details;
  private String tax;
  private String recipient;
  private double price;

  //getter and setter
}

3) And I have a ValidatorObject. This can be a java or javascript object. Depending on your suggestion.

This ValidatorObject has a method validate, which has the Javascript Rules File (see Point 1) and the Java Object, Invoice, (see Point 2) as parameters.

validate(Rules.js, Invoice i){

  //here it must take the Rules.js and use the rules inside to validate the Invoice i 

}

So my question would be: Are there any frameworks that I can use to validate a Java Object based on rules defined in a javascript file? Or any tutorials, videos or suggestions?

Or how can I read a javascript file into a java object? Are there any getters or setters for javascript?

Anything would be nice!

Regards, Dave

5
  • 1
    why javascript is needed? why not java for writing rules?? Commented Oct 19, 2012 at 12:42
  • Can you give an example of how using Javascript is helps be flexible and create rules as I want? The example you gave would be simpler in Java. Commented Oct 19, 2012 at 12:42
  • 1
    You can use Rhino to execute JavaScript from Java. Commented Oct 19, 2012 at 12:42
  • @PeterLawrey 1) Because I am using Apache Wicket as the web application framework, to build forms and so on, 2) Because I need a declarative approach, the assistant from the university wants it so... Commented Oct 20, 2012 at 14:19
  • I still don't why you can't do both of those things dynamically in Java. Commented Oct 20, 2012 at 15:26

2 Answers 2

0

Here is how to embed a ScripEngine into Java Application:

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;



public static void main( String[] args )
   throws
      ScriptException, IOException
{
   final String run;
   if( args.length > 0 )
   {
      run = args[0];
      if( run.contains( "=" ))
      {
         usage();
      }
   }
   else
   {
      run = "run.js";
   }
   File script = new File( run );
   if( script.canRead())
   {
      ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType( "text/javascript" );
      Bindings bindings = engine.getBindings( ScriptContext.GLOBAL_SCOPE );
      bindings.put( "controller", new Controller());
      for( int i = 1; i < args.length; ++i )
      {
         String[] varVal = args[i].split( "=" );
         if( varVal.length == 2 )
         {
            bindings.put( varVal[0], varVal[1] );
         }
         else
         {
            usage();
         }
      }
      info( "Loading and executing: " + script );
      engine.put( ScriptEngine.FILENAME, script.toString());
      engine.eval( new FileReader( script ));
   }
   else
   {
      System.err.println( "Can't read automation script file: " + script );
      usage();
   }
}

In this case the Controller Java class exposes some public methods to JavaScript to use the flexibility of the interpreted code coupled to the robustness of compiled Java code.

1
  • I heard I can use Rhino too. but what is the difference? Is Rhino not a part of the java script library? Commented Oct 30, 2012 at 21:53
0

I solved it using Rhino! where I can read and manipulate my javascript file...

https://developer.mozilla.org/en-US/docs/Rhino

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.