Author: kevj Date: Sat Jun 6 11:46:05 2009 New Revision: 782238 URL: http://svn.apache.org/viewvc?rev=782238&view=rev Log: -refactor, convenience classes and initial documentation Added: ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html (with props) ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java (with props) ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java (with props) Modified: ant/sandbox/antlibs/compress/trunk/README ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml Modified: ant/sandbox/antlibs/compress/trunk/README URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/README?rev=782238&r1=782237&r2=782238&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/README (original) +++ ant/sandbox/antlibs/compress/trunk/README Sat Jun 6 11:46:05 2009 @@ -8,7 +8,11 @@ # Compressing many javascript files - + Added: ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html?rev=782238&view=auto ============================================================================== --- ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html (added) +++ ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html Sat Jun 6 11:46:05 2009 @@ -0,0 +1,92 @@ + + + + + +Yahoo! UI Compressor Task + + + + + +

Compressor

+

Description

+

Compresses or 'minifies' JavaScript or CSS files using the +Yahoo! UI Compressor library

+

Note: This task depends on external libraries not included in the Ant distribution.

+

Installation

+

Download the Yahoo! UI Compressor distribution +and copy the yuicompressor-n.n.n.jar to $ANT_HOME/lib; ~/.ant/lib or the ant $CLASSPATH. +Also take a copy of this antlib and place in the same location.

+

Utilise the antlib by referencing it: +

+<project name="compress-test" basedir="." 
+         default="all" xmlns:compress="antlib:org.apache.ant.js.compressor">
+
+

+ +

Parameters

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
typeThe type of files to compress valid options are js or cssYES
charsetThe character set encoding of the input fileNO [defaults to UTF-8 if not specified]
linebreakThe column to break at in the output fileNO [defaults to never breaking lines]
verboseSets the output of the task to display all warnings while minifyingNO [defaults to no]
+ +

Parameters specified as nested elements

+

fileset

+

FileSets are used to select +sets of files to copy.

+ +

Examples

+
  
+<compress:yui.compressor 
+  verbose="true" 
+  outputPath="${outputPath}"
+  linebreak="80"
+  type="js">
+  
+  <fileset dir="${testscript.dir}">
+    <include name="*.js"/>
+  </fileset>
+</compress:yui.compressor>
+
+

Minifies all javascript files found in the fileset. In the output file lines are broken after 80 chars.

