Search in sources :

Example 1 with EspReader

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) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) InputStream(java.io.InputStream) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) EspReader(org.apache.sling.scripting.javascript.io.EspReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) EspReader(org.apache.sling.scripting.javascript.io.EspReader) BufferedReader(java.io.BufferedReader) IdFunctionObject(org.mozilla.javascript.IdFunctionObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 2 with EspReader

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) {
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) InputStream(java.io.InputStream) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) EspReader(org.apache.sling.scripting.javascript.io.EspReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) EspReader(org.apache.sling.scripting.javascript.io.EspReader) BufferedReader(java.io.BufferedReader)

Aggregations

BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)2 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)2 EspReader (org.apache.sling.scripting.javascript.io.EspReader)2 IdFunctionObject (org.mozilla.javascript.IdFunctionObject)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1