DynamicJava

*
*                 Sun Public License Notice
* 
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
* 
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*/

package org.netbeans.modules.scripting;


import java.io.StringReader;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;

import koala.dynamicjava.interpreter.InterpreterException;
import koala.dynamicjava.interpreter.TreeInterpreter;
import koala.dynamicjava.parser.wrapper.JavaCCParserFactory;

/**
* Script service which provides an interface 
* to the DynamicJava {@link http://www.inria.fr/koala/djava}
*
* @author David Strupl
* @see AbstractScriptType
* @see org.netbeans.execution.ScriptType
*/
public class DynamicJavaScriptType extends AbstractScriptType {

   /** Generated serial version UID. */
   static final long serialVersionUID = -6919647689707066209L;

   /** Dynamic java extension. */
   private static final String DJAVA_EXTENSION = "djava"; // NOI18N
                               
   /** Dynamic java script service type display name. */
   private static final String DJAVA_DISPLAY_NAME = "DynamicJava"; // NOI18N
                               
   /** Interpreter of djava script. */
   private transient TreeInterpreter dJavaInterpreter;
                               
   /** Creates bean shell script service.
   * Registers djava file extension. */
   public DynamicJavaScriptType() {
       // Register extensions to be recognized for this script type
       getExtensions().addExtension(DJAVA_EXTENSION);
   }

   /** Creates clone. Overrides superclass method. */
   protected Object clone() throws CloneNotSupportedException {
       return new DynamicJavaScriptType();
   }
                               
   /** Gets interpreter. 
    * @return instance of TreeInterpreter
    * @see koala.dynamicjava.interpreter.TreeInterpreter */
   private TreeInterpreter getInterpreter() {
      if(dJavaInterpreter == null) {
          dJavaInterpreter = new TreeInterpreter(new JavaCCParserFactory());
      }
                                   
      return dJavaInterpreter;
   }
                               
   /** Gets display name. Overrides superclass method. */
   protected String displayName() {
       return DJAVA_DISPLAY_NAME;
   }
                               
   /** Sets variable to interpreter. Implements superclass abstract method.
    * @param name name of variable to set 
    * @param value value of the variable */
   public void addVariable(String name, Object value) {
       TreeInterpreter interpreter = getInterpreter();
                                   
       if(value == null) {
          // Unset variable if it is there.
          if(interpreter.getVariableNames().contains(name))
              interpreter.setVariable(name, null);
       } else {
           if(interpreter.getVariableNames().contains(name))
               // Set new value.
               interpreter.setVariable(name, value);
           else
                // Define new variable.
                interpreter.defineVariable(name, value);
       }
   }

   /** Evaluates an expression. Overrides superclass method. 
    * @param script script to evaluate 
    * @param context script context */
   public Object eval(String script, Context context) throws InvocationTargetException {
      try {
          if (!script.endsWith(";")) // NOI18N
              script += ";";         // NOI18N

          Reader reader = new StringReader(script);
                                       
          return getInterpreter().interpret(reader, displayName()); 
      } catch (InterpreterException ie) {
          throw new InvocationTargetException(ie, displayName());
      }
   }
                               
}

Legal Notices