+ + \ No newline at end of file Propchange: ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml?rev=782238&r1=782237&r2=782238&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml (original) +++ ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml Sat Jun 6 11:46:05 2009 @@ -19,7 +19,6 @@ - @@ -28,14 +27,11 @@ - - - - - - - - + @@ -46,10 +42,23 @@ + + + + + + + + + + + + + - +
\ No newline at end of file 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=782238&r1=782237&r2=782238&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 11:46:05 2009 @@ -1,19 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ package org.apache.ant.js.compressor; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.apache.tools.ant.BuildException; 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; @@ -22,8 +41,12 @@ import com.yahoo.platform.yui.compressor.CssCompressor; import com.yahoo.platform.yui.compressor.JavaScriptCompressor; -import com.yahoo.platform.yui.compressor.YUICompressor; +/** + * Base task for wrapping the Yahoo! UI compressor library + * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH + * @author kevj@apache.org + */ public class YUICompressorTask extends Task { private boolean verbose; @@ -33,62 +56,89 @@ private boolean preserveSemi; private boolean disableOptimization; private String charset; - private String output; private String outputPath; - private boolean mergeFiles; private List inputResources = new ArrayList(); - + public void execute() { 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();) { - FileResource f = (FileResource)j.next(); - if(verbose) { - log("Minifying: "+f.getFile().getAbsolutePath()); - } - //Ensure charset is set - if(null == charset || charset.trim() == "") { - charset = "UTF-8"; - } - InputStreamReader in = new InputStreamReader(new FileInputStream(f.getFile().getAbsolutePath()), charset); - 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())); - } - - if(null != type && type.trim() != "" && "js".equals(type)) { - JavaScriptCompressor c = getJavaScriptCompressor(in); - c.compress(out, - (null == getLineBreak() || getLineBreak() == "" ? -1 : Integer.parseInt(getLineBreak())), - isNomunge(), - isVerbose(), - isPreserveSemi(), - isDisableOptimization() - ); - } else if(null != type && type != "" && "css".equals(type)) { - CssCompressor c = getCssCompressor(in); - c.compress(out, (null == getLineBreak() || getLineBreak() == "" ? -1 : Integer.parseInt(getLineBreak()))); - } - in.close(); - out.close(); - } - - } - } + validate(); + for(Iterator i = inputResources.iterator(); i.hasNext();) { + FileSet fs = (FileSet)i.next(); + for(Iterator j = fs.iterator(); j.hasNext();) { + FileResource f = (FileResource)j.next(); + minify(f); + } + } } catch (Exception e) { - log("Error occurred processing file "+ e.getMessage()); + log("Error occurred processing file "); + e.printStackTrace(); + } + } + + private void validate() throws BuildException { + if(null == type || "".equals(type)) { + throw new BuildException("Type property must be specified "); + } + if(null == charset || "".equals(charset)) { + setCharset("UTF-8"); + log("Charset not set; using [UTF-8]"); + } + if(null == lineBreak || "".equals(lineBreak)) { + setLineBreak("-1"); + log("Linebreak position not set; using [-1]"); + } else if (lineBreak.matches("\\D+")) { + throw new BuildException("Linebreak must be an integer"); } } + /** + * @param f the FileResource to compress + * @throws UnsupportedEncodingException + * @throws FileNotFoundException + * @throws IOException + */ + private void minify(FileResource f) throws UnsupportedEncodingException, + FileNotFoundException, IOException { + + Reader in = new InputStreamReader(new FileInputStream(f.getFile().getAbsolutePath()), charset); + Writer out = getWriter(f); + if(verbose) { + log("Minifying: "+f.getFile().getAbsolutePath()); + } + + if("js".equals(getType())) { + JavaScriptCompressor c = getJavaScriptCompressor(in); + //in case we are overwriting the input file, ensure that it's closed first + in.close(); + in = null; + c.compress(out, + Integer.parseInt(getLineBreak()), + isNomunge(), + isVerbose(), + isPreserveSemi(), + isDisableOptimization() + ); + out.close(); + } else if("css".equals(getType())) { + CssCompressor c = getCssCompressor(in); + //in case we are overwriting the input file, ensure that it's closed first + in.close(); + in = null; + c.compress(out, Integer.parseInt(getLineBreak())); + out.close(); + } + } + + private Writer getWriter(FileResource f) throws IOException { + Writer out; + out = new OutputStreamWriter( + null != getOutputPath() && getOutputPath().trim() != "" ? + new FileOutputStream(getOutputPath() + File.separator + f.getFile().getName()) : + f.getOutputStream() + ); + return out; + } + protected JavaScriptCompressor getJavaScriptCompressor(Reader in) throws IOException { JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() { @@ -125,51 +175,6 @@ return new CssCompressor(in); } - protected Commandline buildArgs() { - Commandline cmd = new Commandline(); - if(verbose) { - cmd.createArgument().setValue("-v"); - } - if(nomunge) { - cmd.createArgument().setValue("--nomunge"); - } - if(preserveSemi) { - cmd.createArgument().setValue("--preserve-semi"); - } - if(disableOptimization) { - cmd.createArgument().setValue("--disable-optimizations"); - } - if(null != lineBreak && lineBreak.trim() != "") { - cmd.createArgument().setValue("--line-break"); - cmd.createArgument().setValue(lineBreak); - } - if(null != type && type.trim() != "") { - cmd.createArgument().setValue("--type"); - cmd.createArgument().setValue(type); - } - if(null != charset && charset.trim() != "") { - cmd.createArgument().setValue("--charset"); - cmd.createArgument().setValue(charset); - } - if(null != output && output.trim() != "" && mergeFiles) { - cmd.createArgument().setValue("-o"); - cmd.createArgument().setValue(output); - } - - if(mergeFiles) { - for(Iterator i = inputResources.iterator(); i.hasNext();) { - FileSet fs = (FileSet)i.next(); - for(Iterator j = fs.iterator(); j.hasNext();) { - FileResource f = (FileResource)j.next(); - cmd.createArgument().setValue(f.getFile().getAbsolutePath()); - } - - } - } - log("args: "+cmd.describeCommand()); - return cmd; - } - public boolean isVerbose() { return verbose; } @@ -226,14 +231,6 @@ this.charset = charset; } - public String getOutput() { - return output; - } - - public void setOutput(String output) { - this.output = output; - } - public String getOutputPath() { return outputPath; } @@ -249,12 +246,4 @@ public void add(ResourceCollection res) { inputResources.add(res); } - - public boolean isMergeFiles() { - return mergeFiles; - } - - public void setMergeFiles(boolean m) { - mergeFiles = m; - } -} +} \ No newline at end of file Added: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java?rev=782238&view=auto ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java (added) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java Sat Jun 6 11:46:05 2009 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.js.compressor; + +/** + * Task for compressing CSS files using the Yahoo! UI compressor + * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH + * @author kevj@apache.org + */ +public class YUICssCompressor extends YUICompressorTask { + + public void execute() { + super.setType("css"); + super.execute(); + } +} \ No newline at end of file Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java?rev=782238&view=auto ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java (added) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java Sat Jun 6 11:46:05 2009 @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.js.compressor; + +/** + * Task for compressing JavaScript files using the Yahoo! UI compressor + * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH + * @author kevj@apache.org + */ +public class YUIJsCompressor extends YUICompressorTask { + + public void execute() { + super.setType("js"); + super.execute(); + } +} \ No newline at end of file Propchange: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml?rev=782238&r1=782237&r2=782238&view=diff ============================================================================== --- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml (original) +++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml Sat Jun 6 11:46:05 2009 @@ -1,6 +1,33 @@ + + + + + + \ No newline at end of file