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:
+<project name="x" basedir="." default="y"
xmlns:compress="antlib:org.apache.ant.js.compressor">
+
+# Compressing many javascript files
+<compress:yui.compressor verbose="true" outputPath="${outputPath}" type="js">
+ <fileset dir="${testscript.dir}">
+ <include name="*.js"/>
+ </fileset>
+</compress:yui.compressor>
+
+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) {