duke@1: /* jjh@1305: * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: ohrstrom@1224: package anttasks; ohrstrom@1224: ohrstrom@1224: import compileproperties.CompileProperties; ohrstrom@1224: duke@1: import java.io.File; duke@1: import java.util.ArrayList; duke@1: import java.util.List; duke@1: duke@1: import org.apache.tools.ant.BuildException; duke@1: import org.apache.tools.ant.DirectoryScanner; duke@1: import org.apache.tools.ant.Project; duke@1: import org.apache.tools.ant.taskdefs.MatchingTask; duke@1: duke@1: public class CompilePropertiesTask extends MatchingTask { duke@1: public void setSrcDir(File srcDir) { duke@1: this.srcDir = srcDir; duke@1: } duke@1: duke@1: public void setDestDir(File destDir) { duke@1: this.destDir = destDir; duke@1: } duke@1: duke@1: public void setSuperclass(String superclass) { duke@1: this.superclass = superclass; duke@1: } duke@1: jjg@465: @Override duke@1: public void execute() { duke@1: CompileProperties.Log log = new CompileProperties.Log() { duke@1: public void error(String msg, Exception e) { duke@1: log(msg, Project.MSG_ERR); duke@1: } duke@1: public void info(String msg) { duke@1: log(msg, Project.MSG_INFO); duke@1: } duke@1: public void verbose(String msg) { duke@1: log(msg, Project.MSG_VERBOSE); duke@1: } duke@1: }; duke@1: List mainOpts = new ArrayList(); duke@1: int count = 0; duke@1: DirectoryScanner s = getDirectoryScanner(srcDir); duke@1: for (String path: s.getIncludedFiles()) { duke@1: if (path.endsWith(".properties")) { duke@1: String destPath = duke@1: path.substring(0, path.length() - ".properties".length()) + duke@1: ".java"; duke@1: File srcFile = new File(srcDir, path); duke@1: File destFile = new File(destDir, destPath); duke@1: // Arguably, the comparison in the next line should be ">", not ">=" duke@1: // but that assumes the resolution of the last modified time is fine duke@1: // grained enough; in practice, it is better to use ">=". duke@1: if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) duke@1: continue; duke@1: destFile.getParentFile().mkdirs(); duke@1: mainOpts.add("-compile"); duke@1: mainOpts.add(srcFile.getPath()); duke@1: mainOpts.add(destFile.getPath()); duke@1: mainOpts.add(superclass); duke@1: count++; duke@1: } duke@1: } duke@1: if (mainOpts.size() > 0) { duke@1: log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO); duke@1: CompileProperties cp = new CompileProperties(); duke@1: cp.setLog(log); jjg@465: boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()])); duke@1: if (!ok) duke@1: throw new BuildException("CompileProperties failed."); duke@1: } duke@1: } duke@1: duke@1: private File srcDir; duke@1: private File destDir; duke@1: private String superclass = "java.util.ListResourceBundle"; duke@1: }