View Javadoc
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   * @since 1.0-beta-1
34   */
35  public class DotGroovyFile extends File {
36  
37      private static final long serialVersionUID = -3325908173654793431L;
38  
39      /**
40       * The file extensions to consider as Groovy files.
41       */
42      private Set<String> scriptExtensions = new HashSet<>();
43  
44      /**
45       * Constructs a new DotGroovyFile object with the specified parameters.
46       *
47       * @param pathname Pathname to use to create DotGroovyFile
48       */
49      public DotGroovyFile(final String pathname) {
50          super(pathname);
51      }
52  
53      /**
54       * Constructs a new DotGroovyFile object with the specified parameters.
55       *
56       * @param parent Parent pathname to use to create DotGroovyFile
57       * @param child  Child pathname to use to create DotGroovyFile
58       */
59      public DotGroovyFile(final String parent, final String child) {
60          super(parent, child);
61      }
62  
63      /**
64       * Constructs a new DotGroovyFile object with the specified parameters.
65       *
66       * @param parent Parent file to use to create DotGroovyFile
67       * @param child  Child pathname to use to create DotGroovyFile
68       */
69      public DotGroovyFile(final File parent, final String child) {
70          super(parent, child);
71      }
72  
73      /**
74       * Constructs a new DotGroovyFile object with the specified parameters.
75       *
76       * @param uri URI to use to create DotGroovyFile
77       */
78      public DotGroovyFile(final URI uri) {
79          super(uri);
80      }
81  
82      /**
83       * A convenience constructor to turn a regular file into a DotGroovyFile.
84       *
85       * @param file File to use to create DotGroovyFile
86       */
87      public DotGroovyFile(final File file) {
88          super(file.getAbsolutePath());
89      }
90  
91      /**
92       * A method to lie about the file extension and say it is ".groovy".
93       *
94       * @return Filename with forced <i>.groovy</i> extension
95       */
96      @Override
97      public String getName() {
98          if (scriptExtensions != null && !scriptExtensions.isEmpty() && scriptExtensions.contains(FileUtils.getFileExtension(super.getAbsolutePath()))) {
99              return FileUtils.getNameWithoutExtension(super.getName()) + ".groovy";
100         } else {
101             return super.getName();
102         }
103     }
104 
105     /**
106      * Gets the script extensions for this Groovy file.
107      *
108      * @return The script extensions for this Groovy file
109      */
110     public Set<String> getScriptExtensions() {
111         return scriptExtensions;
112     }
113 
114     /**
115      * Sets the script extensions for this Groovy file.
116      *
117      * @param newScriptExtensions The script extensions to set on this Groovy file
118      * @return This object (for fluent invocation)
119      */
120     public DotGroovyFile setScriptExtensions(final Set<String> newScriptExtensions) {
121         scriptExtensions = newScriptExtensions;
122         return this;
123     }
124 
125 }