Return-Path: Delivered-To: apmail-ant-notifications-archive@minotaur.apache.org Received: (qmail 45238 invoked from network); 6 Jun 2009 08:23:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 6 Jun 2009 08:23:46 -0000 Received: (qmail 33106 invoked by uid 500); 6 Jun 2009 08:23:58 -0000 Delivered-To: apmail-ant-notifications-archive@ant.apache.org Received: (qmail 33047 invoked by uid 500); 6 Jun 2009 08:23:58 -0000 Mailing-List: contact notifications-help@ant.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ant.apache.org Delivered-To: mailing list notifications@ant.apache.org Received: (qmail 33038 invoked by uid 99); 6 Jun 2009 08:23:58 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 06 Jun 2009 08:23:58 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 06 Jun 2009 08:23:54 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E283523888C8; Sat, 6 Jun 2009 08:23:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r782206 - in /ant/sandbox/antlibs/compress/trunk: README src/main/org/apache/ant/js/compressor/YUICompressorTask.java Date: Sat, 06 Jun 2009 08:23:33 -0000 To: notifications@ant.apache.org From: kevj@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090606082333.E283523888C8@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kevj Date: Sat Jun 6 08:23:33 2009 New Revision: 782206 URL: http://svn.apache.org/viewvc?rev=782206&view=rev Log: - Refactor to use javascriptcompressor class instead of the YUICompressor Main class - Usage/Examples added to README Modified: ant/sandbox/antlibs/compress/trunk/README ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java Modified: ant/sandbox/antlibs/compress/trunk/README URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/README?rev=782206&r1=782205&r2=782206&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/README (original) +++ ant/sandbox/antlibs/compress/trunk/README Sat Jun 6 08:23:33 2009 @@ -2,3 +2,21 @@ Currently the Yahoo UI Compressor is supported and is required on Ant's classpath. +Example Usage +------------- +# Referencing: + + +# Compressing many javascript files + + + + + + +TODO +---- + +# Make the type parameter work so that the code creates the correct compressor depending on the type +# Make the charset parameter work + Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java?rev=782206&r1=782205&r2=782206&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java (original) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java Sat Jun 6 08:23:33 2009 @@ -1,19 +1,27 @@ package org.apache.ant.js.compressor; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.io.File; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.resources.FileResource; +import org.mozilla.javascript.ErrorReporter; +import org.mozilla.javascript.EvaluatorException; +import com.yahoo.platform.yui.compressor.JavaScriptCompressor; import com.yahoo.platform.yui.compressor.YUICompressor; - public class YUICompressorTask extends Task { private boolean verbose; @@ -29,32 +37,82 @@ private List inputResources = new ArrayList(); public void execute() { - if(mergeFiles) { - Commandline cmd = buildArgs(); - YUICompressor.main(cmd.getArguments()); - } else { - for(Iterator i = inputResources.iterator(); i.hasNext();) { - FileSet fs = (FileSet)i.next(); - for(Iterator j = fs.iterator(); j.hasNext();) { - Commandline cmd = buildArgs(); - FileResource f = (FileResource)j.next(); - if(verbose) { - log("Minifying: "+f.getFile().getAbsolutePath()); - } - cmd.createArgument().setValue("-o"); - if(null != outputPath && outputPath.trim() != "") { - cmd.createArgument().setValue(outputPath + File.separator + f.getFile().getName()); - } else { - cmd.createArgument().setValue(f.getFile().getAbsolutePath()); + try { + if(mergeFiles) { + //TODO + //refactor to remove the commandline building + Commandline cmd = buildArgs(); + YUICompressor.main(cmd.getArguments()); + } else { + for(Iterator i = inputResources.iterator(); i.hasNext();) { + FileSet fs = (FileSet)i.next(); + for(Iterator j = fs.iterator(); j.hasNext();) { + Commandline cmd = buildArgs(); + FileResource f = (FileResource)j.next(); + if(verbose) { + log("Minifying: "+f.getFile().getAbsolutePath()); + } + + //TODO + //get the charset from the property + //check the specified type and create the appropriate compressor + InputStreamReader in = new InputStreamReader(new FileInputStream(f.getFile().getAbsolutePath()), "UTF-8"); + JavaScriptCompressor c = getJavaScriptCompressor(in); + Writer out; + if(null != outputPath && outputPath.trim() != "") { + out = new OutputStreamWriter(new FileOutputStream(outputPath + File.separator + f.getFile().getName())); + } else { + out = new OutputStreamWriter(new FileOutputStream(f.getFile().getAbsolutePath())); + } + + c.compress(out, + (null == getLineBreak() || getLineBreak() == "" ? -1 : Integer.parseInt(getLineBreak())), + isNomunge(), + isVerbose(), + isPreserveSemi(), + isDisableOptimization() + ); } - cmd.createArgument().setValue(f.getFile().getAbsolutePath()); - YUICompressor.main(cmd.getArguments()); - } - } + } + } + } catch (Exception e) { + log("Error occurred processing file "+ e.getMessage()); } } + protected JavaScriptCompressor getJavaScriptCompressor(InputStreamReader in) throws IOException { + + JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() { + + public void warning(String message, String sourceName, + int line, String lineSource, int lineOffset) { + if (line < 0) { + log("\n[WARNING] " + message); + } else { + log("\n[WARNING] " + line + ':' + lineOffset + ':' + message); + } + } + + public void error(String message, String sourceName, + int line, String lineSource, int lineOffset) { + if (line < 0) { + log("\n[ERROR] " + message); + } else { + log("\n[ERROR] " + line + ':' + lineOffset + ':' + message); + } + } + + public EvaluatorException runtimeError(String message, String sourceName, + int line, String lineSource, int lineOffset) { + error(message, sourceName, line, lineSource, lineOffset); + return new EvaluatorException(message); + } + }); + + return compressor; + } + protected Commandline buildArgs() { Commandline cmd = new Commandline(); if(verbose) {