Modify the XML Content on Save
Use Case
You the revised date on a DITA document to be updated when it is saved.
Solution
The Plugins SDK contains a sample plugin type called WorkspaceAccess.Such a plugin is notified when the application starts.
You can add a listener that notifies you before the user saves an XML document. Then if the
XML document is opened in the Author visual editing mode you can use
the Author API to change attributes before the save takes
place:
@Override
public void applicationStarted
(final StandalonePluginWorkspace pluginWorkspaceAccess) {
pluginWorkspaceAccess.addEditorChangeListener
(new WSEditorChangeListener(){
//An editor was opened
@Override
public void editorOpened(URL editorLocation) {
final WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess
(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
if(editorAccess != null){
editorAccess.addEditorListener
(new ro.sync.exml.workspace.api.listeners.WSEditorListener(){
//Editor is about to be saved
@Override
public boolean editorAboutToBeSavedVeto(int operationType) {
if(EditorPageConstants.PAGE_AUTHOR.equals
(editorAccess.getCurrentPageID())){
WSAuthorEditorPage authorPage =
(WSAuthorEditorPage) editorAccess.getCurrentPage();
AuthorDocumentController controller =
authorPage.getDocumentController();
try {
//Find the revised element
AuthorNode[] nodes = controller.findNodesByXPath
("//revised", true, true, true);
if(nodes != null && nodes.length > 0){
AuthorElement revised = (AuthorElement) nodes[0];
//Set the modified attribute to it...
controller.setAttribute("modified",
new AttrValue(new Date().toString()), revised);
}
} catch (AuthorOperationException e) {
e.printStackTrace();
}
}
//And let the save continue..
return true;
}
});
}
}
}, PluginWorkspace.MAIN_EDITING_AREA);
}