1 package org.codehaus.gmavenplus.groovyworkarounds;
2
3 import org.codehaus.gmavenplus.util.FileUtils;
4
5 import java.io.File;
6 import java.net.URI;
7 import java.util.HashSet;
8 import java.util.Set;
9
10
11 /**
12 * This class exists solely to trick
13 * <a href="http://docs.groovy-lang.org/docs/latest/html/api/org/codehaus/groovy/tools/javac/JavaStubCompilationUnit.html#addSource%28java.io.File%29">JavaStubCompilationUnit.addSource(java.io.File)</a>
14 * into letting us use files that don't end in ".groovy" (as a workaround for <a href="http://jira.codehaus.org/browse/GROOVY-5021">GROOVY-5021</a>).
15 *
16 * @author Keegan Witt
17 */
18 public class DotGroovyFile extends File {
19
20 private static final long serialVersionUID = -3325908173654793431L;
21
22 /**
23 * The file extensions to consider as Groovy files.
24 */
25 private Set<String> scriptExtensions = new HashSet<>();
26
27 /**
28 * Constructs a new DotGroovyFile object with the specified parameters.
29 *
30 * @param pathname Pathname to use to create DotGroovyFile
31 */
32 public DotGroovyFile(final String pathname) {
33 super(pathname);
34 }
35
36 /**
37 * Constructs a new DotGroovyFile object with the specified parameters.
38 *
39 * @param parent Parent pathname to use to create DotGroovyFile
40 * @param child Child pathname to use to create DotGroovyFile
41 */
42 public DotGroovyFile(final String parent, final String child) {
43 super(parent, child);
44 }
45
46 /**
47 * Constructs a new DotGroovyFile object with the specified parameters.
48 *
49 * @param parent Parent file to use to create DotGroovyFile
50 * @param child Child pathname to use to create DotGroovyFile
51 */
52 public DotGroovyFile(final File parent, final String child) {
53 super(parent, child);
54 }
55
56 /**
57 * Constructs a new DotGroovyFile object with the specified parameters.
58 *
59 * @param uri URI to use to create DotGroovyFile
60 */
61 public DotGroovyFile(final URI uri) {
62 super(uri);
63 }
64
65 /**
66 * A convenience constructor to turn a regular file into a DotGroovyFile.
67 *
68 * @param file File to use to create DotGroovyFile
69 */
70 public DotGroovyFile(final File file) {
71 super(file.getAbsolutePath());
72 }
73
74 /**
75 * A method to lie about the file extension and say it is ".groovy".
76 *
77 * @return Filename with forced <i>.groovy</i> extension
78 */
79 @Override
80 public String getName() {
81 if (scriptExtensions != null && !scriptExtensions.isEmpty() && scriptExtensions.contains(FileUtils.getFileExtension(super.getAbsolutePath()))) {
82 return FileUtils.getNameWithoutExtension(super.getName()) + ".groovy";
83 } else {
84 return super.getName();
85 }
86 }
87
88 /**
89 * Gets the script extensions for this Groovy file.
90 *
91 * @return The script extensions for this Groovy file
92 */
93 public Set<String> getScriptExtensions() {
94 return scriptExtensions;
95 }
96
97 /**
98 * Sets the script extensions for this Groovy file.
99 *
100 * @param newScriptExtensions The script extensions to set on this Groovy file
101 * @return This object (for fluent invocation)
102 */
103 public DotGroovyFile setScriptExtensions(final Set<String> newScriptExtensions) {
104 scriptExtensions = newScriptExtensions;
105 return this;
106 }
107
108 }