Selected text from Author to xmlfragment

Having trouble installing Oxygen? Got a bug to report? Post it all here.
ronaldo30
Posts: 2
Joined: Sun Oct 24, 2010 9:45 am

Selected text from Author to xmlfragment

Post by ronaldo30 »

Hello,

I have a problem with the selected text after the serialization.

I use for that:

Code: Select all

if(authorAccess.getEditorAccess().hasSelection()) {
AuthorDocumentController documentController = authorAccess.getDocumentController();
AuthorDocumentFragment selectionAsAFragment = documentController.createDocumentFragment(
authorAccess.getEditorAccess().getSelectionStart(), authorAccess.getEditorAccess().getSelectionEnd());
String selectionAsXML = documentController.serializeFragmentToXML(selectionAsAFragment);

//Deletes the selection
authorAccess.getEditorAccess().deleteSelection();

//Process the selectionAsXML fragment, modify it.
//................

//Insert the XML fragment back at caret position.
documentController.insertXMLFragment(selectionAsXML, authorAccess.getEditorAccess().getCaretOffset());
}
I have the selection from the original xml like:

Code: Select all

<container>
<format>text</format>
</container>
After the serializeFragmentToXML the text will be:

String selectionAsXML -> "<container>[space]<format>text</format>[space]</container>"

I have [space] in the text. That isn't correct for me because I want make some changes on text.

How can I get the serialized text like the original text selected without spaces, like ?

String selectionAsXML -> "<container><format>text</format></container>"

Thanks!
Calin
Radu
Posts: 9449
Joined: Fri Jul 09, 2004 5:18 pm

Re: Selected text from Author to xmlfragment

Post by Radu »

Dear Calin,

So the original XML content looks like this, right?

Code: Select all

<container>
<format>text</format>
</container>
That line break that you have before the <format> element is still considered to be a whitespace and when the XML is loaded in the Author editing mode it gets converted to a single space.
Does the DTD/XML Schema associated to the XML specify that the <container> element has element-only content (without the possibility to have text between the <format> elements for example)? If it would, then Oxygen would remove that extra space when presenting the document in the Author editing mode.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
ronaldo30
Posts: 2
Joined: Sun Oct 24, 2010 9:45 am

Re: Selected text from Author to xmlfragment

Post by ronaldo30 »

Thank you Radu for very rapid answer.
I knew your solution.

Unfortunately <container> may contain PCDATA. That xml fragment is like:
1.

Code: Select all


<div>text1 <b>text2 text3</b> text4</div>
2.

Code: Select all


<div>
text1 <b>text2 text3</b> text4
</div>
So, after documentController.serializeFragmentToXML the text will be:
1. -> <div>text1 <b>text2 text3</b> text4</div>
2. -> <div>[space]text1 <b>text2 text3</b> text4[space]</div>

The resulted texts are different from the same original selection.

A workaround can be to get the fragment from original xml but I can't get the selected nodes into original xml.
Maybe with: java.lang.String getXPathExpression(int offset) get the xpath from author mode and will apply that to the original xml. I don't know.

What do you think is possible a workaround for that ?

Thanks!
Calin
Radu
Posts: 9449
Joined: Fri Jul 09, 2004 5:18 pm

Re: Selected text from Author to xmlfragment

Post by Radu »

Hi Calin,

Just to see that I understand your example:

So in the first case there is no space or line break between the <div> and the text1. And in the second case you have a line break between them.
When the edited content the Author mode is obtained by normalizing all spaces so that line break will be converted to a space.
There is no possibility for you to obtain the XML content exactly as it was from the Author API other than setting the <div> element as space preserve in the CSS, something like:

Code: Select all

div  {
white-space:pre;
}
I'm afraid I still do not understand your original use case, why you need the serialization of the AuthorDocumentFragment to be precisely what the XML contained and not an equivalent of that.

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