Custom action to execute XPath search

Post here questions and problems related to oXygen frameworks/document types.
DDubois
Posts: 4
Joined: Tue Oct 13, 2015 10:50 pm

Custom action to execute XPath search

Post by DDubois »

Hello All,

I want to create a custom action that performs an XPath search across a project using information from the current cursor position. I am trying to implement "go to definition" like behaviour (as seen in XSLT) for a custom XML language. I know this is likely achievable with the SDK and author a custom action in JAVA, but I was hoping there was a way to do this without the SDK. Thank you for all your help.

Thanks,
Dirk
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom action to execute XPath search

Post by Radu »

Hi Dirk,

I don't see another possibility other than using our SDK to add custom actions.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
DDubois
Posts: 4
Joined: Tue Oct 13, 2015 10:50 pm

Re: Custom action to execute XPath search

Post by DDubois »

Hi Radu,

Alright not a problem I have done that before and it is pretty straight forward to do. I just want to clairfy, I can make custom actions available inside the "text" mode when editing a document right? Ideally I would write a custom action that allowed performed some Xpath searches and returned a result. This action would be activated via a right mouse click context menu based on the node currently being edited. Finally, do you have any recommendations for how to perform the XPath search and return or navigate the user to the results? I'm familiar with the XPath results window that appears in Oxygen when performing a search. Or the pop-up menu of options that appears with an XSL go to definitions. Thank you for all your help.

Thanks,
Dirk
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom action to execute XPath search

Post by Radu »

Hi Dirk,

So:
I just want to clairfy, I can make custom actions available inside the "text" mode when editing a document right?
If you edit a document type association for the XML document, the Author tab allows you to define actions. But these actions only work with the Author editing mode and are not available in the Text editing mode.
To add contextual menu actions in the Text editing mode you would need to create a workspace access plugin extension:

https://www.oxygenxml.com/doc/versions/ ... lugin.html

Such an extension can add listeners which detect when an editor is opened, and if it is opened in the Text editing mode you can add a popup menu customizer to it:

Code: Select all

ro.sync.exml.workspace.api.editor.page.text.WSTextEditorPage.addPopUpMenuCustomizer(TextPopupMenuCustomizer)
Finally, do you have any recommendations for how to perform the XPath search and return or navigate the user to the results? I'm familiar with the XPath results window that appears in Oxygen when performing a search. Or the pop-up menu of options that appears with an XSL go to definitions.
Tough one, we do not have API to run XPaths over multiple files.
We have APIs to create XSLT transformers:

Code: Select all

ro.sync.exml.workspace.api.util.XMLUtilAccess.createXSLTTransformer(Source, URL[], int)
so potentially you could apply XSLT over each document. Saxon 9 has XSLT extensions which allow you to obtain line and column numbers for XML elements like:

http://www.saxonica.com/html/documentat ... umber.html

but you would need to somehow create your own results view to show the matches, again, we do not have API to allow you to use the XPath results window.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
DDubois
Posts: 4
Joined: Tue Oct 13, 2015 10:50 pm

Re: Custom action to execute XPath search

Post by DDubois »

Hi Radu,

Thank you so much for the complete reply. I was afraid I would have to deploy the workspace access plugin. I have done so once before to provide contextual menus in the editor. Alright, so seeing as it would appear difficult to perform Xpaths across multiple files, what about inside the current file? If I perform an Xpath search in the current file, and I'm given a node (or list of nodes) back from your api, is there an easy way to adjust the users positions in the editor to that location? My thought was I could get the cursor offset of the destination node and adjust the position of the cursor, or something along those lines. Any API ideas you have would be greatly appreciated. As always thank you for all your help.

Thanks,
Dirk
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom action to execute XPath search

Post by Radu »

Hi Dirk,

Yes, you have API to run XPaths on the current XML text page and return intervals which can be located back in the content like:

Code: Select all

      @Override
public void applicationStarted(StandalonePluginWorkspace pluginWorkspaceAccess) {
pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener(){
/**
* @see ro.sync.exml.workspace.api.listeners.WSEditorChangeListener#editorOpened(java.net.URL)
*/
@Override
public void editorOpened(URL editorLocation) {
WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
WSEditorPage currentPage = editorAccess.getCurrentPage();
if(currentPage instanceof WSXMLTextEditorPage){
WSXMLTextEditorPage xmlTextPage = ((WSXMLTextEditorPage)currentPage);
xmlTextPage.addPopUpMenuCustomizer(new TextPopupMenuCustomizer() {
@Override
public void customizePopUpMenu(Object popUp, WSTextEditorPage textPage) {
((JPopupMenu)popUp).add(new AbstractAction("Perform XPath") {
@Override
public void actionPerformed(ActionEvent e) {
try {
WSXMLTextNodeRange[] allElementRanges = xmlTextPage.findElementsByXPath("//*");
xmlTextPage.setCaretPosition(xmlTextPage.getOffsetOfLineStart(allElementRanges[0].getStartLine()) + allElementRanges[0].getStartColumn());
} catch (XPathException e1) {
e1.printStackTrace();
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
}
});
}
}
}, PluginWorkspace.MAIN_EDITING_AREA);
}
One possibility to handle xpaths over multiple files would be to programatically open them in Oxygen, run the XPaths on them using the API exemplified above and then close them. But the user would see the application flicker, maybe you could show a large non-modal progress dialog which would cover the entire application while the operation is in progress.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply