Xpath in Plugin

Oxygen general issues.
patrick
Posts: 96
Joined: Mon May 09, 2011 11:54 am

Xpath in Plugin

Post by patrick »

Hi,

are there xml/xpath functions in an (document) plugin? I only get text-based functions and need to work with results restricted to xpaths (e.g. fileref-Attributes only from graphics inside figures (figure/graphic/@fileref).

How can I solve this?

Thanks,
Patrick
Radu
Posts: 9445
Joined: Fri Jul 09, 2004 5:18 pm

Re: Xpath in Plugin

Post by Radu »

Hi Patrick,

What plugin extension type are you exactly using?
Is it "ro.sync.exml.plugin.document.DocumentPluginExtension"?
This is quite an old plugin type, the Workspace Access plugin extension type should be more powerful and flexible.
But even with the DocumentPluginExtension you can do stuff like:

Code: Select all


  @Override
public DocumentPluginResult process(DocumentPluginContext context) {
WSEditor currentEditorAccess = context.getPluginWorkspace().getCurrentEditorAccess(StandalonePluginWorkspace.MAIN_EDITING_AREA);
WSEditorPage cp = currentEditorAccess.getCurrentPage();
if(cp instanceof WSXMLTextEditorPage) {
//XML edited in Text mode.
try {
WSXMLTextNodeRange[] ranges = ((WSXMLTextEditorPage)cp).findElementsByXPath("//@id");
} catch (XPathException e) {
e.printStackTrace();
}
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
patrick
Posts: 96
Joined: Mon May 09, 2011 11:54 am

Re: Xpath in Plugin

Post by patrick »

Thank you, I switched to an Workspace Access Plugin. Can you post an example, how to get the value as String from the Ranges?
Radu
Posts: 9445
Joined: Fri Jul 09, 2004 5:18 pm

Re: Xpath in Plugin

Post by Radu »

Hi Patrick,

You would more or less do it like:

Code: Select all

    WSXMLTextEditorPage xmlTextPage = ...;
WSXMLTextNodeRange range = ...;
try {
int startOffset = xmlTextPage.getOffsetOfLineStart(range.getStartLine()) + range.getStartColumn();
int endOffset = xmlTextPage.getOffsetOfLineStart(range.getEndColumn()) + range.getEndColumn();
String rangeText = xmlTextPage.getDocument().getText(startOffset, endOffset - startOffset);
} catch (BadLocationException e) {
e.printStackTrace();
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
patrick
Posts: 96
Joined: Mon May 09, 2011 11:54 am

Re: Xpath in Plugin

Post by patrick »

Hi Radu,

thank you, it works now. But another problem: I have tried to add buttons (using the example Workspace Access Plugin) - but the buttons do not have the style like the rest of oxygen. How can I add buttons with the same style which is set in the oxygen preferences?
Radu
Posts: 9445
Joined: Fri Jul 09, 2004 5:18 pm

Re: Xpath in Plugin

Post by Radu »

Hi Patrick,

Instead of JButtons you should create ro.sync.exml.workspace.api.standalone.ui.ToolbarButton which should look the same as our other toolbar buttons.
In the same API package ro.sync.exml.workspace.api.standalone.ui you may find other useful components.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
patrick
Posts: 96
Joined: Mon May 09, 2011 11:54 am

Re: Xpath in Plugin

Post by patrick »

Thank you! But to the other problem with xpath:

Is there another solution which really supports xpath without these ranges? My project got a little bit more complex and now I have to search for a specific element and get its attributes. If I get the whole node as a string, I only get these attribute values with a regex search and this sounds dirty...
Radu
Posts: 9445
Joined: Fri Jul 09, 2004 5:18 pm

Re: Xpath in Plugin

Post by Radu »

Hi Patrick,

The problem is that the Text editing mode does not have a rigid node structure underneath (like the Author editing mode) so we do not really expose much API to look into its structure. Our Outline view in the Text editing mode is based on a semi-structured hierarchical model which we keep synchronized with the text changes but unfortunately we do not yet have API to access that structure, maybe we'll provide this in a future version.

You can also use this API method:

Code: Select all

ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage.evaluateXPath(String)
which usually returns an array of DOM nodes (or atomic values, depending on the XPath expression). But this is done without offering you the localization possible with the other XPath-based method which can only localize elements (and not attribute names and values from an element).

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