Example usage for com.google.common.css.compiler.commandline ClosureCommandLineCompiler main

List of usage examples for com.google.common.css.compiler.commandline ClosureCommandLineCompiler main

Introduction

In this page you can find the example usage for com.google.common.css.compiler.commandline ClosureCommandLineCompiler main.

Prototype

public static void main(String[] args) 

Source Link

Usage

From source file:org.ctoolkit.maven.plugins.optimizer.CssOptimizer.java

/**
 * Process css optimization/* www.j  a v  a  2 s.  com*/
 *
 * @param pathToXml     path to css configuration xml
 * @param cssOutputPath css output path
 */
public static void process(String pathToXml, String cssOutputPath, String baseDir) {
    if (pathToXml == null || pathToXml.trim().isEmpty()) {
        log.info("Css path to xml is not set. Skipping css optimization.");
        return;
    }

    if (!new File(pathToXml).exists()) {
        throw new IllegalArgumentException("Css path to xml does not exists: " + pathToXml);
    }

    log.info("Starting to optimize css...");

    try {
        for (CSSConfig cssConfig : getCssConfigList(pathToXml, baseDir)) {
            String cssOutputFileName = cssConfig.getCssOutputName();
            List<String> cssPaths = new ArrayList<>();

            // create external css files
            for (String externalCssContent : cssConfig.getExternalCssList()) {
                String externalCssFileName = FileHelper
                        .writeToFile(File.createTempFile("externalCssFile", ".css"), externalCssContent);
                cssPaths.add(externalCssFileName);
            }

            // encode images in css
            for (String cssCustomInputFileName : cssConfig.getCssList()) {
                File cssCustomFile = new File(cssCustomInputFileName);

                if (cssCustomFile.exists()) {
                    String cssCustomString = encodeImages(cssCustomFile);
                    String encodedCssFileName = FileHelper
                            .writeToFile(File.createTempFile("encodedCssFile", ".css"), cssCustomString);

                    cssPaths.add(encodedCssFileName);
                }
            }

            // minify css
            String cssOutputString;
            if (cssConfig.getMinfy()) {
                cssPaths.add("-o"); // set output file parameter as temp file (we will load its content into cssOutputString
                File cssMinifiedFile = File.createTempFile("minifiedCssFile", "css");
                cssPaths.add(cssMinifiedFile.getAbsolutePath());
                ClosureCommandLineCompiler.main(cssPaths.toArray(new String[cssPaths.size()]));

                cssOutputString = Files.toString(cssMinifiedFile, Charsets.UTF_8);

                log.info("CSS minified output:\n===\n" + cssOutputString + "\n===\n");
            } else {
                StringBuilder output = new StringBuilder();
                for (String cssPath : cssPaths) {
                    output.append(Files.toString(new File(cssPath), Charsets.UTF_8));
                }

                cssOutputString = output.toString();
            }

            // create final css file
            String optimizedCss = FileHelper.createOutputFile(cssOutputPath, cssOutputFileName,
                    cssOutputString);

            log.info(">>> Css optimization finished successfully. Optimized css file can be found at: "
                    + optimizedCss);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error occurred during processing css: ", e);
    }
}