All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibnet.webpipes.processor.cjson.CJsonRunner Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 0.0.4
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy