langtools/make/tools/CompileProperties/CompilePropertiesTask.java
changeset 12085 ce2780cb121f
parent 12084 b367473ef0f1
child 12086 734243132d25
equal deleted inserted replaced
12084:b367473ef0f1 12085:ce2780cb121f
     1 /*
       
     2  * Copyright (c) 2007, 2010, 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 import java.io.File;
       
    27 import java.util.ArrayList;
       
    28 import java.util.List;
       
    29 
       
    30 import org.apache.tools.ant.BuildException;
       
    31 import org.apache.tools.ant.DirectoryScanner;
       
    32 import org.apache.tools.ant.Project;
       
    33 import org.apache.tools.ant.taskdefs.MatchingTask;
       
    34 
       
    35 public class CompilePropertiesTask extends MatchingTask {
       
    36     public void setSrcDir(File srcDir) {
       
    37         this.srcDir = srcDir;
       
    38     }
       
    39 
       
    40     public void setDestDir(File destDir) {
       
    41         this.destDir = destDir;
       
    42     }
       
    43 
       
    44     public void setSuperclass(String superclass) {
       
    45         this.superclass = superclass;
       
    46     }
       
    47 
       
    48     @Override
       
    49     public void execute() {
       
    50         CompileProperties.Log log = new CompileProperties.Log() {
       
    51             public void error(String msg, Exception e) {
       
    52                 log(msg, Project.MSG_ERR);
       
    53             }
       
    54             public void info(String msg) {
       
    55                 log(msg, Project.MSG_INFO);
       
    56             }
       
    57             public void verbose(String msg) {
       
    58                 log(msg, Project.MSG_VERBOSE);
       
    59             }
       
    60         };
       
    61         List<String> mainOpts = new ArrayList<String>();
       
    62         int count = 0;
       
    63         DirectoryScanner s = getDirectoryScanner(srcDir);
       
    64         for (String path: s.getIncludedFiles()) {
       
    65             if (path.endsWith(".properties")) {
       
    66                 String destPath =
       
    67                         path.substring(0, path.length() - ".properties".length()) +
       
    68                         ".java";
       
    69                 File srcFile = new File(srcDir, path);
       
    70                 File destFile = new File(destDir, destPath);
       
    71                 // Arguably, the comparison in the next line should be ">", not ">="
       
    72                 // but that assumes the resolution of the last modified time is fine
       
    73                 // grained enough; in practice, it is better to use ">=".
       
    74                 if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified())
       
    75                     continue;
       
    76                 destFile.getParentFile().mkdirs();
       
    77                 mainOpts.add("-compile");
       
    78                 mainOpts.add(srcFile.getPath());
       
    79                 mainOpts.add(destFile.getPath());
       
    80                 mainOpts.add(superclass);
       
    81                 count++;
       
    82             }
       
    83         }
       
    84         if (mainOpts.size() > 0) {
       
    85             log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO);
       
    86             CompileProperties cp = new CompileProperties();
       
    87             cp.setLog(log);
       
    88             boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()]));
       
    89             if (!ok)
       
    90                 throw new BuildException("CompileProperties failed.");
       
    91         }
       
    92     }
       
    93 
       
    94     private File srcDir;
       
    95     private File destDir;
       
    96     private String superclass = "java.util.ListResourceBundle";
       
    97 }