Search in sources :

Example 1 with XmlCompressor

use of com.googlecode.htmlcompressor.compressor.XmlCompressor 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)

Aggregations

HtmlCompressor (com.googlecode.htmlcompressor.compressor.HtmlCompressor)1 XmlCompressor (com.googlecode.htmlcompressor.compressor.XmlCompressor)1 CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)1 JavaScriptCompressor (com.yahoo.platform.yui.compressor.JavaScriptCompressor)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 BuildException (org.apache.tools.ant.BuildException)1