/******************************************************************************* * * Copyright 2011-2014 Spiffy UI Team * * Licensed 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.spiffyui.build; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileList; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.types.resources.Resources; import org.apache.tools.ant.types.resources.Restrict; import org.apache.tools.ant.types.resources.selectors.Exists; import org.apache.tools.ant.types.resources.selectors.Not; import org.apache.tools.ant.types.resources.selectors.ResourceSelector; import org.apache.tools.ant.util.FileUtils; /** * This is a custom task for a list of GZIPed files from a list of * uncompressed files */ public class GzipListTask extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final ResourceSelector EXISTS = new Exists(); private static final ResourceSelector NOT_EXISTS = new Not(EXISTS); /** * The destination directory for the compressed files */ private File m_destinationDir; /** * Stores a collection of file sets and/or file lists, used to * select multiple files for compilation. */ private ResourceCollection m_rc; /** * Adds the file set of JavaScript files to compile * * @param set the set of files */ public void addFileset(FileSet set) { add(set); } /** * List of files to compile. * * @param list the list of files */ public void addFilelist(FileList list) { add(list); } /** * Sepcify the location of the directory to output the compressed files * * @param dir the output file directory */ public void setDest(File dir) { this.m_destinationDir = dir; } /** * Add an arbitrary ResourceCollection. * @param c the ResourceCollection to add. * @since Ant 1.7 */ public synchronized void add(ResourceCollection c) { if (m_rc == null) { m_rc = c; return; } if (!(m_rc instanceof Resources)) { Resources newRc = new Resources(); newRc.setProject(getProject()); newRc.add(m_rc); m_rc = newRc; } ((Resources) m_rc).add(c); } private ResourceCollection getResources() { if (m_rc == null) { return null; } Restrict noexistRc = new Restrict(); noexistRc.add(NOT_EXISTS); noexistRc.add(m_rc); for (Iterator<Resource> i = noexistRc.iterator(); i.hasNext();) { log(i.next() + " does not exist.", Project.MSG_ERR); } if (m_destinationDir != null) { for (Iterator<Resource> i = m_rc.iterator(); i.hasNext();) { Object o = i.next(); if (o instanceof FileResource) { File f = ((FileResource) o).getFile(); if (FILE_UTILS.fileNameEquals(f, m_destinationDir)) { throw new BuildException("Input file \"" + f + "\" is the same as the output directory."); } } } } Restrict result = new Restrict(); result.add(EXISTS); result.add(m_rc); return result; } /** * Reset state to default. */ public void reset() { m_rc = null; m_destinationDir = null; } /** * Executes this task to run the compiler. * * @exception BuildException * if there isn't enough information to run the GWT compiler */ public void execute() throws BuildException { if (m_destinationDir == null) { throw new BuildException("Must specify an output directory"); } if (getResources() == null) { return; } ResourceCollection rc = getResources(); Iterator<Resource> i = rc.iterator(); ArrayList<File> files = new ArrayList<File>(); try { while (i.hasNext()) { files.add(((FileResource) i.next()).getFile()); } GzipListUtil.zipFileList(files, m_destinationDir); } catch (IOException ioe) { throw new BuildException("Unable to GZIP file list", ioe); } } }