Programmatic access to change tracking

Having trouble installing Oxygen? Got a bug to report? Post it all here.
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Programmatic access to change tracking

Post by sijomon »

Hi, me again,

I have a problem concerning change tracking and failed XML inserts.

A requirement is that we turn change tracking on for all documents edited in our framework. I use an ExtensionBundle and the createAuthorExtensionStateListener() to provide a listener that flips change tracking to on whenever the activated() callback method is invoked, and flip it back off again in the deactivated() method. This works great; however the fact change tracking is on causes some problems in some of the framework's Operations.

I have an Operation that expands the currently selected text into an XML element. The selected text is then deleted and the XML fragment inserted where the text used to be. Incidentaly I can't use the surroundInFragment() method as I need to control where in the XML fragment the selected text occurs. This is fine unless the XML fragement is invalid (I have relativley little control over the exact XML used as it comes from a 3rd party tool). When it is invalid I get an AuthorOperationException thrown, which I catch and then roll back the insert. I do this by re-inserting the orignal text. This would be fine if it weren't for the change tracking which flags this failure case as 2 changes, one being the original text as deleted, and the second being that same text re-inserted. Ideally in the case of a failure I would like no chnages recorded. To my mind this means getting access to the change tracking framework and removing the last two changes, or supressing change tracking during the opertaion and applying all chnages if and only if the insert succeeds.

Have you any suggestions about how I might address this problem?

Simon.
Radu
Posts: 9439
Joined: Fri Jul 09, 2004 5:18 pm

Re: Programmatic access to change tracking

Post by Radu »

Hello Simon,

You have several ways in which to address this problem:

1) Check the XML fragment is valid before inserting it.

Code: Select all


 XMLReader xmlReader = authorAccess.getUtilAccess().newNonValidatingXMLReader();
try {
xmlReader.parse(new InputSource(new StringReader(xmlFragment)));
} catch (IOException e) {
//Should not happen
e.printStackTrace();
} catch (SAXException e) {
//The XML was not wellformed
e.printStackTrace();
}
2) Copy the selected text without deleting it, try to insert the XML fragment. If the insert fails, nothing was performed, else delete the original selected text.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
sijomon
Posts: 83
Joined: Wed May 20, 2009 1:18 pm

Re: Programmatic access to change tracking

Post by sijomon »

Hi,

Thanks for the response, helpful and quick, as usual :)

Unfortunatly the first solution you suggest will valiadte the fragement as valid in itself but not whether it will be valid when inserted into the target position in the document.

Your second solution is spot on, thanks, I don't know why I didn't think of this, coding blindness I reckon ;)

many thanks,

Simon.
Post Reply