The ScriptProvider Abstract Base Class

From Apache OpenOffice Wiki
Jump to: navigation, search



The ScriptProvider class is an abstract Java class with three abstract methods:

  // this method is used to get a script for a script URI
  public abstract XScript getScript( String scriptURI )
      throws com.sun.star.uno.RuntimeException,
          com.sun.star.script.provider.ScriptFrameworkErrorException;
 
  // This method is used to determine whether the ScriptProvider has a ScriptEditor
  public abstract boolean hasScriptEditor();
 
  // This method is used to get the ScriptEditor for this ScriptProvider
  public abstract ScriptEditor getScriptEditor();

The most important method is the getScript() method which must be implemented in order for OpenOffice.org to execute macros in your scripting language. Fortunately this is made easy by a set of helper methods in the ScriptProvider class, and by a set of helper classes which implement the BrowseNode API described in Writing a LanguageScriptProvider UNO Component From Scratch.

Here is an example of a ScriptProvider implementation for a new ScriptProviderForYourLanguage:

  import com.sun.star.uno.XComponentContext;
  import com.sun.star.lang.XMultiServiceFactory;
  import com.sun.star.lang.XSingleServiceFactory;
  import com.sun.star.registry.XRegistryKey;
  import com.sun.star.comp.loader.FactoryHelper;
  import com.sun.star.lang.XServiceInfo;
  import com.sun.star.lang.XInitialization;
 
  import com.sun.star.script.provider.XScriptContext;
  import com.sun.star.script.provider.XScript;
  import com.sun.star.script.framework.provider.ScriptProvider;
  import com.sun.star.script.framework.provider.ScriptEditor;
  import com.sun.star.script.framework.container.ScriptMetaData;
 
  public class ScriptProviderForYourLanguage
  {
      public static class _ScriptProviderForYourLanguage extends ScriptProvider
      {
          public _ScriptProviderForYourLanguage(XComponentContext ctx)
          {
              super (ctx, "YourLanguage");
          }
 
          public XScript getScript(String scriptURI)
              throws com.sun.star.uno.RuntimeException,
                  com.sun.star.script.provider.ScriptFrameworkErrorException
          {
              YourLanguageScript script = null;
 
              try
              {
                  ScriptMetaData scriptMetaData = getScriptData(scriptURI);
                  XScriptContext xScriptContext = getScriptingContext();
                  script = new YourLanguageScript(xScriptContext, scriptMetaData);
              }
              catch (com.sun.star.uno.Exception e)
              {
                  System.err.println("Failed to get script: " + scriptURI);
              }
              return script;
          }
 
          public boolean hasScriptEditor()
          {
              return true;
          }
 
          public ScriptEditor getScriptEditor()
          {
              return new ScriptEditorForYourLanguage();
          }
      }
 
      // code to register and create a service factory for ScriptProviderForYourLanguage
      // this code is the standard code for registering classes which implement UNO services
      public static XSingleServiceFactory __getServiceFactory( String implName,
              XMultiServiceFactory multiFactory,
              XRegistryKey regKey )
      {
          XSingleServiceFactory xSingleServiceFactory = null;
 
          if ( implName.equals( ScriptProviderForYourLanguage._ScriptProviderForYourLanguage.class.getName() ) )
          {
              xSingleServiceFactory = FactoryHelper.getServiceFactory(
                  ScriptProviderForYourLanguage._ScriptProviderForYourLanguage.class,
                      "com.sun.star.script.provider.ScriptProviderForYourLanguage",
                      multiFactory,
                      regKey );
          }
 
          return xSingleServiceFactory;
      }
 
      public static boolean __writeRegistryServiceInfo( XRegistryKey regKey )
      {
          String impl =
              "ScriptProviderForYourLanguage$_ScriptProviderForYourLanguage";
 
          String service1 =
              "com.sun.star.script.provider.ScriptProvider";
 
          String service2 =
              "com.sun.star.script.provider.LanguageScriptProvider";
 
          String service3 =
              "com.sun.star.script.provider.ScriptProviderForYourLanguage";
 
          FactoryHelper.writeRegistryServiceInfo(impl, service1, regKey);
          FactoryHelper.writeRegistryServiceInfo(impl, service2, regKey);
          FactoryHelper.writeRegistryServiceInfo(impl, service3, regKey);
 
          return true;
      }
  }

The getScriptData() and getScriptingContext() methods, make the implementation of the getScript() method easy.

The __getServiceFactory() and __writeRegistryServiceInfo() methods are standard Apache OpenOffice methods for registering UNO components. The only thing you need to change in them is the name of your ScriptProvider.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages