|
1 /* |
|
2 * Copyright (c) 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 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; |
|
41 |
|
42 public class PropertiesParserTask extends MatchingTask { |
|
43 public void addSrc(Path src) { |
|
44 if (srcDirs == null) |
|
45 srcDirs = new Path(getProject()); |
|
46 srcDirs.add(src); |
|
47 } |
|
48 |
|
49 public void setDestDir(File destDir) { |
|
50 this.destDir = destDir; |
|
51 } |
|
52 |
|
53 @Override |
|
54 public void execute() { |
|
55 List<String> mainOpts = new ArrayList<String>(); |
|
56 int count = 0; |
|
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); |
|
63 String destPath = |
|
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()) |
|
72 continue; |
|
73 destDir.mkdirs(); |
|
74 mainOpts.add("-compile"); |
|
75 mainOpts.add(srcFile.getPath()); |
|
76 mainOpts.add(destDir.getPath()); |
|
77 count++; |
|
78 } |
|
79 } |
|
80 } |
|
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()])); |
|
85 if (!ok) |
|
86 throw new BuildException("PropertiesParser failed."); |
|
87 } |
|
88 } |
|
89 |
|
90 private Path srcDirs; |
|
91 private File destDir; |
|
92 } |