How to use latest Rhino?

Post here questions and problems related to oXygen frameworks/document types.
msklvsk
Posts: 9

How to use latest Rhino?

Fri May 06, 2016 6:28 pm

Oxygen 18.0 uses "Rhino 1.7 release 3 2011 05 09" which is 5 years old and missing all these features&fixes, including ES5 support. What is the best way to run my .js plugin on the latest version of Rhino?
Radu
Posts: 5129

Re: How to use latest Rhino?

Mon May 09, 2016 10:50 am

Hi,

We'll look into updating the Rhino library which comes bundled for Oxygen 18.1.
An Oxygen plugin can also contribute to Oxygen extra JAR libraries:

https://www.oxygenxml.com/doc/versions/17.1/ug-editor/index.html#concepts/oxygen-plugin.html

with various scopes.
So possibly you could bundle your own "js.jar" with the plugin and refer to it from the plugin.xml like:

Code: Select all

<library name="your_js.jar" scope="globalHighPriority"/>


which might be enough to make your JAR have more high priority than ours.
But I'm not sure if this will work or not, it's possible that the API in the latest js.jar has changed and our Java code needs to be updated in order to be used with the latest library.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
msklvsk
Posts: 9

Re: How to use latest Rhino?

Thu May 12, 2016 1:42 pm

Thanks for the instructions. I've tried to reference new Rhino with scope="globalHighPriority", but Oxygen still used Rhino 1.7.3. Maybe I missed something.
Anyway, I might create a java workspace extension and use whatever scripting engine I want, e.g. Nashorn, which is the successor of Rhino.
Radu
Posts: 5129

Re: How to use latest Rhino?

Thu May 12, 2016 1:53 pm

Hi,

Indeed, you can create your own Java implementation of the Workspace Access plugin extension which delegates to the scripting engine of your choice.
Just to get you started, our implementation looks like this:

Code: Select all

public class WorkspaceAccessOverJSPluginExtension implements WorkspaceAccessPluginExtension{
    /**
   *  Logger for logging.
   */
  private static final Logger logger = Logger.getLogger(WorkspaceAccessOverJSPluginExtension.class.getName());

  /**
   * The JS File
   */
  private URL jsFileURL;

  /**
   * Plugin workspace access.
   */
  private StandalonePluginWorkspace pluginWorkspaceAccess;

  /**
   * Constructor.
   * @param jsFileURL URL pointing to the Javascript file.
   */
  public WorkspaceAccessOverJSPluginExtension(URL jsFileURL) {
    this.jsFileURL = jsFileURL;}

  /**
   * @see ro.sync.exml.plugin.workspace.WorkspaceAccessPluginExtension#applicationStarted(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)
   */
  @Override
  public void applicationStarted(final StandalonePluginWorkspace pluginWorkspaceAccess) {
    this.pluginWorkspaceAccess = pluginWorkspaceAccess;
    callFunction(pluginWorkspaceAccess, "applicationStarted");
  }

  /**
   * Constructor.
   *
   * @param pluginWorkspaceAccess The plugin workspace access.
   * @param functionName The function name
   * @return The function return value.
   */
  public Object callFunction(StandalonePluginWorkspace pluginWorkspaceAccess, String functionName) {
    //Use it
    org.mozilla.javascript.Context cx = org.mozilla.javascript.Context.enter();
    try{
      org.mozilla.javascript.Scriptable globalScope = cx.initStandardObjects();
      //EXM-36130 Global field which points to Javascript dir URL.
      Object wrappedDispatcher =  org.mozilla.javascript.Context.javaToJS(URLUtil.getCanonicalURL(URLUtil.getParentURL(jsFileURL)), globalScope);
      org.mozilla.javascript.ScriptableObject.defineProperty(globalScope, "jsDirURL", wrappedDispatcher, org.mozilla.javascript.ScriptableObject.CONST);

      // Now evaluate the string we've collected. We'll ignore the result.
      cx.evaluateString(globalScope, IOUtil.readURL(jsFileURL, "UTF8").toString(), jsFileURL.toString(), 1, null);
     
      Object fObj = globalScope.get(functionName, globalScope);
      org.mozilla.javascript.Function f = (org.mozilla.javascript.Function)fObj;
      return f.call(cx, globalScope, globalScope, new Object[]{pluginWorkspaceAccess});
    } catch(Exception ex){
      logger.error(ex, ex);
      pluginWorkspaceAccess.showErrorMessage(ex.getMessage());
    } finally{
      org.mozilla.javascript.Context.exit();
    }
    return null;
  }

  /**
   * @see ro.sync.exml.plugin.workspace.WorkspaceAccessPluginExtension#applicationClosing()
   */
  @Override
  public boolean applicationClosing() {
    Object ret = callFunction(pluginWorkspaceAccess, "applicationClosing");
    if(ret instanceof Boolean){
      return (Boolean) ret;
    }
    //Alow close operation to continue
    return true;
  }
}


Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
msklvsk
Posts: 9

Re: How to use latest Rhino?

Thu May 12, 2016 4:17 pm

Thanks!
Outstanding support.
Radu
Posts: 5129

Re: How to use latest Rhino?

Fri Oct 28, 2016 11:33 am

Hi,

We released Oxygen 18.1 a couple of weeks ago and we updated in it the Rhyno library to version 1.7.7.1.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “SDK-API, Frameworks - Document Types”

Who is online

Users browsing this forum: No registered users and 1 guest