Auto-Generate an ID When a Document is Opened or Created
Question
Is it possible to configure how the application generates ids? For project compliance we need ids having a certain format for each created topic.
Answer
This could be done implementing a plugin for Oxygen XML Author using the Plugins SDK:
https://www.oxygenxml.com/oxygen_sdk.html#Developer_Plugins
There is a type of plugin called "Workspace Access" that can be used to add a listener to be notified when an editor is opened.
The implemented plugin would intercept the opened editor and editor page change events (which occur when a new editor is created) and generate a new ID attribute value on the root element.
The Java code would look like this:
pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
/**
* @see WSEditorChangeListener#editorOpened(java.net.URL)
*/
@Override
public void editorOpened(URL editorLocation) {
WSEditor ed = pluginWorkspaceAccess.getEditorAccess
(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
generateID(ed);
}
/**
* @see WSEditorChangeListener#editorPageChanged(java.net.URL)
*/
@Override
public void editorPageChanged(URL editorLocation) {
WSEditor ed = pluginWorkspaceAccess.getEditorAccess
(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
generateID(ed);
}
private void generateID(WSEditor ed) {
if(ed.getCurrentPage() instanceof WSAuthorEditorPage) {
WSAuthorEditorPage authorEditPage = (WSAuthorEditorPage) ed.getCurrentPage();
AuthorDocumentController ctrl = authorEditPage.getDocumentController();
AuthorElement root = ctrl.getAuthorDocumentNode().getRootElement();
if(root.getAttribute("id") == null ||
!root.getAttribute("id").getValue().startsWith("generated_")) {
ctrl.setAttribute("id", new AttrValue("generated_" + Math.random()), root);
}
}
}
}, PluginWorkspace.MAIN_EDITING_AREA);