Page 1 of 1

Plugin - access files bundled with plugin

Posted: Tue Nov 15, 2022 1:39 am
by Krille
Hi!

I'm writing a plugin for importing files from classical text editor to (TEI-) XML. In the plugin, I'd like to access an XSLT stylesheet bundled in the plugin. It is in the plugins folder xsl/tei.xsl.

I'd like to pass this stylesheet to a Saxon transformer the plugin instantiates. But I don't know how to get the file path from my java extension class. Here is what I've tried:

Code: Select all

...
import java.nio.file.Paths;
...

public class CTEImportPluginExtension implements WorkspaceAccessPluginExtension {

    public static String getExtensionRoot() {
	// get the URL of the jar file
	File jarFile = new File(WorkspaceAccessPluginExtension.class.getProtectionDomain().getCodeSource().getLocation().getPath());
	String path = jarFile.getParent();
	return path;
    }

    private static String xsl = Paths.get(getExtensionRoot(), "xsl", "tei.xsl").toString();

  ...

}


The xsl variable points to /opt/oxygenxml/Oxygen%20XML%20Editor%2024/lib/xsl/tei.xsl, but I would expect it to be /opt/oxygenxml/Oxygen%20XML%20Editor%2024/plugins/oxygen-cte-importer/xsl/tei.xsl , as I have installed the plugin directory oxygen-cte-importer by sym-linking it to OXYGEN_INSTALL_DIR/plugin.

How can I access the stylesheet?

Kind regards,
Christian

Re: Plugin - access files bundled with plugin

Posted: Tue Nov 15, 2022 1:54 pm
by Cosmin Duna
Hi Christian,

For obtaining the plugin directory you should get the instance of the class that extends "ro.sync.exml.plugin.Plugin" and from this instance, get the "PluginDescriptor" using the "getDescriptor" method. From this object you can find the plugin's directory using the "ro.sync.exml.plugin.PluginDescriptor.getBaseDir()" method.
For example:

Code: Select all

File baseDir = CTEImportPlugin.getInstance().getDescriptor().getBaseDir();
File teiXsl = new File (baseDir, "xsl/tei.xsl");
Also, make sure that the "xsl" folder will be packed into the jar file of the add-on. For this you should add the following fileset element into the "assembly.xml" file:

Code: Select all

     <fileSet>
	      <directory>xsl</directory>
	      <outputDirectory>xsl</outputDirectory>
	      <includes>
	        <include>**/*</include>
	      </includes>
    </fileSet>
Best regards,
Cosmin

Re: Plugin - access files bundled with plugin

Posted: Wed Nov 16, 2022 1:34 am
by Krille
Hi Cosmin!

Great, that works. Thanks.

I will post a link to my source code as soon as it's on a public repo.

Kind regards,
Christian