View Javadoc
1   package org.codehaus.mojo.natives;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.codehaus.plexus.util.DirectoryScanner;
8   
9   /*
10   * The MIT License
11   * 
12   * Copyright (c) 2004, The Codehaus
13   * 
14   * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
15   * associated documentation files (the "Software"), to deal in the Software without restriction,
16   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
17   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
18   * furnished to do so, subject to the following conditions:
19   * 
20   * The above copyright notice and this permission notice shall be included in all copies or
21   * substantial portions of the Software.
22   * 
23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
24   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28   */
29  
30  /**
31   * Compilable list of source file in a directory
32   * 
33   * @author dantran@gmail.com
34   * @description
35   * @version $Id$
36   */
37  public class NativeSources
38  {
39      private File directory;
40  
41      private String[] fileNames = new String[0];
42  
43      private boolean dependencyAnalysisParticipation = true;
44  
45      /**
46       * Hint Maven to bundle up all file under 'directory' and deploy
47       */
48      private boolean deployable = false;
49  
50      /**
51       * ANT expression to get all compilable source files
52       */
53      private String[] includes;
54  
55      /**
56       * ANT expression for source exclusions
57       */
58      private String[] excludes;
59  
60      public NativeSources()
61      {
62  
63      }
64  
65      /**
66       * @return
67       */
68      public File getDirectory()
69      {
70          return this.directory;
71      }
72  
73      /**
74       * @param directory
75       */
76      public void setDirectory( File directory )
77      {
78          this.directory = directory;
79      }
80  
81      /**
82       * @return
83       */
84      public String[] getFileNames()
85      {
86          return this.fileNames;
87      }
88  
89      /**
90       * @param fileNames
91       */
92      public void setFileNames( String[] fileNames )
93      {
94          if ( fileNames == null )
95          {
96              this.fileNames = new String[0];
97          }
98  
99          this.fileNames = fileNames;
100     }
101 
102     /**
103      * @return
104      */
105     public String[] getIncludes()
106     {
107         return this.includes;
108     }
109 
110     /**
111      * @param includes
112      */
113     public void setIncludes( String[] includes )
114     {
115         this.includes = includes;
116     }
117 
118     /**
119      * @return
120      */
121     public String[] getExcludes()
122     {
123         return this.excludes;
124     }
125 
126     /**
127      * @param excludes
128      */
129     public void setExcludes( String[] excludes )
130     {
131         this.excludes = excludes;
132     }
133 
134     /**
135      * @return
136      */
137     public boolean getDependencyAnalysisParticipation()
138     {
139         return this.dependencyAnalysisParticipation;
140     }
141 
142     /**
143      * @param flag
144      */
145     public void setDependencyAnalysisParticipation( boolean flag )
146     {
147         this.dependencyAnalysisParticipation = flag;
148     }
149 
150     public boolean isDeployable()
151     {
152         return deployable;
153     }
154 
155     public void setDeployable( boolean deployable )
156     {
157         this.deployable = deployable;
158     }
159 
160     // //////////////////////////////////////////////////////////////////////////
161 
162     public List getFiles()
163     {
164         String[] filePaths = new String[0];
165 
166         if ( includes != null || excludes != null )
167         {
168             DirectoryScanner scanner = new DirectoryScanner();
169             scanner.setBasedir( this.directory );
170             scanner.setIncludes( includes );
171             scanner.setExcludes( excludes );
172             scanner.addDefaultExcludes();
173 
174             scanner.scan();
175 
176             filePaths = scanner.getIncludedFiles();
177         }
178 
179         List files = new ArrayList( filePaths.length + this.fileNames.length );
180         for ( int i = 0; i < filePaths.length; ++i )
181         {
182             files.add( new File( this.directory, filePaths[i] ) );
183         }
184 
185         // remove duplicate files
186         for ( int i = 0; i < this.fileNames.length; ++i )
187         {
188             File file = new File( this.directory, this.fileNames[i] );
189 
190             boolean found = false;
191 
192             for ( int k = 0; k < filePaths.length; ++k )
193             {
194                 if ( files.get( k ).equals( file ) )
195                 {
196                     found = true;
197                     break;
198                 }
199             }
200 
201             if ( !found )
202             {
203                 files.add( file );
204             }
205         }
206 
207         return files;
208 
209     }
210 
211     // ///////////////////////////////////////////////////////////////////////
212     // HELPERS
213     // ///////////////////////////////////////////////////////////////////////
214 
215     /**
216      * Helper to get all source files in a Array of NativeSources
217      * 
218      * @param sources
219      * @return
220      */
221     public static File[] getAllSourceFiles( NativeSources[] sources )
222     {
223         if ( sources == null )
224         {
225             return new File[0];
226         }
227 
228         List sourceFiles = new ArrayList();
229 
230         for ( int i = 0; i < sources.length; ++i )
231         {
232             sourceFiles.addAll( sources[i].getFiles() );
233         }
234 
235         return (File[]) sourceFiles.toArray( new File[sourceFiles.size()] );
236     }
237 
238     public static File[] getIncludePaths( NativeSources[] sources )
239     {
240         if ( sources == null )
241         {
242             return new File[0];
243         }
244 
245         List list = new ArrayList();
246 
247         for ( int i = 0; i < sources.length; ++i )
248         {
249             if ( sources[i].getDependencyAnalysisParticipation() )
250             {
251                 list.add( sources[i].getDirectory() );
252             }
253         }
254 
255         return (File[]) list.toArray( new File[0] );
256     }
257 
258     public static File[] getSystemIncludePaths( NativeSources[] sources )
259     {
260         if ( sources == null )
261         {
262             return new File[0];
263         }
264 
265         List list = new ArrayList();
266 
267         for ( int i = 0; i < sources.length; ++i )
268         {
269             if ( !sources[i].getDependencyAnalysisParticipation() )
270             {
271                 list.add( sources[i].getDirectory() );
272             }
273         }
274 
275         return (File[]) list.toArray( new File[0] );
276     }
277 
278 }