Determine if opened file was just created from a custom template

Post here questions and problems related to oXygen frameworks/document types.
kirkilj
Posts: 110
Joined: Fri May 14, 2010 12:14 am

Determine if opened file was just created from a custom template

Post 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?
Cosmin Duna
Site Admin
Posts: 120
Joined: Wed Dec 12, 2018 5:33 pm

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

Post 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
Cosmin Duna
<oXygen/> XML Editor
http://www.oxygenxml.com
kirkilj
Posts: 110
Joined: Fri May 14, 2010 12:14 am

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

Post by kirkilj »

Thanks so much Cosmin. We'll give it a try.
Post Reply