Hi Mike,
If pasted text is " hello [1;2]" i want to change that into " hello <xref linkend="id.1"/><xref linkend="id.2"/>"
The architecture of the
AuthorDocumentFragment can be described better by the image located here:
http://www.oxygenxml.com/InstData/Edito ... gment.html
Basically the
AuthorNode's usually correspond to XML Elements (they are instances of
AuthorElement.
The method which allows you to manipulate the text is this one:
Code: Select all
ro.sync.ecss.extensions.api.node.AuthorDocumentFragment.getContent()
The
ro.sync.ecss.extensions.api.Content interface allows you access to the text characters and you can modify them but you have to be careful not to delete any special control characters (which have the integer value of '0').
What you are trying to do will also imply adding two more nodes to the fragment content nodes corresponding to the 2 xrefs which you are trying to add.
This is quite impossible to accomplish with the current API.
The simplest way would have been to be able from the API to set another fragment back to the
ClipboardFragmentInformation like:
Code: Select all
/**
* @see ro.sync.ecss.extensions.api.content.ClipboardFragmentProcessor#process(ro.sync.ecss.extensions.api.content.ClipboardFragmentInformation)
*/
public void process(ClipboardFragmentInformation fragmentInformation) {
AuthorDocumentFragment frag = fragmentInformation.getFragment();
try {
String xmlContent = authorAccess.getDocumentController().serializeFragmentToXML(frag);
xmlContent.replace("[1;2]", "<xref linkend=\"id.1\"/><xref linkend=\"id.2\"/>");
//Re-create a new fragment from the XML
AuthorDocumentFragment newFragment = authorAccess.getDocumentController().createNewDocumentFragmentInContext(xmlContent, authorAccess.getEditorAccess().getCaretOffset());
//UNFORTUNATELY THIS METHOD DOES NOT EXIST
fragmentInformation.setFragment(newFragment);
} catch (BadLocationException e) {
e.printStackTrace();
} catch (AuthorOperationException e) {
e.printStackTrace();
}
}
but unfortunately we do not yet have a method to set another fragment in the fragment information object. We'll try to add this method in the upcoming Oxygen 13 (in a couple of weeks).
Maybe as a workaround you could instead overwrite this method in the ExtensionsBundle:
Code: Select all
ro.sync.ecss.extensions.api.ExtensionsBundle#getAuthorSchemaAwareEditingHandler()
and return an extension of :
Code: Select all
ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandlerAdapter
which overwrites this method:
Code: Select all
ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandlerAdapter.handlePasteFragment(int, AuthorDocumentFragment[], int, AuthorAccess)
The final code would be like this:
Code: Select all
/**
* @see ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandlerAdapter#handlePasteFragment(int, ro.sync.ecss.extensions.api.node.AuthorDocumentFragment[], int, ro.sync.ecss.extensions.api.AuthorAccess)
*/
@Override
public boolean handlePasteFragment(int offset, AuthorDocumentFragment[] fragmentsToInsert,
int actionId, AuthorAccess authorAccess) throws InvalidEditException {
try {
for (int i = 0; i < fragmentsToInsert.length; i++) {
String xmlContent = authorAccess.getDocumentController().serializeFragmentToXML(fragmentsToInsert[i]);
if(xmlContent.contains("[1;2]")) {
xmlContent = xmlContent.replace("[1;2]", "<xref linkend=\"id.1\"/><xref linkend=\"id.2\"/>");
//Re-create a new fragment from the XML
AuthorDocumentFragment newFragment = authorAccess.getDocumentController().createNewDocumentFragmentInContext(xmlContent, authorAccess.getEditorAccess().getCaretOffset());
//And set the new fragment in the array.
fragmentsToInsert[i] = newFragment;
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
//We are just modifying fragments here, so tell that the insertion should be done in the default implementation.
return false;
}
Regards,
Radu