How best invoke dita ot cmd from custom oxygen plugin

Post here questions and problems related to oXygen frameworks/document types.
msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Tue May 25, 2021 7:39 pm

Hi,

Oxygen XML Editor 23.0

We have written a custom Oxygen plugin to have menu option to display a form(Conversion Form) by extending WorkspaceAccessPluginExtension. On Form submission, we need to invoke a DITA-OT Plugin to convert xml to dita. I am able to extract the input data from user form and prepare a dita-ot command some thing as below.
'C:\dita-ot-2.4.6\bin\dita' \
'--format=xml2dita' \
'-Dxml2.release=21a' \
'--input=/dev/null' \
'-Dargs.input.dir=D:\xml2dita\input' \
'--output=D:\xml2dita\output' |


We have created and installed required custom DITA-OT plugin as well.

How best we can invoke the dita cmd from java? It would be great if you can suggest any pointer. Any best practices to make the Oxygen plugin robust with changes in Oxygen / DITA OT versions and installation paths?

Thanks,
Samba.

Radu
Posts: 7643
Joined: Fri Jul 09, 2004 5:18 pm

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by Radu » Wed May 26, 2021 7:10 am

Hi Samba,

If you want to run the DITA OT on your own from the Oxygen plugin you will probably need to start a new process from the Java code:
https://docs.oracle.com/javase/8/docs/a ... ocess.html

We also have two API methods for starting a new process:
ro.sync.exml.workspace.api.WorkspaceUtilities.startProcess(String, File, String, boolean)
ro.sync.exml.workspace.api.WorkspaceUtilities.createProcess(ProcessListener, String, File, String, boolean)

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

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Wed May 26, 2021 9:22 am

Thanks Radu!

Tried with below code,

Code: Select all

command = "C\\:\\oxygen\\Oxygen XML Editor 23\\frameworks\\dita\\DITA-OT3.x\\bin\\dita" + " --format=xml2dita" + " -Dxml2.release=21a" + " --input=null" + " -Dargs.input.dir=D:\\xml2dita\\input'" + " --output=D:\\xml2dita\\output'";
	     boolean success = false;
		try 
		{

		 Runtime rt = Runtime.getRuntime();
	         Process pr = rt.exec(command);
	         System.out.println("Gobbler output: " + pr.getInputStream());
	         pr.getInputStream();
	     } 
	     catch(Exception e) 
	     {
	         System.out.println(e.toString());
	         e.printStackTrace();
		}
Error message in the console:

Code: Select all

java.io.IOException: Cannot run program "C\:\oxygen\Oxygen^": CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "C\:\oxygen\Oxygen^": CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
	at java.lang.Runtime.exec(Runtime.java:620)
	at java.lang.Runtime.exec(Runtime.java:450)
	at java.lang.Runtime.exec(Runtime.java:347)
Please suggest clue to escape the space in java code. Some how I don't have luck with caret char(^) as well.
Thanks.

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Wed May 26, 2021 9:30 am

It would be great if you can suggest better way to invoke in built in dita-ot from oxygen plugin. Instead of manually preparing the complete dita cmd as below,

Code: Select all

command = "C\\:\\oxygen\\Oxygen XML Editor 23\\frameworks\\dita\\DITA-OT3.x\\bin\\dita" + " --format=xml2dita" + " -Dxml2.release=21a" + " --input=null" + " -Dargs.input.dir=D:\\xml2dita\\input'" + " --output=D:\\xml2dita\\output'";
Thanks in advance.

Radu
Posts: 7643
Joined: Fri Jul 09, 2004 5:18 pm

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by Radu » Wed May 26, 2021 9:48 am

Hi,

You can find the path to the DITA OT installation in Oxygen using something like this:

Code: Select all

String ditaExecutableCommand = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().expandEditorVariables("${configured.ditaot.dir}", null) + "\\bin\\dita";
You need to surround in double quotes the path to the DITA OT because it may contain spaces (and it does contain spaces in your case).
So something like this:

Code: Select all

command ="\"" + ditaExecutableCommand + "\"" + .....
If you want to read the input and error streams from the started process you can start threads which read them while the process runs:

https://stackoverflow.com/questions/334 ... ntime-exec

Also that --input=null parameter that you are passing does not look right.

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

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Wed May 26, 2021 10:16 am

Thanks Radu!
It's really great feature to get dita-ot path in Oxygen Plugin. But some how, I am still having issue with space character.

Code: Select all

