Search in sources :

Example 1 with CssCompressor

use of com.yahoo.platform.yui.compressor.CssCompressor in project BroadleafCommerce by BroadleafCommerce.

the class ResourceMinificationServiceImpl method minify.

protected void minify(BufferedReader in, BufferedWriter out, String filename, String type) throws IOException {
    if (JS_TYPE.equals(type)) {
        JavaScriptCompressor jsc = new JavaScriptCompressor(in, getLogBasedErrorReporter(filename));
        jsc.compress(out, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
    } else if (CSS_TYPE.equals(type)) {
        CssCompressor cssc = new CssCompressor(in);
        cssc.compress(out, 100);
    }
}
Also used : CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) JavaScriptCompressor(com.yahoo.platform.yui.compressor.JavaScriptCompressor)

Example 2 with CssCompressor

use of com.yahoo.platform.yui.compressor.CssCompressor in project scout.rt by eclipse.

the class MinifyCssWithYui method run.

public String run(String content) throws IOException {
    try (StringReader reader = new StringReader(content);
        StringWriter writer = new StringWriter()) {
        CssCompressor compressor = new CssCompressor(reader);
        compressor.compress(writer, -1);
        writer.flush();
        return writer.toString();
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor)

Example 3 with CssCompressor

use of com.yahoo.platform.yui.compressor.CssCompressor in project yui-compressor-ant-task by n0ha.

the class YuiCompressorTask method compressFile.

private void compressFile(final File inFile, final File outFile, final String fileType) throws EvaluatorException, BuildException {
    // always recompress when outFile and inFile are exactly the same file
    if (outFile.isFile() && !inFile.getAbsolutePath().equals(outFile.getAbsolutePath())) {
        if (outFile.lastModified() >= inFile.lastModified()) {
            return;
        }
    }
    try {
        // prepare input file
        Reader in = openFile(inFile);
        // prepare output file
        outFile.getParentFile().mkdirs();
        Writer out = new OutputStreamWriter(new FileOutputStream(outFile), charset);
        if (fileType.equals(FileType.JS_FILE)) {
            final JavaScriptCompressor compressor = createJavaScriptCompressor(in);
            compressor.compress(out, lineBreakPosition, munge, warn, preserveAllSemiColons, !optimize);
        } else if (fileType.equals(FileType.CSS_FILE)) {
            final CssCompressor compressor = new CssCompressor(in);
            compressor.compress(out, lineBreakPosition);
        } else if (fileType.equals(FileType.HTML_FILE) || fileType.equals(FileType.XHTML_FILE)) {
            final HtmlCompressor compressor = new HtmlCompressor();
            out.write(compressor.compress(readerToString(in)));
        } else if (fileType.equals(FileType.XML_FILE)) {
            final XmlCompressor compressor = new XmlCompressor();
            out.write(compressor.compress(readerToString(in)));
        }
        // close all streams
        in.close();
        in = null;
        out.close();
        out = null;
        if (verbose) {
            log(stats.getFileStats(inFile, outFile, fileType));
        }
    } catch (final IOException ioe) {
        throw new BuildException("I/O Error when compressing file", ioe);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) XmlCompressor(com.googlecode.htmlcompressor.compressor.XmlCompressor) OutputStreamWriter(java.io.OutputStreamWriter) HtmlCompressor(com.googlecode.htmlcompressor.compressor.HtmlCompressor) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) JavaScriptCompressor(com.yahoo.platform.yui.compressor.JavaScriptCompressor) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with CssCompressor

use of com.yahoo.platform.yui.compressor.CssCompressor in project xwiki-platform by xwiki.

the class CssExtension method getCompressor.

@Override
public SxCompressor getCompressor() {
    return new SxCompressor() {

        @Override
        public String compress(String source) {
            try {
                CssCompressor compressor = new CssCompressor(new StringReader(source));
                StringWriter out = new StringWriter();
                compressor.compress(out, -1);
                return out.toString();
            } catch (IOException ex) {
                LOGGER.warn("Exception compressing SSX code", ex);
            }
            return source;
        }
    };
}
Also used : StringWriter(java.io.StringWriter) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 5 with CssCompressor

use of com.yahoo.platform.yui.compressor.CssCompressor in project wombat by PLOS.

the class AssetServiceImpl method compileAsset.

/**
 * Minifies the given asset file and returns the file and its contents.
 *
 * @param uncompiled uncompiled asset file
 * @return File that has been minified.  The file name will contain a hash reflecting the file's contents.
 * @throws IOException
 */
private CompiledAsset compileAsset(AssetType assetType, File uncompiled) throws IOException {
    // Compile to memory instead of a file directly, since we will need the raw bytes
    // in order to generate a hash (which appears in the filename).
    ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
    try (InputStreamReader isr = new InputStreamReader(new FileInputStream(uncompiled));
        OutputStreamWriter osw = new OutputStreamWriter(baos)) {
        if (assetType == AssetType.CSS) {
            CssCompressor compressor = new CssCompressor(isr);
            compressor.compress(osw, -1);
        } else if (assetType == AssetType.JS) {
            JavaScriptCompressor compressor = new JavaScriptCompressor(isr, new JsErrorReporter());
            compressor.compress(osw, -1, true, false, true, false);
        } else {
            throw new IllegalArgumentException("Unexpected asset type " + assetType);
        }
    }
    byte[] contents = baos.toByteArray();
    String contentHash = CacheKey.createContentHash(contents);
    CompiledDigest digest = new CompiledDigest(AssetUrls.COMPILED_NAME_PREFIX + contentHash + assetType.getExtension());
    File file = digest.getFile();
    // Overwrite if the file already exists.
    if (file.exists()) {
        file.delete();
    }
    try (OutputStream os = new FileOutputStream(file)) {
        IOUtils.write(contents, os);
    }
    return new CompiledAsset(digest, contents);
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaScriptCompressor(com.yahoo.platform.yui.compressor.JavaScriptCompressor) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)7 JavaScriptCompressor (com.yahoo.platform.yui.compressor.JavaScriptCompressor)4 StringReader (java.io.StringReader)4 StringWriter (java.io.StringWriter)4 IOException (java.io.IOException)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SourceFile (com.google.javascript.jscomp.SourceFile)1 HtmlCompressor (com.googlecode.htmlcompressor.compressor.HtmlCompressor)1 XmlCompressor (com.googlecode.htmlcompressor.compressor.XmlCompressor)1 CompilationException (io.bit3.jsass.CompilationException)1 Compiler (io.bit3.jsass.Compiler)1 Options (io.bit3.jsass.Options)1 Output (io.bit3.jsass.Output)1 FileContext (io.bit3.jsass.context.FileContext)1 StringContext (io.bit3.jsass.context.StringContext)1 Import (io.bit3.jsass.importer.Import)1 Importer (io.bit3.jsass.importer.Importer)1