public static XScriptContext createContext(
      XModel xModel,
      XScriptInvocationContext xInvocContext,
      XComponentContext xCtxt,
      XMultiComponentFactory xMCF) {
    XScriptContext sc = null;

    try {

      Object xInterface = null;
      XDesktop xDesktop = null;

      xInterface = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xCtxt);
      xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface);
      if (xModel != null) {
        sc = new ScriptContext(xCtxt, xDesktop, xModel, xInvocContext);
      } else {
        sc = new EditorScriptContext(xCtxt, xDesktop);
      }

    } catch (Exception e) {
      LogUtils.DEBUG(LogUtils.getTrace(e));
    }
    return sc;
  }
  ScriptImpl(
      XComponentContext ctx,
      ScriptMetaData metaData,
      XModel xModel,
      XScriptInvocationContext xContext)
      throws com.sun.star.uno.RuntimeException {
    this.metaData = metaData;
    this.m_xContext = ctx;
    this.m_xModel = xModel;
    this.m_xInvocContext = xContext;

    try {
      this.m_xMultiComponentFactory = m_xContext.getServiceManager();
    } catch (Exception e) {
      LogUtils.DEBUG(LogUtils.getTrace(e));
      throw new com.sun.star.uno.RuntimeException(
          "Error constructing  ScriptImpl [beanshell]: " + e.getMessage());
    }

    LogUtils.DEBUG("ScriptImpl [beanshell] script data = " + metaData);
  }
  private ScriptErrorRaisedException processBshException(bsh.EvalError e, String script) {
    LogUtils.DEBUG("Beanshell error RAW message " + e.getMessage());
    String message = e.getMessage();
    int usefulInfoIndex = message.lastIndexOf("\' :");
    int lineNum = e.getErrorLineNumber();

    raiseEditor(lineNum);

    // String stackTrace = te.getScriptStackTrace();  // never seems to have any info??
    if (usefulInfoIndex > -1) {
      message = message.substring(usefulInfoIndex + 2);
    }
    if (e instanceof bsh.TargetError) {
      LogUtils.DEBUG("got instance of  TargetError");
      if (usefulInfoIndex == -1) {
        message = ((bsh.TargetError) e).getTarget().getMessage();
      }
      String wrappedException = "";
      String full = e.toString();
      int index = full.indexOf("Target exception:");
      if (index > -1) {
        String toParse = full.substring(index);
        LogUtils.DEBUG("About to parse " + toParse);
        StringTokenizer tokenizer = new StringTokenizer(full.substring(index), ":");
        if (tokenizer.countTokens() > 2) {
          LogUtils.DEBUG("First token = " + (String) tokenizer.nextElement());
          wrappedException = (String) tokenizer.nextElement();
          LogUtils.DEBUG("wrapped exception = = " + wrappedException);
        }
      }
      ScriptExceptionRaisedException se = new ScriptExceptionRaisedException(message);
      se.lineNum = lineNum;
      se.scriptName = script;
      se.exceptionType = wrappedException;
      se.language = "BeanShell";
      LogUtils.DEBUG("UnCaught Exception error: ");
      LogUtils.DEBUG("\tscript: " + script);
      LogUtils.DEBUG("\tline: " + lineNum);
      LogUtils.DEBUG("\twrapped exception: " + wrappedException);
      LogUtils.DEBUG("\tmessage: " + message);
      return se;
    } else {
      LogUtils.DEBUG("Error or ParseError Exception error: ");
      LogUtils.DEBUG("\tscript: " + script);
      LogUtils.DEBUG("\tline: " + lineNum);
      LogUtils.DEBUG("\tmessage: " + message);
      return new ScriptErrorRaisedException(message, null, script, "BeanShell", lineNum);
    }
  }