insertXMLFragment w/ Processing Instructions

Post here questions and problems related to oXygen frameworks/document types.
bpopp
Posts: 37

insertXMLFragment w/ Processing Instructions

Fri Jan 26, 2018 9:15 pm

Hopefully someone can help me. I'm trying to add a processing instruction using an AuthorOperation around the current selection. My code looks like:

Code: Select all

         
int selStart = authorAccess.getEditorAccess().getSelectionStart();
int selEnd = authorAccess.getEditorAccess().getSelectionEnd();
           
if ( selEnd != selStart )
{
   System.out.println("Adding processing instruction.");
   //String fragmentStart = "<?oxy_insert_start?>";
   //authorAccess.getDocumentController().insertXMLFragment(fragmentStart, selStart);

   //String fragmentEnd = "<?oxy_insert_end?>";
   //authorAccess.getDocumentController().insertXMLFragment(fragmentEnd, selEnd);

              
   String fragment = "<?oxy_insert_start?><?oxy_insert_end?>";
   authorAccess.getDocumentController().surroundInFragment(fragment, selStart, selEnd );
}


As you can see, I've tried adding the processing instructions with "insertXMLFragment" and with "surroundInFragment". Individually, the end processing instruction gets added, but not the beginning and an Author message is displayed:

[Document]:Found insert processing instruction without an end.
[Document]:Ignoring end processing instruction for insert change tracking which does not have a start.


The surroundInFragment code doesn't generate any errors, but no PI's are added.
Radu
Posts: 5459

Re: insertXMLFragment w/ Processing Instructions

Mon Jan 29, 2018 12:44 pm

Hi,

Change tracking processing instructions are special and they should never be added as plain XML content.
Could you tell me more precisely what you are trying to achieve.
If you want to make changes via the API and have them recorded although change tracking is disabled, you can use the APIs ro.sync.ecss.extensions.api.ChangeTrackingController.toggleTrackChanges() and ChangeTrackingController.isTrackingChanges().
For example looking at your code you can first save the selected content to an AuthorDocumentFragment AuthorDocumentController.createDocumentFragment(int, int), then turn off change tracking using the API and delete the selected content (AuthorDocumentController.delete(int, int)). Then enable change tracking using the API and insert the fragment back in the document (AuthorDocumentController.insertFragment(int, AuthorDocumentFragment)).

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

Re: insertXMLFragment w/ Processing Instructions

Mon Jan 29, 2018 6:48 pm

Actually, all I'm trying to do is add change-tracking processing instructions surrounding the current expression. It's admittedly a little goofy adding "insert" processing instructions around changes , but for our particular use case, we don't need to know exactly what was changed (ie. change tracking). Change tracking would work, except we've had trouble rendering change tracking information reliably. I apply change bars between start and end tags, and inevitably our writers end up with change bars that wrap hundreds of pages.

Ultimately we just need to know where the change starts and ends and we use a custom element to describe each change. I was hoping to simplify the process of adding this information with a simple button.

I was able to wrap text in custom processing instructions, so my issue is clearly surrounding the protected oxy processing instructions.
Radu
Posts: 5459

Re: insertXMLFragment w/ Processing Instructions

Tue Jan 30, 2018 2:45 pm

Hi,

Doing this will not work:

Code: Select all

   //String fragmentStart = "<?oxy_insert_start?>";
   //authorAccess.getDocumentController().insertXMLFragment(fragmentStart, selStart);

   //String fragmentEnd = "<?oxy_insert_end?>";
   //authorAccess.getDocumentController().insertXMLFragment(fragmentEnd, selEnd);


because you are inserting the start and end processing instructions in two separate stages.
And this approach:

Code: Select all

   String fragment = "<?oxy_insert_start?><?oxy_insert_end?>";
   authorAccess.getDocumentController().surroundInFragment(fragment, selStart, selEnd );


will not work because the "surroundInFragment" was thought to usually takes an XML element as a fragment, otherwise it interprets those as two consecutive processing instructions, not as one which should be placed at the start of the selection, and one at the end.

I would recommend you try this approach:

Code: Select all

    if(authorAccess.getEditorAccess().hasSelection()) {
        int selStart = authorAccess.getEditorAccess().getSelectionStart();
        int selEnd = authorAccess.getEditorAccess().getSelectionEnd();
        AuthorDocumentFragment selectedContent = authorAccess.getDocumentController().createDocumentFragment(selStart, selEnd - 1);
        String selContentAsXML = authorAccess.getDocumentController().serializeFragmentToXML(selectedContent);
        //Delete selected content
        authorAccess.getDocumentController().delete(selStart, selEnd - 1);
        String toInsert = "<?oxy_insert_start?>" + selContentAsXML + "<?oxy_insert_end?>";
        authorAccess.getDocumentController().insertXMLFragment(toInsert, authorAccess.getEditorAccess().getCaretOffset());
    }


This will work only if the end user has disabled change tracking. If they have change tracking enabled you can use our APi to turn it off, use the API to insert the content and then turn it on again.

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

Return to “SDK-API, Frameworks - Document Types”

Who is online

Users browsing this forum: No registered users and 0 guests