1 /*
2 * Copyright (C) 2011 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.codehaus.gmavenplus.groovyworkarounds;
18
19 import org.codehaus.gmavenplus.util.FileUtils;
20
21 import java.io.File;
22 import java.net.URI;
23 import java.util.HashSet;
24 import java.util.Set;
25
26
27 /**
28 * This class exists solely to trick
29 * <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>
30 * 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>).
31 *
32 * @author Keegan Witt
33 */
34 public class DotGroovyFile extends File {
35
36 private static final long serialVersionUID = -3325908173654793431L;
37
38 /**
39 * The file extensions to consider as Groovy files.
40 */
41 private Set<String> scriptExtensions = new HashSet<>();
42
43 /**
44 * Constructs a new DotGroovyFile object with the specified parameters.
45 *
46 * @param pathname Pathname to use to create DotGroovyFile
47 */
48 public DotGroovyFile(final String pathname) {
49 super(pathname);
50 }
51
52 /**
53 * Constructs a new DotGroovyFile object with the specified parameters.
54 *
55 * @param parent Parent pathname to use to create DotGroovyFile
56 * @param child Child pathname to use to create DotGroovyFile
57 */
58 public DotGroovyFile(final String parent, final String child) {
59 super(parent, child);
60 }
61
62 /**
63 * Constructs a new DotGroovyFile object with the specified parameters.
64 *
65 * @param parent Parent file to use to create DotGroovyFile
66 * @param child Child pathname to use to create DotGroovyFile
67 */
68 public DotGroovyFile(final File parent, final String child) {
69 super(parent, child);
70 }
71
72 /**
73 * Constructs a new DotGroovyFile object with the specified parameters.
74 *
75 * @param uri URI to use to create DotGroovyFile
76 */
77 public DotGroovyFile(final URI uri) {
78 super(uri);
79 }
80
81 /**
82 * A convenience constructor to turn a regular file into a DotGroovyFile.
83 *
84 * @param file File to use to create DotGroovyFile
85 */
86 public DotGroovyFile(final File file) {
87 super(file.getAbsolutePath());
88 }
89
90 /**
91 * A method to lie about the file extension and say it is ".groovy".
92 *
93 * @return Filename with forced <i>.groovy</i> extension
94 */
95 @Override
96 public String getName() {
97 if (scriptExtensions != null && !scriptExtensions.isEmpty() && scriptExtensions.contains(FileUtils.getFileExtension(super.getAbsolutePath()))) {
98 return FileUtils.getNameWithoutExtension(super.getName()) + ".groovy";
99 } else {
100 return super.getName();
101 }
102 }
103
104 /**
105 * Gets the script extensions for this Groovy file.
106 *
107 * @return The script extensions for this Groovy file
108 */
109 public Set<String> getScriptExtensions() {
110 return scriptExtensions;
111 }
112
113 /**
114 * Sets the script extensions for this Groovy file.
115 *
116 * @param newScriptExtensions The script extensions to set on this Groovy file
117 * @return This object (for fluent invocation)
118 */
119 public DotGroovyFile setScriptExtensions(final Set<String> newScriptExtensions) {
120 scriptExtensions = newScriptExtensions;
121 return this;
122 }
123
124 }