Manipulating XML in text mode

Post here questions and problems related to oXygen frameworks/document types.
jakubsimek
Posts: 11
Joined: Fri Oct 04, 2019 11:26 pm

Manipulating XML in text mode

Post by jakubsimek » Fri Nov 06, 2020 12:04 am

Hi,

I am trying to figure out how I could write a plugin action for the text mode which would manipulate some elements or attributes. As far as I can see, the sample plugins in the SDK only demonstrate string manipulation for the text mode (e.g. capitalizing).

The "TextDocumentController" which can be used on "WSXMLTextEditorPage" only has a method for inserting an XML fragment.

Suppose I would like to insert an attribute on a certain element which is already present in the document. Could i use the "evaluateXPath" method on "WSXMLTextEditorPage" to return the element I want to manipulate? The method returns an array of java.lang.Object. Does this mean that in case the items returned by "evaluateXPath" are XML element nodes, I could then manipulate them "in place" as instances of something like "org.w3c.dom.Element"?

Would something like this be the way to go or are there other ways in the API to manipulate XML nodes in text mode?

I am a bloody beginner in the Oxygen SDK, so sorry if I am asking something which is perhaps obvious.

Best wishes,
Jakub

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

Re: Manipulating XML in text mode

Post by Radu » Fri Nov 06, 2020 8:47 am

Hi Jakub,

This is a good question, our API on the Text editing mode side is more primitive than the API for the Author visual editing mode. Partly this is because in the Text mode you might not have wellformed XML, so we cannot create a nodes structure over it.
Basically the Text page is a Swing JTextArea, and you can gain access to its internal document ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.getDocument() if you want to perform insertions and deletions other than what our API allows.
This API gives you ranges of elements ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage.findElementsByXPath(String). Each range has start line/column and end line/column information. After this you can use our API ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.getOffsetOfLineStart(int) to convert line/column information to an offset in the document and thus obtain the offset range beween which an element exists, in this way you can retrieve the entire element content in a string, maybe manipulate it and then insert it back in the document.

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

jakubsimek
Posts: 11
Joined: Fri Oct 04, 2019 11:26 pm

Re: Manipulating XML in text mode

Post by jakubsimek » Fri Nov 06, 2020 9:36 am

Hi Radu,

Thanks a lot for this explanation. May I ask back about the evaluateXPath() method on WSXMLTextEditorPage? What kind of objects are being returned by this method if I e.g. address element nodes by the XPath expression?

Best,
Jakub

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

Re: Manipulating XML in text mode

Post by Radu » Fri Nov 06, 2020 9:50 am

Hi Jakub,

The "evaluateXPath" should return an array of DOM "org.w3c.dom.Node". You can use it more to interrogate if certain XML elements are found and what they contain. Making changes to the DOM nodes will not update the XML content.

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

jakubsimek
Posts: 11
Joined: Fri Oct 04, 2019 11:26 pm

Re: Manipulating XML in text mode

Post by jakubsimek » Fri Nov 06, 2020 11:03 am

OK, thank you very much for explaining this.
Best,
Jakub

Post Reply