YUIScriptProcessor.java 4.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.yahoo.platform.yui.compressor.CssCompressor
 *  com.yahoo.platform.yui.compressor.JavaScriptCompressor
 *  javax.annotation.Nonnull
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.mozilla.javascript.ErrorReporter
 *  org.mozilla.javascript.EvaluatorException
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.ui.clientlibs.impl;

import com.adobe.granite.ui.clientlibs.LibraryType;
import com.adobe.granite.ui.clientlibs.script.ScriptProcessor;
import com.adobe.granite.ui.clientlibs.script.ScriptResource;
import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={ScriptProcessor.class})
public class YUIScriptProcessor
implements ScriptProcessor {
    private static final Logger log = LoggerFactory.getLogger(YUIScriptProcessor.class);
    private static final int lineBreakPos = 0;
    private static final boolean warn = false;
    private static final boolean munge = true;
    private static final boolean preserveAllSemiColons = false;
    private static final boolean disableOptimizationsOpt = false;

    @Nonnull
    @Override
    public String getName() {
        return "yui";
    }

    @Override
    public boolean handles(@Nonnull LibraryType type) {
        return type == LibraryType.CSS || type == LibraryType.JS;
    }

    @Override
    public boolean process(@Nonnull LibraryType type, @Nonnull ScriptResource source, @Nonnull Writer output, @Nonnull Map<String, String> options) throws IOException {
        if (!this.handles(type)) {
            log.warn("invalid process call with given type {} and options {}", (Object)type, options);
            return false;
        }
        String minify = options.get("minify");
        if (!"true".equals(minify)) {
            return false;
        }
        if (type == LibraryType.CSS) {
            CssCompressor compressor = new CssCompressor(source.getReader());
            compressor.compress(output, 0);
            return true;
        }
        return this.minifyJS(source.getReader(), output);
    }

    protected boolean minifyJS(@Nonnull Reader source, @Nonnull Writer output) throws IOException {
        JavaScriptCompressor compressor = new JavaScriptCompressor(source, new ErrorReporter(){

            public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
                if (line < 0) {
                    log.debug("\n[WARNING] " + message);
                } else {
                    log.debug("\n[WARNING] " + line + ':' + lineOffset + ':' + message);
                    log.debug("\n[WARNING] " + lineSource);
                }
            }

            public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
                if (line < 0) {
                    log.error("\n[ERROR] " + message);
                } else {
                    log.error("\n[ERROR] " + line + ':' + lineOffset + ':' + message);
                    log.error("\n[ERROR] " + lineSource);
                }
            }

            public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) {
                this.error(message, sourceName, line, lineSource, lineOffset);
                return new EvaluatorException(message);
            }
        });
        try {
            compressor.compress(output, null, 0, true, false, false, false);
            return true;
        }
        catch (IndexOutOfBoundsException e) {
            log.warn("Internal error while compressing script. Most probably caused by empty input: {}", (Object)e.toString());
            return false;
        }
    }

}