Page 1 of 1

JSOperation API issues

Posted: Fri Apr 05, 2019 1:05 am
by martindholmes
HI there,

I'm working on creating actions using the JSOperation functionality, and encountering some problems calling API functions. Here's some example code:

Code: Select all


function doOperation() {
var selText = authorAccess.getEditorAccess().getSelectedText();
var documentController = authorAccess.getDocumentController();
var workspaceAccess = authorAccess.getWorkspaceAccess();
try {

if (selText.length() > 0) {
//First get the selection bounds.
var selEnd = authorAccess.getEditorAccess().getSelectionEnd();
var selStart = authorAccess.getEditorAccess().getSelectionStart();

//Get all the starting anchors in our document.
var anchors = new Array(documentController.findNodesByXPath("//*:anchor[@next]", true, true, true));

//THIS SHOWS "1" EVEN THOUGH THERE ARE LOTS OF SUCH ANCHORS IN THE DOCUMENT.
workspaceAccess.showErrorMessage(anchors.length);

//Get the ids we care about.
var relevantAnchors = [];
for (var i = 0; i < anchors.length; i++) {

//THIS SHOWS THE ERROR MESSAGE BELOW.
var nodeStart = anchors[i].getStartOffset();
if ((nodeStart >= selStart) && (nodeStart < selEnd)) {
relevantAnchors.push(anchors[i].getAttribute('next').toString().substring(1));
}
}
}

} catch (e1) {
//e1.printStackTrace();
workspaceAccess.showErrorMessage(e1);
}
}
The error message I see is:

InternalError: Java class "[Lro.sync.ecss.extensions.api.node.AuthorNode;" has no public instance field or method named "getStartOffset".

There does seem to be such a function according to the API docs:

https://www.oxygenxml.com/InstData/Edit ... rtOffset--

I'm a bit puzzled by the square bracket and L character at the beginning of the class specification in the message. I've tried explicitly casting to an AuthorNode. What am I missing here? Why does the length of the AuthorNode array come back as only 1, and why can't I call getStartOffset()?

All help appreciated,
Martin

Re: JSOperation API issues

Posted: Fri Apr 05, 2019 1:15 am
by martindholmes
Figured it out. I don't need to cast the findNodesByXPath result to an array.

Re: JSOperation API issues

Posted: Fri Apr 05, 2019 7:26 am
by Radu
Hi Martin,

One of the disadvantages of working with Javascript is the weak typing. If you worked with Java code the compiler would let you know about such problems plus the Java IDE would help you find out what type of results each API function returns.
But with Javascript you are stuck to reading the Javadoc to see the return types and parameters for each function.

Regards,
Radu

Re: JSOperation API issues

Posted: Sat Apr 06, 2019 2:41 am
by martindholmes
You're absolutely right. But with the JS I can write code and test it on any old machine, whereas I need a Java build environment to use Java. I always find it painful to get Eclipse set up for Java work and I tend to avoid it if I can.

It would be neat if there were a docker container all pre-configured for programming Oxygen. :-)

Cheers,
Martin