1 /* 2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package anttasks; 27 28 import java.io.File; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.List; 32 33 import compileproperties.CompileProperties; 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.DirectoryScanner; 36 import org.apache.tools.ant.Project; 37 import org.apache.tools.ant.taskdefs.MatchingTask; 38 import org.apache.tools.ant.types.Path; 39 import org.apache.tools.ant.types.Resource; 40 41 public class CompilePropertiesTask extends MatchingTask { addSrc(Path src)42 public void addSrc(Path src) { 43 if (srcDirs == null) 44 srcDirs = new Path(getProject()); 45 srcDirs.add(src); 46 } 47 setDestDir(File destDir)48 public void setDestDir(File destDir) { 49 this.destDir = destDir; 50 } 51 setSuperclass(String superclass)52 public void setSuperclass(String superclass) { 53 this.superclass = superclass; 54 } 55 56 @Override execute()57 public void execute() { 58 CompileProperties.Log log = new CompileProperties.Log() { 59 public void error(String msg, Exception e) { 60 log(msg, Project.MSG_ERR); 61 } 62 public void info(String msg) { 63 log(msg, Project.MSG_INFO); 64 } 65 public void verbose(String msg) { 66 log(msg, Project.MSG_VERBOSE); 67 } 68 }; 69 List<String> mainOpts = new ArrayList<String>(); 70 int count = 0; 71 for (String dir : srcDirs.list()) { 72 File baseDir = getProject().resolveFile(dir); 73 DirectoryScanner s = getDirectoryScanner(baseDir); 74 for (String path: s.getIncludedFiles()) { 75 if (path.endsWith(".properties")) { 76 String destPath = 77 path.substring(0, path.length() - ".properties".length()) + 78 ".java"; 79 File srcFile = new File(baseDir, path); 80 File destFile = new File(destDir, destPath); 81 // Arguably, the comparison in the next line should be ">", not ">=" 82 // but that assumes the resolution of the last modified time is fine 83 // grained enough; in practice, it is better to use ">=". 84 if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) 85 continue; 86 destFile.getParentFile().mkdirs(); 87 mainOpts.add("-compile"); 88 mainOpts.add(srcFile.getPath()); 89 mainOpts.add(destFile.getPath()); 90 mainOpts.add(superclass); 91 count++; 92 } 93 } 94 } 95 if (mainOpts.size() > 0) { 96 log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO); 97 CompileProperties cp = new CompileProperties(); 98 cp.setLog(log); 99 boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()])); 100 if (!ok) 101 throw new BuildException("CompileProperties failed."); 102 } 103 } 104 105 private Path srcDirs; 106 private File destDir; 107 private String superclass = "java.util.ListResourceBundle"; 108 } 109