Search in sources :

Example 1 with EvaluatorException

use of com.yahoo.platform.yui.org.mozilla.javascript.EvaluatorException in project jahia by Jahia.

the class StaticAssetsFilter method minify.

private static void minify(String path, Resource resource, String type, File minifiedFile, boolean compress) throws IOException {
    Reader reader = null;
    Writer writer = null;
    File tmpMinifiedFile = new File(minifiedFile.getParentFile(), minifiedFile.getName() + "." + System.nanoTime());
    try {
        reader = new InputStreamReader(resource.getInputStream(), ASSET_ENCODING);
        writer = new OutputStreamWriter(new FileOutputStream(tmpMinifiedFile), ASSET_ENCODING);
        if (compress && type.equals("css") && !path.contains(".min")) {
            String s = IOUtils.toString(reader);
            IOUtils.closeQuietly(reader);
            s = urlRewriting(s, path);
            reader = new StringReader(s);
            CssCompressor compressor = new CssCompressor(reader);
            compressor.compress(writer, -1);
        } else if (compress && type.equals("js") && !path.contains(".min")) {
            try {
                JavaScriptCompressor compressor = new JavaScriptCompressor(reader, new JavaScriptErrorReporter());
                compressor.compress(writer, -1, true, true, false, false);
            } catch (EvaluatorException e) {
                logger.error("Error when minifying " + path, e);
                IOUtils.closeQuietly(reader);
                IOUtils.closeQuietly(writer);
                reader = new InputStreamReader(resource.getInputStream(), ASSET_ENCODING);
                writer = new OutputStreamWriter(new FileOutputStream(tmpMinifiedFile), ASSET_ENCODING);
                IOUtils.copy(reader, writer);
            }
        } else {
            if (type.equals("css")) {
                String s = IOUtils.toString(reader);
                IOUtils.closeQuietly(reader);
                reader = new StringReader(urlRewriting(s, path));
            }
            BufferedWriter bw = new BufferedWriter(writer);
            BufferedReader br = new BufferedReader(reader);
            try {
                String s;
                while ((s = br.readLine()) != null) {
                    bw.write(s);
                    bw.write("\n");
                }
            } finally {
                IOUtils.closeQuietly(bw);
                IOUtils.closeQuietly(br);
            }
        }
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
        atomicMove(tmpMinifiedFile, minifiedFile);
    }
}
Also used : CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) JavaScriptCompressor(com.yahoo.platform.yui.compressor.JavaScriptCompressor) EvaluatorException(com.yahoo.platform.yui.org.mozilla.javascript.EvaluatorException)

Aggregations

CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)1 JavaScriptCompressor (com.yahoo.platform.yui.compressor.JavaScriptCompressor)1 EvaluatorException (com.yahoo.platform.yui.org.mozilla.javascript.EvaluatorException)1