Javascript Custom Action using XLST

Oxygen general issues.
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Javascript Custom Action using XLST

Post by mwiechec »

I am trying to implement an author action that will replace all occurrences of an element attribute name with a new name and retain it's value, e.g. element(attr1="val1") with element(attr2="val1"). I was able to create an XSLT transform and execute it via ro.sync.ecss.extensions.commons.operations.XSLTOperation
<action>
<field name="id">
<String>update.XML</String>
</field>
...
<field name="actionModes">
<actionMode-array>
<actionMode>
<field name="xpathCondition">
<String>//*[@id]</String>
</field>
<field name="argValues">
<serializableOrderedMap>
<entry>
<String>action</String>
<String>Replace</String>
</entry>
<entry>
<String>script</String>
<String>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
&lt;xsl:template match="assembly[@id]">
&lt;assembly>
&lt;xsl:attribute name="uid">&lt;xsl:value-of select="@id"/>&lt;/xsl:attribute>&lt;xsl:apply-templates>&lt;/xsl:apply-templates>
&lt;/assembly>
&lt;/xsl:template>
&lt;xsl:template match="@* | node()">
&lt;xsl:copy>
&lt;xsl:apply-templates select="@* | node()"/>
&lt;/xsl:copy>
&lt;/xsl:template>
&lt;/xsl:stylesheet></String>
</entry>
<entry>
<String>sourceLocation</String>
<String>/xmldoc</String>
</entry>
<entry>
<String>targetLocation</String>
<String>/xmldoc</String>
</entry>
</serializableOrderedMap>
</field>
<field name="operationID">
<String>ro.sync.ecss.extensions.commons.operations.XSLTOperation</String>
</field>
</actionMode>
</actionMode-array>
</field>
<field name="enabledInReadOnlyContext">
<Boolean>false</Boolean>
</field>
</action>

This works fine in the oxygen editor but I wanted to implement in Webauthor using a javascript custom action, so I have more control. I tried

UpdateXMLAction.prototype.actionPerformed = function(callback) {
this.editor.getActionsManager().invokeOperation(
'ro.sync.ecss.extensions.commons.operations.XSLTOperation', {
sourceLocation: '/xmldoc',
targetLocation: '/xmldoc',
script: UpdateXMLAction.xslt,
action: 'Replace'
}, callback);
callback && callback();
};

UpdateXMLAction.xslt = `<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="assembly[@id]">
<assembly>
<xsl:attribute name="uid"><xsl:value-of select="@id"/></xsl:attribute><xsl:apply-templates></xsl:apply-templates>
</assembly>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>`;

I can trigger the action but it does nothing and returns no error. Any help would be appreciated.
mihai_coanda
Posts: 78
Joined: Wed Jul 20, 2016 8:22 am

Re: Javascript Custom Action using XLST

Post by mihai_coanda »

Hello,

Due to the fact that the XSLTOperation is very powerful and could cause security issues, only actions defined in the framework can use it. You should see a log entry informing you of that.
You should define an action in the framework that uses the operation for it to be allowed and call it from JS.
Is there something preventing you from using this scenario?

Regards,
Michael
Michael

https://www.oxygenxml.com
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

Thank you, I will try to use this scenario. However I noticed some unexpected behavior.

1. The activation path works well in oxygen Editor enabling and disabling the associated toolbar button. However in Webauthor the toolbar button state is always enabled. Clicking on the button when the activation path is false gives a message "The action: 'xxx' is not available in the current location." I am assuming this how it works in Webauthor, but is there a way in the javascript framework to remove or disable the button based on the activation path?

2. The ro.sync.ecss.extensions.commons.operations.XSLTOperation behaves differently in Webauthor. In the Editor when I execute the transform
it correctly retains my tracked changes. In Webauthor it removes all the tracked changes. Interestingly the Webauthor Test Server works correctly.

3. I may have been able to use the ro.sync.ecss.extensions.commons.operations.ChangeAttributesOperation to solve my issue. However I could not figure out if the "values" setting can be set to the target element attribute's value. Is this possible? Maybe an XPath to the current attribute could be used.

Thank You,
Mark
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

I should have mentioned the versions XML Editor 23.0, build 2020111805, oxygen Web Author Test Server, 23.0.0, oXygen XML Web Author 23.0, build 2020122201/2020122201.
mihai_coanda
Posts: 78
Joined: Wed Jul 20, 2016 8:22 am

Re: Javascript Custom Action using XLST

Post by mihai_coanda »

Hello,

Regarding first issue:

The XPath actions activation does not work on Web Author so you will have to override the isEnabled method of the action client-side to check whether it is in a context in which it should be active:

ActionsManager.getActionById('your-action-id').isEnabled = function() {
// activation logic here
};

Useful API to determine the selection context:
https://www.oxygenxml.com/maven/com/oxy ... ditor.html
https://www.oxygenxml.com/maven/com/oxy ... on__anchor
https://www.oxygenxml.com/maven/com/oxy ... ction.html
https://www.oxygenxml.com/maven/com/oxy ... .Node.html

Every time the action activation status changes you will also have to call the sync.api.ActionsManager.efreshActionsStatus[1] method so the toolbar updates the action's status.

1. https://www.oxygenxml.com/maven/com/oxy ... us__anchor



Regarding the second issue:
Could you send us the minimum resources needed to reproduce the issue you are experiencing at support@oxygenxml.com with a detailed scenario of what you are experiencing?

Regarding the third issue:
The operation is meant to change multiple attributes of a target element, identified by the XPath and the values parameter are the values of the attributes mentioned on as the attributeNames parameter.

Regards,
Michael
Michael

https://www.oxygenxml.com
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

Thanks for your quick response.

1. I will implement your recommendations for the activation path issue. I do have more one more question. For this particular action the activation path is "//*[@id]". I want the action to be active if the document contains any elements that have an "id" attribute. I do not know how to check for that condition in the javascript framework. Is this possible?

2. I am attaching a zip file with a bare bones framework, css, and xml file. The xml contains contains tracked changes. In the image you can see the results, the action works the attribute name is changed but the tracked changes are also removed.
test.zip
(2.51 KiB) Downloaded 371 times
authoraction.jpg
authoraction.jpg (56.18 KiB) Viewed 3868 times
3. So am I to understand that the value field for ChangeAttributesOperation can only be a constant string value?

Thanks,
Mark
cristi_talau
Posts: 495
Joined: Thu Sep 04, 2014 4:22 pm

Re: Javascript Custom Action using XLST

Post by cristi_talau »

Hello,

1. We have DOM-like API that you can use to determine if there is node with the ID attribute: https://www.oxygenxml.com/maven/com/oxy ... nt__anchor .

You can use the code below:

Code: Select all

function anyHasId(node) { console.log(node, node.type);
  if (node.nodeType === Node.ELEMENT_NODE && node.getAttributeNode('id') != null) {
    return true;
  }
  for (let child of node.childNodes) {
    if (anyHasId(child)) {
      return true;
    }
  } 
  return false;
}

anyHasId(editor.getEditingSupport().getDocument());
2. I could not reproduce the problem using your framework. Can you try to reproduce the problem with a Web Author kit without any non-default plugins?

3. It seems that the ChangeAttributeOperation is not suitable for your usecase, which is, if I understand correctly that you want to rename an attribute.

Currently I can see several alternatives:
  • Define an action in the framework and use XSLTOperation. You mentioned that JS code would give you more flexibility. Some more detailes on that might help suggesting a better alternative.
  • If you want to simply rename the attribute name in many documents, you can use refactoring actions in Oxygen XML Editor .
  • Create a custom Java AuthorOperation that extends XSLTOperation and is annotated with @WebappRestSafe, so that it can be invoked from JS.. Note: To really make the operation safe, you should restrict the XLST script that JS code can provide.
Best,
Cristian
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

Cristian,

1. I will try the code you suggested, thanks.

2. I removed all plugins except for WebDAV Connector v23.0 and WebDAV Server v23.0 and restarted the server. I still get the issue. Is there something else you want me to try or send you? I did try inserting just some generic processing instructions and they do not get removed.
authoraction2.jpg
authoraction2.jpg (49.24 KiB) Viewed 3824 times
I also tried a previous release of oxygen 22.1 and the same results.

3. Thanks for all the suggestions. I think if we can fix issue #2 then the XSLT approach will work.

