2
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
29
import java.util.ArrayList;
30
import java.util.Arrays;
33
import propertiesparser.PropertiesParser;
34
import propertiesparser.gen.ClassGenerator;
35
import org.apache.tools.ant.BuildException;
36
import org.apache.tools.ant.DirectoryScanner;
37
import org.apache.tools.ant.Project;
38
import org.apache.tools.ant.taskdefs.MatchingTask;
39
import org.apache.tools.ant.types.Path;
40
import org.apache.tools.ant.types.Resource;
42
public class PropertiesParserTask extends MatchingTask {
43
public void addSrc(Path src) {
45
srcDirs = new Path(getProject());
49
public void setDestDir(File destDir) {
50
this.destDir = destDir;
54
public void execute() {
55
List<String> mainOpts = new ArrayList<String>();
57
for (String dir : srcDirs.list()) {
58
File baseDir = getProject().resolveFile(dir);
59
DirectoryScanner s = getDirectoryScanner(baseDir);
60
for (String path : s.getIncludedFiles()) {
61
if (path.endsWith(".properties")) {
62
File srcFile = new File(baseDir, path);
64
path.substring(0, path.lastIndexOf(File.separator) + 1) +
65
ClassGenerator.toplevelName(srcFile) + ".java";
66
File destFile = new File(this.destDir, destPath);
67
File destDir = destFile.getParentFile();
68
// Arguably, the comparison in the next line should be ">", not ">="
69
// but that assumes the resolution of the last modified time is fine
70
// grained enough; in practice, it is better to use ">=".
71
if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified())
74
mainOpts.add("-compile");
75
mainOpts.add(srcFile.getPath());
76
mainOpts.add(destDir.getPath());
81
if (mainOpts.size() > 0) {
82
log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO);
83
PropertiesParser pp = new PropertiesParser(msg -> log(msg, Project.MSG_INFO));
84
boolean ok = pp.run(mainOpts.toArray(new String[mainOpts.size()]));
86
throw new BuildException("PropertiesParser failed.");