Search in sources :

Example 76 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class JaggeryParser method parse.

/**
     * Main Parser to process the .jss script
     *
     * @param stream script as the input stream
     * @throws ScriptException If an error occurred during the script parsing
     */
public static InputStream parse(InputStream stream) throws ScriptException {
    try {
        boolean opened = false;
        boolean isExpression = false;
        String str;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        PrintStream source = new PrintStream(output);
        StringBuilder html = new StringBuilder();
        StringBuilder jsExp = new StringBuilder();
        Reader inputReader = new InputStreamReader(stream, "utf-8");
        int ch = inputReader.read();
        while (ch != -1) {
            if (ch == '<') {
                ch = inputReader.read();
                if (ch == '%') {
                    opened = true;
                    str = html.toString();
                    //as it is html, we can avoid adding empty print("") calls
                    if (!str.equals("")) {
                        source.append("print(\"").append(str).append("\");");
                        html = new StringBuilder();
                    }
                    ch = inputReader.read();
                    if (ch == '=') {
                        isExpression = true;
                    } else {
                        continue;
                    }
                } else {
                    if (opened) {
                        if (isExpression) {
                            jsExp.append("<");
                        } else {
                            source.append("<");
                        }
                    } else {
                        html.append('<');
                    }
                    continue;
                }
                ch = inputReader.read();
            } else if (ch == '%') {
                ch = inputReader.read();
                if (ch == '>') {
                    opened = false;
                    if (isExpression) {
                        isExpression = false;
                        //if it need, we can validate "jsExp" here or let the compiler to do it.
                        source.append("print(").append(jsExp).append(");");
                        jsExp = new StringBuilder();
                    }
                } else {
                    if (opened) {
                        source.append('%');
                    } else {
                        html.append('%');
                    }
                    continue;
                }
                ch = inputReader.read();
            } else {
                if (opened) {
                    if (isExpression) {
                        jsExp.append((char) ch);
                    } else {
                        source.append((char) ch);
                    }
                    ch = inputReader.read();
                } else {
                    int next = inputReader.read();
                    if (ch == '"') {
                        html.append('\\').append('\"');
                    } else if (ch == '\\') {
                        html.append('\\').append('\\');
                    } else if (ch == '\r') {
                        html.append('\\').append('r');
                    } else if (ch == '\n') {
                        source.append("print(\"").append(html.toString());
                        if (next != -1) {
                            source.append('\\').append('n');
                        }
                        source.append("\");").append('\n');
                        html = new StringBuilder();
                    } else if (ch == '\t') {
                        // Not sure we need this
                        html.append('\\').append('t');
                    } else {
                        html.append((char) ch);
                    }
                    ch = next;
                }
            }
        }
        str = html.toString();
        if (!str.equals("")) {
            source.append("print(\"").append(str).append("\");");
        }
        str = jsExp.toString();
        if (!str.equals("")) {
            source.append("print(").append(str).append(");");
        }
        return new ByteArrayInputStream(output.toByteArray());
    } catch (IOException e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 77 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class FeedHostObject method jsFunction_getFeed.

public static synchronized void jsFunction_getFeed(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
    if (arguments.length != 1) {
        throw new ScriptException("Invalid parameter");
    }
    if (arguments[0] instanceof String) {
        feed = null;
        URL url = null;
        try {
            url = new URL((String) arguments[0]);
            feed = (Feed) Abdera.getNewParser().parse(url.openStream()).getRoot();
            isRssFeed = false;
        } catch (ClassCastException e) {
            XmlReader reader = null;
            try {
                reader = new XmlReader(url);
                rssFeed = new SyndFeedInput().build(reader);
                isRssFeed = true;
                for (Iterator i = rssFeed.getEntries().iterator(); i.hasNext(); ) {
                    SyndEntry entry = (SyndEntry) i.next();
                }
            } catch (IOException e1) {
                throw new ScriptException(e1);
            } catch (Exception e1) {
                throw new ScriptException(e1);
            } finally {
                if (reader != null)
                    try {
                        reader.close();
                    } catch (IOException e1) {
                        throw new ScriptException(e1);
                    }
            }
        } catch (IRISyntaxException e) {
            throw new ScriptException(e);
        } catch (MalformedURLException e) {
            throw new ScriptException(e);
        } catch (IOException e) {
            throw new ScriptException(e);
        }
    } else {
        throw new ScriptException("Invalid parameter, It is must to be a String");
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) MalformedURLException(java.net.MalformedURLException) SyndFeedInput(com.sun.syndication.io.SyndFeedInput) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) Iterator(java.util.Iterator) IRISyntaxException(org.apache.abdera.i18n.iri.IRISyntaxException) XmlReader(com.sun.syndication.io.XmlReader) IOException(java.io.IOException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IRISyntaxException(org.apache.abdera.i18n.iri.IRISyntaxException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 78 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class FeedHostObject method jsGet_logo.

public String jsGet_logo() throws ScriptException {
    String logoStr = null;
    IRI logo = feed.getLogo();
    if (logo == null) {
        return null;
    }
    try {
        logoStr = logo.toURL().toString();
    } catch (MalformedURLException e) {
        throw new ScriptException(e);
    } catch (URISyntaxException e) {
        throw new ScriptException(e);
    }
    return logoStr;
}
Also used : IRI(org.apache.abdera.i18n.iri.IRI) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException)

Example 79 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RequestHostObject method parseMultipartParams.

private static void parseMultipartParams(RequestHostObject rho, String encoding, Context cx, Scriptable scope) throws ScriptException {
    if (rho.parameters != null) {
        return;
    }
    rho.parameters = rho.context.newObject(rho);
    for (String name : rho.parameterMap.keySet()) {
        try {
            ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
            if (x.size() > 1) {
                Scriptable y = cx.newArray(scope, x.size());
                for (int i = 0; i < x.size(); i++) {
                    y.put(i, y, x.get(i).getString(encoding));
                }
                rho.parameters.put(name, rho.parameters, y);
            } else {
                rho.parameters.put(name, rho.parameters, x.get(0).getString(encoding));
            }
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Scriptable(org.mozilla.javascript.Scriptable)

Example 80 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RequestHostObject method jsFunction_getInputStream.

public static InputStream jsFunction_getInputStream(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getInputStream";
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    RequestHostObject rho = (RequestHostObject) thisObj;
    try {
        return rho.request.getInputStream();
    } catch (IOException e) {
        String msg = "Error occurred while reading Servlet InputStream";
        log.warn(msg, e);
        throw new ScriptException(msg, e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) IOException(java.io.IOException)

Aggregations

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)83 IOException (java.io.IOException)15 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)9 URL (java.net.URL)8 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)8 MalformedURLException (java.net.MalformedURLException)7 ServletContext (javax.servlet.ServletContext)6 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)6 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 Context (org.mozilla.javascript.Context)5 File (java.io.File)4 StringReader (java.io.StringReader)4 Callable (java.util.concurrent.Callable)4 ExecutorService (java.util.concurrent.ExecutorService)4 FileItem (org.apache.commons.fileupload.FileItem)4 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)4 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)4