Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Custom factory tutorial



Overview

Suppose that we want all our scripts to implement the following interface:
      public interface MyScript {
        public int compute();
      }

We will need to create a custom ScriptFactory, which will use one ScriptLanguageFactory to create script for this interface.

Create the ScriptFactory


We need to implement the ScriptFactory.createScriptLanguageFactoryImpl(short, int, ScriptSource) method to create the ScriptLanguageFactory.

In our case, we have only one ScriptLanguageFactory because we want to implement the same interface for all scripts, so we don't need to take care of any of the arguments of the method. We will have this very simple code:

      public class CustomScriptFactory extends AbstractScriptFactory {
        private final CustomScriptLanguageFactory langFactory = new CustomScriptLanguageFactory();

        public CustomScriptFactory() {
          super();
          langFactories.put(CustomScript.class, langFactory);
        }

        public ScriptLanguageFactory<?> createScriptLanguageFactoryImpl(short scriptType, int countScripts, ScriptSource source) {
          return langFactory;
        }
      }      

As you see, we keep the unique ScriptLanguageFactory as a field of our ScriptFactory nand we will return it for all scripts.

Create the ScriptLanguageFactory

Now we need to implement the ScriptLanguageFactory. The characteristic of this factory is that it will create scripts implementing the MyScript interface:

      public class DefaultScriptLanguageFactory extends AbstractScriptLanguageFactory<MyScript> {
        protected Class<ScriptBuilder> createBuilderClass(short scriptType) {  
         create a builder which will create scripts of the specified script type
        }
      }    

Create the ScriptBuilders

The ScriptBuilder is the class which is responsible for creating scripts of:
  • A particular script type
  • And implementing a particular interface
Let's begin by creating a builder for the Groovy language:

      public class MyGroovyScriptBuilder extends AbstractScriptBuilder {
        public ScriptWrapper<MyScript> createScriptWrapper() {
          // create the GroovyScriptWrapper, see the documentation for the scriptHelper scripting library
          // (https://sourceforge.net/projects/scripthelper/)
          ScriptWrapper<MyScript>  wrapper = new GroovyScriptWrapper<MyScript> () {
          };
          // add the default imports for the javaXUL library
          addDefaultImports(wrapper);
          // set the exception listener and the script context 
          wrapper.addExceptionListener(exceptionListener);
          wrapper.setScriptContext(context);
          return wrapper;
        }
      }      

We can do the same for all the languages we want to implement. We will then have the following code in our ScriptLanguageFactory:

      public class MyScriptLanguageFactory extends AbstractScriptLanguageFactory<MyScript> {
        protected Class<ScriptBuilder> createBuilderClass(short scriptType) {  
          Class<ScriptBuilder> clazz = null;
          if (scriptType == ScriptTypes.GROOVY) {
            clazz = createBuilderClassImpl("my.package.MyGroovyScriptBuilder");
          } else if (scriptType == ScriptTypes.JAVASCRIPT) {
            clazz = createBuilderClassImpl("org.xul.script.custom.factory.MyJSScriptBuilder");
          } // etc...
          }      
          return clazz;
        }
      }    

Using the ScriptFactory

To use our ScriptFactory, we just have to call the ScriptManager.setScriptFactory(ScriptFactory) method in our ScriptManager. For example:

      ScriptManager manager = new DefaultScriptManager();
      manager.setScriptFactory(new CustomScriptFactory());

See also


Categories: tutorials

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