Thanks,
Mark
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hi,

We finally reproduced the problem you reported. If the track changes state is on when you invoke the XSLTOperation then all the track changes from the affected nodes are accepted. We will add a fix for this in a future version. Until then, we created for you a jar containing an operation that extends XSLTOperation and adds a fix for the change tracking issue:
xsltOperationWithTrackCganesFix.zip
(1.2 KiB) Downloaded 391 times
The operation is ro.sync.ecss.extensions.xslt.operation.XSLTOperationWithChangeTrackingFix.

What you have to do is to add the jar from the zip in your document type classpath [1] and then replace the XSLTOperation you used in your action with XSLTOperationWithChangeTrackingFix. Do not forget to insert again the same arguments values (the arguments are cleared when the operation is changed).

[1] https://www.oxygenxml.com/doc/versions/ ... h-tab.html

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

I implemented the fix xsltOperationWithTrackCganesFix.zip and it did not solve the problem. So I did some more testing to try and understand why we are seeing different results. I tried a fresh install of WebAuthor for Window 64 and it worked. I then tried a fresh install of WebAuthor for Linux on a local Ubuntu pc and it worked also. The production server is a All Platforms Distribution running on Linux with java version "1.8.0_161" Java(TM) SE Runtime Environment (build 1.8.0_161-b12) and Apache Tomcat/7.0.104. I do not know if there is something different about this distribution or our environment. Can you please test it specifically with the All platform distribution and let me know what I should check fo in my envirionment.
Here is a summary of my results.
Results.jpg
Results.jpg (62.37 KiB) Viewed 3740 times
Thanks,
Mark
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hello,

Did you tested with different Web Author versions? What are the versions you used for your tests?

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

The latest version 23.0.0.1 - Running oXygen XML Web Author 23.0, build 2020122201/2020122201. My last test where all on the latest version.
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hi,

I tried again to test on a Web Author All Platforms Distribution install, by adding both actions on the toolbar: one that uses XSLTOperation and one that uses XLTOPerationWithChangeTrackingFix (both with suspendTrackChanges true). With Change Tracking ON my results are:
- when using XSLTOperation the tracked changes are removed
- when using XLTOPerationWithChangeTrackingFix the tracked changes are not removed.

So, please make sure that you remove the old framework before adding a new one on Web Author admin page. Also, please make sure that you restart the server after installing a new framework.
I think the best and simple way to make these tests is to add all the actions you want to test in your framework (add them on the toolbar). In this way you can test them together, without the need to add a new framework and restart the server.

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
mwiechec
Posts: 17
Joined: Mon Nov 19, 2018 10:49 pm

Re: Javascript Custom Action using XLST

Post by mwiechec »

I finally found the problem. I installed a fresh all platforms distribution and noticed that it worked. I determined the problem was our options.xml file. Specifically the entry
<entry>
<String>track.changes.initial.state</String>
<Integer>1</Integer>
</entry>
After removing this entry your fix now works. We use this setting because our users always track changes and it is very convenient when the editor opens with it already turned on.

Thanks,
Mark
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hello,

Thank you for the details, we managed to reproduce this problem. We will search for a solution and we will let you know when we will have a conclusion.

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hello,

We managed to find a fix for the problem you reported. There is no time to include the fix in the next release (version 23.1 will be released soon) but we will try to include it in the next maintenance build. We will update this thread when it will be available.

In the meantime, if you want, you can remove the option that imposes the "always ON" track changes state and replace it with a plugin [1] that imposes the track changes ON state [2] when the document is loaded [3] (by registering a listener on editing session about to be started event).
Please let us know if you need more information.

[1] https://www.oxygenxml.com/doc/versions/ ... ugins.html
[2] https://www.oxygenxml.com/InstData/Edit ... kChanges--
[3] https://www.oxygenxml.com/InstData/Edit ... .util.Map-

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Javascript Custom Action using XLST

Post by mihaela »

Hello,

I wanted to let you know that we just released the 23.1.1 version of Web Author and it contains the fix for the problem you reported here.
Here is the "What's New" list for this new version:
https://www.oxygenxml.com/xml_web_autho ... 3.1.1.html

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
Post Reply