make/tools/anttasks/CompilePropertiesTask.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make/tools/anttasks/CompilePropertiesTask.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,101 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package anttasks;
    1.30 +
    1.31 +import compileproperties.CompileProperties;
    1.32 +
    1.33 +import java.io.File;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.List;
    1.36 +
    1.37 +import org.apache.tools.ant.BuildException;
    1.38 +import org.apache.tools.ant.DirectoryScanner;
    1.39 +import org.apache.tools.ant.Project;
    1.40 +import org.apache.tools.ant.taskdefs.MatchingTask;
    1.41 +
    1.42 +public class CompilePropertiesTask extends MatchingTask {
    1.43 +    public void setSrcDir(File srcDir) {
    1.44 +        this.srcDir = srcDir;
    1.45 +    }
    1.46 +
    1.47 +    public void setDestDir(File destDir) {
    1.48 +        this.destDir = destDir;
    1.49 +    }
    1.50 +
    1.51 +    public void setSuperclass(String superclass) {
    1.52 +        this.superclass = superclass;
    1.53 +    }
    1.54 +
    1.55 +    @Override
    1.56 +    public void execute() {
    1.57 +        CompileProperties.Log log = new CompileProperties.Log() {
    1.58 +            public void error(String msg, Exception e) {
    1.59 +                log(msg, Project.MSG_ERR);
    1.60 +            }
    1.61 +            public void info(String msg) {
    1.62 +                log(msg, Project.MSG_INFO);
    1.63 +            }
    1.64 +            public void verbose(String msg) {
    1.65 +                log(msg, Project.MSG_VERBOSE);
    1.66 +            }
    1.67 +        };
    1.68 +        List<String> mainOpts = new ArrayList<String>();
    1.69 +        int count = 0;
    1.70 +        DirectoryScanner s = getDirectoryScanner(srcDir);
    1.71 +        for (String path: s.getIncludedFiles()) {
    1.72 +            if (path.endsWith(".properties")) {
    1.73 +                String destPath =
    1.74 +                        path.substring(0, path.length() - ".properties".length()) +
    1.75 +                        ".java";
    1.76 +                File srcFile = new File(srcDir, path);
    1.77 +                File destFile = new File(destDir, destPath);
    1.78 +                // Arguably, the comparison in the next line should be ">", not ">="
    1.79 +                // but that assumes the resolution of the last modified time is fine
    1.80 +                // grained enough; in practice, it is better to use ">=".
    1.81 +                if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified())
    1.82 +                    continue;
    1.83 +                destFile.getParentFile().mkdirs();
    1.84 +                mainOpts.add("-compile");
    1.85 +                mainOpts.add(srcFile.getPath());
    1.86 +                mainOpts.add(destFile.getPath());
    1.87 +                mainOpts.add(superclass);
    1.88 +                count++;
    1.89 +            }
    1.90 +        }
    1.91 +        if (mainOpts.size() > 0) {
    1.92 +            log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO);
    1.93 +            CompileProperties cp = new CompileProperties();
    1.94 +            cp.setLog(log);
    1.95 +            boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()]));
    1.96 +            if (!ok)
    1.97 +                throw new BuildException("CompileProperties failed.");
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    private File srcDir;
   1.102 +    private File destDir;
   1.103 +    private String superclass = "java.util.ListResourceBundle";
   1.104 +}

mercurial