Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Scripting architecture



Scripting in javaXUL use the scriptHelper library. By default there are no constraints on the scripts method declaration, but it is possible to customize the cration of scripts, for example to make them implement specific interfaces, for all sccripts or only for some of them.

Overview

Main Article: ScriptFactory

The framework is based on two factories: scriptfactory

ScriptFactory

To create a custom ScriptFactory, you just have to implement the ScriptFactory.createScriptLanguageFactoryImpl(short, int, ScriptSource) method to return the appropriate ScriptLanguageFactory depending on:
  • The script type (automatically created by the framework from the MIME type provided by the user and the file extension if the MIME type is not defined)
  • The ScriptSource which encapsulate the script file
Using a custom ScriptFactory allows to:
  • Make the script implemetn a specific interface
  • Have different ScriptContext depending on the script

ScriptLanguageFactory

To create a custom ScriptLanguageFactory, you just have to implement the AbstractScriptLanguageFactory.createBuilderClass(short) and declare which interface this factory makes the scripts implement.

For example, suppose that we want our scripts to implement the following interface:

      public interface MyScript {
        public default void subscribe() {      
      }

Then we could have the following ScriptLanguageFactory code:

      public class CustomScriptLanguageFactory extends AbstractScriptLanguageFactory<MyScript> {
        protected Class<ScriptBuilder> createBuilderClass(short scriptType) {
          Class<ScriptBuilder> clazz = null;
          if (scriptType == ScriptTypes.GROOVY) {
            clazz = createBuilderClassImpl("my.factory.MyGroovyScriptBuilder");
          } else if (scriptType == ScriptTypes.JAVASCRIPT) {
            clazz = createBuilderClassImpl("my.factory.MyJSScriptBuilder");
          } else if (scriptType == ScriptTypes.Ruby) {
            clazz = createBuilderClassImpl("my.factory.MyRubyScriptBuilder");      
          } else if (scriptType == ScriptTypes.Python) {
            clazz = createBuilderClassImpl("my.factory.MyPythonScriptBuilder");            
          }
          return clazz;
        }
      }

And for the code of the builders:

      public class CustomGroovyScriptBuilder extends AbstractScriptBuilder {
        public ScriptWrapper<MyScript> createScriptWrapper() {
          ScriptWrapper<MyScript> wrapper = new GroovyScriptWrapper<MyScript>() {
          };
          wrapper.addExceptionListener(exceptionListener);
          wrapper.setScriptContext(context);
          return wrapper;
        }
      }      

See also


Categories: scripts

Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence