AuthorDocumentController.evaluateXPath with custom extension

Post here questions and problems related to oXygen frameworks/document types.
Patrik
Posts: 231
Joined: Thu Nov 28, 2013 9:32 am
Location: Hamburg/Germany
Contact:

AuthorDocumentController.evaluateXPath with custom extension

Post by Patrik » Fri Apr 11, 2014 9:19 am

Hi,

in my custom frameworks I'm using xpath expressions defined as fixed attributes within the xsd that need to be evaluated when working with the document. When the document is beeing processed for pdf output or transformations with saxon xslt I can use my own extension functions within these xpath expressions.

However, during editing the document I'm using AuthorDocumentController.evaluateXPath to evaluate the xpath expression (for instance within the StyleFilter or LinkTextResolver).

Now I'm wondering if there's a way to register my own extension functions so they can be used in the path evaluated this way? Especially since you already have extension functions oxy:current-selected-element() and oxy:allows-child-element().

Thanks and regards,

Patrik

alex_jitianu
Posts: 702
Joined: Wed Nov 16, 2005 11:11 am

Re: AuthorDocumentController.evaluateXPath with custom exten

Post by alex_jitianu » Fri Apr 11, 2014 3:05 pm

Hello Patrik,

Currently we don't have such an API. I can think of the following solutions:

1. Instead of using *AuthorDocumentController.evaluateXPath* create a transformer (you can specify the extensions functions to use) and execute it over the current document. Something like this:

Code: Select all


ExtensionFunctionDefinition[] saxonExtensions = ...;
Transformer transformer = access.getXMLUtilAccess().createSaxon9HEXSLTTransformerWithExtensions(styleSource, saxonExtensions);
InputSource inputSource = new InputSource(access.getEditorAccess().getEditorLocation().toString());
inputSource.setCharacterStream(access.getEditorAccess().createContentReader());

Source xmlSource = new SAXSource(inputSource);
transformer.transform(xmlSource, outputTarget);
2. The second solution involves using reflection to invoke your extension functions (reflexive extension functions):
2.1 Copy the jar with the extension inside Oxygen lib folder.
2.2 On an AuthorExtensionStateListener.activated declare the namespace mappings by executing a the code like the one below. Please note that the attribute is marked as being "notSpecified" (the false argument to the AttrValue) so it will not appear in the document.

Code: Select all


/**
* @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#activated(ro.sync.ecss.extensions.api.AuthorAccess)
*/
@Override
public void activated(AuthorAccess authorAccess) {
AuthorNode authorElement = authorAccess.getDocumentController().getAuthorDocumentNode().getRootElement();
authorAccess.getDocumentController().setAttribute("xmlns:date", new AttrValue("test.MyClass", "test.MyClass", false), (AuthorElement) authorElement);
2.3. On the LinkTextResolver or StylesFilter you can execute the XPath explicitly requesting XPath 3.0 (this will force Saxon PE as the transformer, which supports reflexive extensions functions):

Code: Select all


Object[] evaluateXPath = authorAccess.getDocumentController().evaluateXPath(
"date:getCurrentDate()",
node,
true,
true,
true,
false,
XPathVersion.XPATH_3_0);
Best regards,
Alex

Post Reply