String ditaExecutableCommand = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().expandEditorVariables("${configured.ditaot.dir}", null) + "\\bin\\dita";

System.out.println("ditaExecutableCommand:"+ditaExecutableCommand);
Console Output:

Code: Select all

ditaExecutableCommand: C:\oxygen\Oxygen XML Editor 23\frameworks/dita/DITA-OT3.x\bin\dita
java.io.IOException: Cannot run program ""C:\oxygen\Oxygen": CreateProcess error=193, %1 is not a valid Win32 application
Surprised with mix of forward & backward slashes in the path output i.e ditaExecutableCommand: C:\oxygen\Oxygen XML Editor 23\frameworks/dita/DITA-OT3.x\bin\dita

Could you suggest any clue on the IOException?
Thanks.

Radu
Posts: 7643
Joined: Fri Jul 09, 2004 5:18 pm

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by Radu » Wed May 26, 2021 10:40 am

Hi,

I checked this and the quotes around the path to the executable do not help, it's best to give each parameter, including the path as separate entries in a command line array, also include the entire name of the executable "dita.bat", like for example on my side:

Code: Select all

    List<String> cmdArray = new ArrayList<String>();
    cmdArray.add("D:\\Program Files\\Oxygen XML Author 22.1\\frameworks\\dita\\DITA-OT3.x\\bin\\dita.bat");
    cmdArray.add("-v");
    //TODO add more parameters as separate values in the array
    Runtime.getRuntime().exec(cmdArray.toArray(new String[0]));
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Wed May 26, 2021 1:43 pm

Thanks Radu!
It's working! :D

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Thu May 27, 2021 2:52 pm

Hi,

To the continuation of above thread, I am planning to use startProcess instead of Runtime.getRuntime().exec() to log the process into Oxygen window.

But some how, it's throwing exception as shown below, Found the same issue with 'dita.exe --version' command as well.

Please correct me if Iam wrong. Could you please suggest any pointed to use startProcess method.

String ditaExecutablePath = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().expandEditorVariables("${configured.ditaot.dir}", null) + "\\bin\\" ;
PluginWorkspaceProvider.getPluginWorkspace().startProcess(
"dita conversion",
new File(ditaExecutablePath),
"dita --version",
true);

Error Message:
Started: dita --version
Cannot run program "dita" (in directory "C:\oxygen\Oxygen XML Editor 23\frameworks\dita\DITA-OT3.x\bin"): CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "dita" (in directory "C:\oxygen\Oxygen XML Editor 23\frameworks\dita\DITA-OT3.x\bin"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at ro.sync.cmdline.h.b(Unknown Source)
at ro.sync.exml.workspace.b.c$5.kue(Unknown Source)
at ro.sync.ui.application.lb.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 4 more
Process ended with exit code: -1234567

Thanks.

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Thu May 27, 2021 5:02 pm

Sorry for inconvenience. I got it with below change,
PluginWorkspaceProvider.getPluginWorkspace().startProcess(
"dita conversion",
new File(ditaExecutablePath),
"cmd.exe /c \"dita --version\"",
true);


Thanks.

msambasiva
Posts: 85
Joined: Tue Jul 17, 2018 6:57 am

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by msambasiva » Mon Jun 28, 2021 7:47 pm

Hi,

Found 'The input line is too long.' issue with cmd.exe as I posted in the forum @ topic22985.html .

Could you suggest any clue to overcome this issue? Can we use powershell instead of cmd? If so, any pointer pls?

Thanks,
Samba.

Radu
Posts: 7643
Joined: Fri Jul 09, 2004 5:18 pm

Re: How best invoke dita ot cmd from custom oxygen plugin

Post by Radu » Tue Jun 29, 2021 7:48 am

Hi Samba,

You are starting a process from Java, so I don't think using powershell is an option.
On that DITA OT issue:
https://github.com/dita-ot/dita-ot/issu ... -368855434
I mentioned there is a DITA OT plugin which adds lots of libraries:
dita-ot-3.x/plugins/org.lwdita
This particular plugin is for publishing markdown content as DITA and for publishing DITA content as markdown. If you do not need this functionality you can try to remove the "org.lwdita" plugin, re-run the DITA OT integrator and the command line will be shorter.

As I said the DITA OT bundled with Oxygen has various Java patches to overcome this limitation. So instead of your custom DITA OT distribution you could use ours (which is based on DITA OT 3.6) and install your plugins inside it:
https://www.oxygenxml.com/publishing_engine.html

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

Post Reply