Page 1 of 1

Determine if opened file was just created from a custom template

Posted: Mon Feb 20, 2023 8:59 pm
by kirkilj
We'd like to know if the current editor document in Author mode was just created from one of our custom DITA templates and hasn't been touched by anyone so we can apply some custom logic to generate some initial content based on a live lookup that cannot be accomplished with a custom editor variable.

One thought is to include a processing instruction in the template topic that we can detect through when a DITA topic is opened by Oxygen's New action, insert our generated content, and then remove the processing instruction so that it's no longer applied when the file is next opened.

Is there a better way to determine if a file that was just opened was created from one of our templates?

I noticed that the XML Refactoring operation for deleting processing instructions will only remove it if it's within the topic's root element and not before. Is this a restriction throughout the SDK, or is there a method that could detect and delete a specific processing instruction before the root topic declaration?

Re: Determine if opened file was just created from a custom template

Posted: Tue Feb 21, 2023 6:47 pm
by Cosmin Duna
Hi John,

I think using a processing instruction for marking your template is a good idea.
We have an example in one of our add-on when it listens when an editor is opened and checks if it's a new document. See line
52 from here https://github.com/oxygenxml/oxygen-dit ... nsion.java. It was also needed to check the creation time of the files for the cases when the new document was created from the contextual menu of Project or DITA Maps Manager view.

After, you can execute an XPath for finding the PI node from the custom DITA template. Then, use the "deleteNode" method for removing the marker. Something like this:

Code: Select all

	
	                 	final WSEditor editor = workspace.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
				WSEditorPage page = editor.getCurrentPage();
				if (page instanceof WSAuthorEditorPage) {
				     AuthorDocumentController documentController = ((WSAuthorEditorPage) page).getDocumentController();
				     String piXPath = "/processing-instruction('xml-stylesheet')";
                                     AuthorNode[] piNodes = documentController.findNodesByXPath(piXPath, false, false, false);
          
                                    documentController.deleteNode(node[0]);
				}
Best regards,
Cosmin

Re: Determine if opened file was just created from a custom template

Posted: Wed Feb 22, 2023 1:59 am
by kirkilj
Thanks so much Cosmin. We'll give it a try.