use of org.apache.sling.scripting.javascript.io.EspReader in project sling by apache.
the class SlingGlobal method load.
private void load(Context cx, Scriptable thisObj, Object[] args) {
SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING, SlingScriptHelper.class);
if (sling == null) {
throw new NullPointerException(SlingBindings.SLING);
}
Scriptable globalScope = ScriptableObject.getTopLevelScope(thisObj);
Resource scriptResource = sling.getScript().getScriptResource();
ResourceResolver resolver = scriptResource.getResourceResolver();
// the path of the current script to resolve realtive paths
String currentScript = sling.getScript().getScriptResource().getPath();
String scriptParent = ResourceUtil.getParent(currentScript);
for (Object arg : args) {
String scriptName = ScriptRuntime.toString(arg);
Resource loadScript = null;
if (!scriptName.startsWith("/")) {
String absScriptName = scriptParent + "/" + scriptName;
loadScript = resolver.resolve(absScriptName);
}
// not resolved relative to the current script
if (loadScript == null) {
loadScript = resolver.resolve(scriptName);
}
if (loadScript == null) {
throw Context.reportRuntimeError("Script file " + scriptName + " not found");
}
InputStream scriptStream = loadScript.adaptTo(InputStream.class);
if (scriptStream == null) {
throw Context.reportRuntimeError("Script file " + scriptName + " cannot be read from");
}
try {
// reader for the stream
Reader scriptReader = new InputStreamReader(scriptStream, Charset.forName("UTF-8"));
// check whether we have to wrap the basic reader
if (scriptName.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
scriptReader = new EspReader(scriptReader);
}
// read the suff buffered for better performance
scriptReader = new BufferedReader(scriptReader);
// now, let's go
cx.evaluateReader(globalScope, scriptReader, scriptName, 1, null);
} catch (IOException ioe) {
throw Context.reportRuntimeError("Failure reading file " + scriptName + ": " + ioe);
} finally {
// ensure the script input stream is closed
try {
scriptStream.close();
} catch (IOException ignore) {
}
}
}
}
use of org.apache.sling.scripting.javascript.io.EspReader in project sling by apache.
the class SlingGlobal method loadModule.
private ModuleScope loadModule(Context cx, String modulePath, ModuleScope moduleScope, Scriptable thisObj) {
String absolutePath = modulePath;
if (modulePath.startsWith(".")) {
// relative
if (moduleScope == null) {
throw Context.reportRuntimeError("Cannot resolve relative module name outside of a module scope.");
}
absolutePath = (moduleScope.getModuleName() + "/" + modulePath).replaceAll("[^/]*/\\./", "");
while (absolutePath.matches("([^/]*/)?[^/]*/\\.\\./")) {
absolutePath = absolutePath.replaceAll("([^/]*/)?[^/]*/\\.\\./", "");
}
}
absolutePath = absolutePath + ".js";
SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING, SlingScriptHelper.class);
if (sling == null) {
throw new NullPointerException(SlingBindings.SLING);
}
ResourceResolver resrev = sling.getScript().getScriptResource().getResourceResolver();
Resource script = null;
String scriptName = null;
for (String basepath : resrev.getSearchPath()) {
script = resrev.resolve(basepath + absolutePath);
if (script != null && !(script instanceof NonExistingResource)) {
scriptName = basepath + absolutePath;
break;
}
}
if (script == null) {
throw Context.reportRuntimeError("Unable to resolve module " + absolutePath + " in search path");
}
InputStream scriptStream = script.adaptTo(InputStream.class);
if (scriptStream == null) {
//try once again
scriptStream = resrev.resolve(scriptName).adaptTo(InputStream.class);
if (scriptStream == null) {
throw Context.reportRuntimeError("Script file " + script.getPath() + " cannot be read");
}
}
try {
// reader for the stream
Reader scriptReader = new InputStreamReader(scriptStream, Charset.forName("UTF-8"));
// check whether we have to wrap the basic reader
if (scriptName.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
scriptReader = new EspReader(scriptReader);
}
// read the suff buffered for better performance
scriptReader = new BufferedReader(scriptReader);
//TODO: execute script with ModuleScope
// now, let's go
ModuleScope scope = moduleScope;
if (scope == null) {
scope = new ModuleScope(thisObj, absolutePath.substring(0, absolutePath.length() - 3));
} else {
scope.reset();
}
cx.evaluateReader(scope, scriptReader, scriptName, 1, null);
return scope;
} catch (IOException ioe) {
throw Context.reportRuntimeError("Failure reading file " + scriptName + ": " + ioe);
} finally {
// ensure the script input stream is closed
try {
scriptStream.close();
} catch (IOException ignore) {
}
}
}
Aggregations