AddHighlight with style

Post here questions and problems related to oXygen frameworks/document types.
feherbbj
Posts: 5
Joined: Mon Mar 09, 2020 2:02 pm

AddHighlight with style

Post by feherbbj »

Hi,

A Plugin has been developed which marks the content of the XML based on a terminology termbase and some rule.
Non-persistent highlighter has been used to mark the content. At first glance it worked pretty well, but after I tried several files I found that if the file reference any styles, the marking slightly shifted.

So this is the file without formatting:
oxygen_1.PNG
oxygen_1.PNG (101.32 KiB) Viewed 1019 times
And this is the file with some formatting
oxygen_style.PNG
oxygen_style.PNG (89.45 KiB) Viewed 1019 times
If the CSS file content is empty, I get the same results.

I simply use this code.

Code: Select all

AuthorHighlighter highlighter = authorPageAccess.getHighlighter();
ColorHighlightPainter painter = new ColorHighlightPainter(withColor, 20, 10);
highlighter.addHighlight(startOffset , startOffset + length, painter, null);
Can you help why this happens and how can I solve this?
Thank you in advance

Best regards,
Balazs
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: AddHighlight with style

Post by Radu »

Hi Balazs,

This looks similar to what our terminology checker add-on does:
https://www.oxygenxml.com/doc/versions/ ... addon.html

Can you give me a small overview over how you compute those "startOffset" and "length" parameters that you use to add highlights?
When XML content is loaded in the Author visual editing mode and the XML has a CSS and schema associated usually Oxygen trims consecutive white spaces, for example if the original XML text was like this:

Code: Select all

<p>some text
          second line
Oxygen will remove the consecutive spaces in the indentation so that in the Author mode the text content is like this:

Code: Select all

some text second line
When saving the XML content back Oxygen might add again line breaks and indentation.
If the XML does not have a CSS or schema associated, Oxygen considers that all XML elements inside it are space preserve and leaves the indentation and line breaks exactly as they were.

Our Terminology Checker add-on uses this API "ro.sync.ecss.extensions.api.AuthorDocumentController.getTextContentIterator(int, int)" to retrieve text segments exactly as they are present in the Author visual editing mode and check that text instead of checking the initial XML text as it was serialized on disk.

I think another approach would be to do what this free Oxygen add-on which integrates with LanguageTools does:
https://github.com/danielnaber/oxygen-l ... ector.java

to use the "AuthorDocumentController.serializeFragmentToXML" API to serialize the Author nodes to XML, a serialization which does not add indents and extra spaces so it maps well to the content in the Author page.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
feherbbj
Posts: 5
Joined: Mon Mar 09, 2020 2:02 pm

Re: AddHighlight with style

Post by feherbbj »

My internal document representation contains key-value pairs, where the key is the XPath to the node and the value is the node's content.
When the user interacts with my program I try to get the AuthorNode by the XPath, and get it start offset by:

Code: Select all

AuthorNode node = ...; // get node by current selection or by XPath
int temp = node.getStartOffset();
int startOffset = temp + item.startIndex;
The use case is that, some user select a word and want to highlight it ( or replace with another one). The item.startIndex is the word's start index inside the node's content that I want to highlight (I get the content by node.getTextContent()). The length is the word length.

So if I understand issue well, the problem is that the content that I receive from (node.getTextContent()) contains multiple consecutive whitespaces, so it will be trimmed by oxygen if CSS is loaded, am I right?

If that's the case:
  • How can I determine if a CSS is loaded or not?
  • Does "node.getStartOffset()" return the valid or "miscounted" location, if any previous nodes contains consecutive whitespaces? (So should I calculate all the consecutive whitespace count before the actual word index, or is it enough to calculate only for the current node's content)
adrian_sorop
Posts: 73
Joined: Wed Jun 22, 2016 2:48 pm

Re: AddHighlight with style

Post by adrian_sorop »

Hi,
As Radu stated in his previous post, we encourage developers to use the TextContentIterator API and the TextContext API.
The usage is something like:

Code: Select all

TextContentIterator textContentIterator = authorDocumentController.getTextContentIterator(node.getStartOffset(), node.getEndOffset());
while (textContentIterator.hasNext()) {
	TextContext textContent = textContentIterator.next();
	// now you can use the textContent to get the text, it's offsets and others
}
Regards,
Adrian S
Adrian Sorop
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply