org.hibnet.webpipes.processor.cjson.CJsonRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webpipes-cjson Show documentation
Show all versions of webpipes-cjson Show documentation
Webpipes is a library to manage web resources like javascript and css files which need processing
before being served. It manages processing like minimization, file merging, css generation from less, linters,
jsx transformation. Resources can be managed as streams from source, or regularly checked cached resources, or
to be generated with an Ant task.
This module adds support for the CJSon compression algorithm.
package org.hibnet.webpipes.processor.cjson;
import org.hibnet.webpipes.Webpipe;
import org.hibnet.webpipes.processor.rhino.RhinoRunner;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class CJsonRunner extends RhinoRunner {
@Override
protected void initScope(Context context, ScriptableObject globalScope) throws Exception {
addCommon(context, globalScope);
addClientSideEnvironment(context, globalScope);
evaluateFromClasspath(context, globalScope, "/org/hibnet/webpipes/processor/cjson/cjson.min.js");
}
public String run(Webpipe webpipe, boolean pack) throws Exception {
String content = webpipe.getOutput().getContent();
Context context = enterContext();
try {
ScriptableObject scope = createLocalScope(context);
StringBuilder script = new StringBuilder();
if (pack) {
script.append("CJSON.stringify(JSON.parse(");
} else {
script.append("JSON.stringify(CJSON.parse(");
}
script.append(toJSMultiLineString(content));
script.append("));");
content = evaluate(context, scope, script.toString());
} finally {
Context.exit();
}
return content;
}
}