/******************************************************************************* * * 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 the Ant custom task for generating properties files from HTML files */ public class HTMLPropertiesTask 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 of the stream. If <code>null</code>, the system * console is used. */ private File m_destinationFile; /** * The package name for the generated Java file */ private String m_packageName; /** * 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); } /** * Specify the location of the properties output file. This is required. * * @param file the output file name */ public void setProperties(File file) { m_destinationFile = file; } /** * Specify the package name for the generated Java file. If this field * isn't specified then this task will only generate the properties file * * @param packageName The package name for the generated Java file */ public void setPackage(String packageName) { m_packageName = packageName; } /** * 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_destinationFile != 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_destinationFile)) { throw new BuildException("Input file \"" + f + "\" is the same as the output file."); } } } } Restrict result = new Restrict(); result.add(EXISTS); result.add(m_rc); return result; } /** * Reset state to default. */ public void reset() { m_rc = null; m_destinationFile = 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_destinationFile == null) { throw new BuildException("Must specify a Properties file output"); } if (getResources() == null) { return; } try { ArrayList<File> files = new ArrayList<File>(); ResourceCollection rc = getResources(); Iterator<Resource> i = rc.iterator(); while (i.hasNext()) { files.add(((FileResource) i.next()).getFile()); } HTMLPropertiesUtil.generatePropertiesFiles(files, m_destinationFile, m_packageName); } catch (IOException ioe) { throw new BuildException("Unable to generate HTML properties", ioe); } } }