Implementing the XScript interface

From Apache OpenOffice Wiki
Jump to: navigation, search



The next step is to provide the YourLanguageScript implementation which will execute the macro code. The following example shows the code for the YourLanguageScript class:

  import com.sun.star.uno.Type;
  import com.sun.star.uno.Any;
  import com.sun.star.lang.IllegalArgumentException;
  import com.sun.star.lang.WrappedTargetException;
  import com.sun.star.reflection.InvocationTargetException;
  import com.sun.star.script.CannotConvertException;
 
  import com.sun.star.script.provider.XScriptContext;
  import com.sun.star.script.provider.XScript;
  import com.sun.star.script.provider.ScriptFrameworkErrorException;
  import com.sun.star.script.provider.ScriptFrameworkErrorType;
 
  import com.sun.star.script.framework.provider.ClassLoaderFactory;
  import com.sun.star.script.framework.container.ScriptMetaData;
 
  public class YourLanguageScript implements XScript
  {
      private XScriptContext xScriptContext;
      private ScriptMetaData scriptMetaData;
 
      public YourLanguageScript(XScriptContext xsc, ScriptMetaData smd)
      {
          this.xScriptContext = xsc;
          this.scriptMetaData = smd;
      }
 
      public Object invoke( Object[] aParams,
                            short[][] aOutParamIndex,
                            Object[][] aOutParam )
          throws com.sun.star.script.provider.ScriptFrameworkErrorException,
              com.sun.star.reflection.InvocationTargetException
      {
          // Initialise the out paramters - not used at the moment
          aOutParamIndex[0] = new short[0];
          aOutParam[0] = new Object[0];
 
          // Use the following code to set up a ClassLoader if you need one
          ClassLoader cl = null;
          try {
              cl = ClassLoaderFactory.getURLClassLoader( scriptMetaData );
          }
          catch ( java.lang.Exception e )
          {
              // Framework error
              throw new ScriptFrameworkErrorException(
                  e.getMessage(), null,
                  scriptMetaData.getLanguageName(), scriptMetaData.getLanguage(),
                  ScriptFrameworkErrorType.UNKNOWN );
          }
 
          // Load the source of your script using the scriptMetaData object
          scriptMetaData.loadSource();
          String source = scriptMetaData.getSource();
          Any result = null;
 
          // This is where you add the code to execute your script
          // You should pass the xScriptContext variable to the script
          // so that it can access the application API
 
          result = yourlanguageinterpreter.run( source );
 
          // The invoke method should return a com.sun.star.uno.Any object
          // containing the result of the script. This can be created using
          // the com.sun.star.uno.AnyConverter helper class
          if (result == null)
          {
              return new Any(new Type(), null);
          }
          return result;
      }
  }

If the interpreter for YourLanguage supports Java class loading, then the ClassLoaderFactory helper class can be used to load any class or jar files associated with a macro. The ClassLoaderFactory uses the parcel-descriptor.xml file (see Compiling and Deploying Java macros) to discover what class and jar files need to be loaded by the script.

The ScriptMetaData class will load the source code of the macro which can then be passed to the YourLanguage interpreter.

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