author | duke |
Wed, 05 Jul 2017 20:36:16 +0200 | |
changeset 30851 | 1c0a1cee6054 |
parent 28334 | 1633de6070ae |
permissions | -rw-r--r-- |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
1 |
/* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
4 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
10 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
15 |
* accompanied this code). |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
16 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
20 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
23 |
* questions. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
24 |
*/ |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
25 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
26 |
package anttasks; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
27 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
28 |
import java.io.File; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
29 |
import java.util.ArrayList; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
30 |
import java.util.Arrays; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
31 |
import java.util.List; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
32 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
33 |
import propertiesparser.PropertiesParser; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
34 |
import propertiesparser.gen.ClassGenerator; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
35 |
import org.apache.tools.ant.BuildException; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
36 |
import org.apache.tools.ant.DirectoryScanner; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
37 |
import org.apache.tools.ant.Project; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
38 |
import org.apache.tools.ant.taskdefs.MatchingTask; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
39 |
import org.apache.tools.ant.types.Path; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
40 |
import org.apache.tools.ant.types.Resource; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
41 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
42 |
public class PropertiesParserTask extends MatchingTask { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
43 |
public void addSrc(Path src) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
44 |
if (srcDirs == null) |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
45 |
srcDirs = new Path(getProject()); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
46 |
srcDirs.add(src); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
47 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
48 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
49 |
public void setDestDir(File destDir) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
50 |
this.destDir = destDir; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
51 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
52 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
53 |
@Override |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
54 |
public void execute() { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
55 |
List<String> mainOpts = new ArrayList<String>(); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
56 |
int count = 0; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
57 |
for (String dir : srcDirs.list()) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
58 |
File baseDir = getProject().resolveFile(dir); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
59 |
DirectoryScanner s = getDirectoryScanner(baseDir); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
60 |
for (String path : s.getIncludedFiles()) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
61 |
if (path.endsWith(".properties")) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
62 |
File srcFile = new File(baseDir, path); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
63 |
String destPath = |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
64 |
path.substring(0, path.lastIndexOf(File.separator) + 1) + |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
65 |
ClassGenerator.toplevelName(srcFile) + ".java"; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
66 |
File destFile = new File(this.destDir, destPath); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
67 |
File destDir = destFile.getParentFile(); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
68 |
// Arguably, the comparison in the next line should be ">", not ">=" |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
69 |
// but that assumes the resolution of the last modified time is fine |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
70 |
// grained enough; in practice, it is better to use ">=". |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
71 |
if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
72 |
continue; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
73 |
destDir.mkdirs(); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
74 |
mainOpts.add("-compile"); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
75 |
mainOpts.add(srcFile.getPath()); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
76 |
mainOpts.add(destDir.getPath()); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
77 |
count++; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
78 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
79 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
80 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
81 |
if (mainOpts.size() > 0) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
82 |
log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
83 |
PropertiesParser pp = new PropertiesParser(msg -> log(msg, Project.MSG_INFO)); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
84 |
boolean ok = pp.run(mainOpts.toArray(new String[mainOpts.size()])); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
85 |
if (!ok) |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
86 |
throw new BuildException("PropertiesParser failed."); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
87 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
88 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
89 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
90 |
private Path srcDirs; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
91 |
private File destDir; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
92 |
} |