CompilePropertiesTask.java revision 1223:7245999a0075
1290001Sglebius/*
2290001Sglebius * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3290001Sglebius * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4290001Sglebius *
5290001Sglebius * This code is free software; you can redistribute it and/or modify it
6290001Sglebius * under the terms of the GNU General Public License version 2 only, as
7290001Sglebius * published by the Free Software Foundation.  Oracle designates this
8290001Sglebius * particular file as subject to the "Classpath" exception as provided
9290001Sglebius * by Oracle in the LICENSE file that accompanied this code.
10290001Sglebius *
11290001Sglebius * This code is distributed in the hope that it will be useful, but WITHOUT
12290001Sglebius * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13290001Sglebius * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14290001Sglebius * version 2 for more details (a copy is included in the LICENSE file that
15290001Sglebius * accompanied this code).
16290001Sglebius *
17290001Sglebius * You should have received a copy of the GNU General Public License version
18290001Sglebius * 2 along with this work; if not, write to the Free Software Foundation,
19290001Sglebius * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20290001Sglebius *
21290001Sglebius * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22290001Sglebius * or visit www.oracle.com if you need additional information or have any
23290001Sglebius * questions.
24290001Sglebius */
25290001Sglebius
26290001Sglebiuspackage anttasks;
27290001Sglebius
28290001Sglebiusimport compileproperties.CompileProperties;
29290001Sglebius
30290001Sglebiusimport java.io.File;
31290001Sglebiusimport java.util.ArrayList;
32290001Sglebiusimport java.util.List;
33290001Sglebius
34290001Sglebiusimport org.apache.tools.ant.BuildException;
35290001Sglebiusimport org.apache.tools.ant.DirectoryScanner;
36290001Sglebiusimport org.apache.tools.ant.Project;
37290001Sglebiusimport org.apache.tools.ant.taskdefs.MatchingTask;
38290001Sglebius
39290001Sglebiuspublic class CompilePropertiesTask extends MatchingTask {
40290001Sglebius    public void setSrcDir(File srcDir) {
41290001Sglebius        this.srcDir = srcDir;
42290001Sglebius    }
43290001Sglebius
44290001Sglebius    public void setDestDir(File destDir) {
45290001Sglebius        this.destDir = destDir;
46290001Sglebius    }
47290001Sglebius
48290001Sglebius    public void setSuperclass(String superclass) {
49290001Sglebius        this.superclass = superclass;
50290001Sglebius    }
51290001Sglebius
52290001Sglebius    @Override
53290001Sglebius    public void execute() {
54290001Sglebius        CompileProperties.Log log = new CompileProperties.Log() {
55290001Sglebius            public void error(String msg, Exception e) {
56290001Sglebius                log(msg, Project.MSG_ERR);
57290001Sglebius            }
58290001Sglebius            public void info(String msg) {
59290001Sglebius                log(msg, Project.MSG_INFO);
60290001Sglebius            }
61290001Sglebius            public void verbose(String msg) {
62290001Sglebius                log(msg, Project.MSG_VERBOSE);
63290001Sglebius            }
64290001Sglebius        };
65290001Sglebius        List<String> mainOpts = new ArrayList<String>();
66290001Sglebius        int count = 0;
67290001Sglebius        DirectoryScanner s = getDirectoryScanner(srcDir);
68290001Sglebius        for (String path: s.getIncludedFiles()) {
69290001Sglebius            if (path.endsWith(".properties")) {
70290001Sglebius                String destPath =
71290001Sglebius                        path.substring(0, path.length() - ".properties".length()) +
72290001Sglebius                        ".java";
73290001Sglebius                File srcFile = new File(srcDir, path);
74290001Sglebius                File destFile = new File(destDir, destPath);
75290001Sglebius                // Arguably, the comparison in the next line should be ">", not ">="
76290001Sglebius                // but that assumes the resolution of the last modified time is fine
77290001Sglebius                // grained enough; in practice, it is better to use ">=".
78290001Sglebius                if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified())
79290001Sglebius                    continue;
80290001Sglebius                destFile.getParentFile().mkdirs();
81290001Sglebius                mainOpts.add("-compile");
82290001Sglebius                mainOpts.add(srcFile.getPath());
83290001Sglebius                mainOpts.add(destFile.getPath());
84290001Sglebius                mainOpts.add(superclass);
85290001Sglebius                count++;
86290001Sglebius            }
87290001Sglebius        }
88290001Sglebius        if (mainOpts.size() > 0) {
89290001Sglebius            log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO);
90290001Sglebius            CompileProperties cp = new CompileProperties();
91290001Sglebius            cp.setLog(log);
92290001Sglebius            boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()]));
93290001Sglebius            if (!ok)
94290001Sglebius                throw new BuildException("CompileProperties failed.");
95290001Sglebius        }
96290001Sglebius    }
97290001Sglebius
98290001Sglebius    private File srcDir;
99290001Sglebius    private File destDir;
100290001Sglebius    private String superclass = "java.util.ListResourceBundle";
101290001Sglebius}
102290001Sglebius