Page 1 of 1

How to access resources from javascript

Posted: Tue Aug 09, 2016 4:59 pm
by steve.cuzner
I'm writing a javascript function to insert xml fragments which works well with authorAccess.getDocumentController().insertXMLFragment. I would like to store my fragments as resource files outside of the javascript. I've tried the javascript File object, but that doesn't seem to work. How can I read an external file into a javascript string variable that I can then pass to insertXMLFragment?

Re: How to access resources from javascript

Posted: Tue Aug 09, 2016 6:34 pm
by steve.cuzner
I've found some success using standard java:

Code: Select all

fileReader = new java.io.FileReader("T:\\custom\\Oxygen\\frameworks\\tmwbook.framework\\frag.xml");
br = new java.io.BufferedReader(fileReader);
sb = new java.lang.StringBuilder();
line = br.readLine()
while (line != null)
{
sb.append(line);
line = br.readLine();
}
frag = sb.toString();
authorAccess.getWorkspaceAccess().showInformationMessage(frag);
But this only works when I have a absolute path. The above code is in a function in commons.js which is in tmwbook.framework, the same directory as the frag.xml. Ideally, I would put frag.xml in a resources sub-directory of tmwbook.framework, but in any event, I need to find the path to the framework in the javascript. How can I do that?

Re: How to access resources from javascript

Posted: Tue Aug 09, 2016 7:17 pm
by steve.cuzner

Code: Select all

frameworkDir = authorAccess.getUtilAccess().expandEditorVariables("${frameworkDir}", null);

Re: How to access resources from javascript

Posted: Wed Aug 10, 2016 10:19 am
by alex_jitianu
Hi Steve,

I see you're find the solutions faster that I can answer :) Yes, this is the way you can obtain the framework directory:

Code: Select all

frameworkDir = authorAccess.getUtilAccess().expandEditorVariables("${frameworkDir}", null);
Best regards,
Alex