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);
}
}
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");
}
}
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;
}
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);
}
}
}
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);
}
}
Aggregations