View Javadoc
1   package org.apache.maven.scm;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Set of files used for SCM operations.
35   * Consists of the base directory of the files and a list of files relative to that directory.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   *
39   */
40  public class ScmFileSet
41      implements Serializable
42  {
43      private static final long serialVersionUID = -5978597349974797556L;
44  
45      private static final String DELIMITER = ",";
46      
47      /** @see DirectoryScanner#DEFAULTEXCLUDES */
48      private static final String DEFAULT_EXCLUDES = StringUtils.join( DirectoryScanner.DEFAULTEXCLUDES, DELIMITER );
49  
50      private final File basedir;
51  
52      private String includes;
53  
54      private String excludes;
55  
56      /**
57       * List of File objects, all relative to the basedir.
58       */
59      private final List<File> files;
60  
61      /**
62       * Create a file set with no files, only the base directory.
63       *
64       * @param basedir directory files in the set are relative to
65       */
66      public ScmFileSet( File basedir )
67      {
68          this( basedir, new ArrayList<File>( 0 ) );
69      }
70  
71      /**
72       * Create a file set with only the file provided, relative to basedir.
73       *
74       * @param basedir directory file is relative to
75       * @param file    file that the set will contain, has to be relative to basedir
76       */
77      public ScmFileSet( File basedir, File file )
78      {
79          this( basedir, new File[]{file} );
80      }
81  
82      /**
83       * Create a file set with only files (not directories) from basefile,
84       * using includes and excludes provided.
85       *
86       * @param basedir  directory files are relative to
87       * @param includes Ant pattern for files to include
88       * @param excludes Ant pattern for files to exclude,
89       *                 if null DEFAULT_EXCLUDES is used, else DEFAULT_EXCLUDES is added.
90       * @throws IOException if any
91       */
92      public ScmFileSet( File basedir, String includes, String excludes )
93          throws IOException
94      {
95          this.basedir = basedir;
96  
97          if ( excludes != null && excludes.length() > 0 )
98          {
99              excludes += DELIMITER + DEFAULT_EXCLUDES;
100         }
101         else
102         {
103             excludes = DEFAULT_EXCLUDES;
104         }
105         @SuppressWarnings( "unchecked" )
106         List<File> fileList = (List<File>) FileUtils.getFiles( basedir, includes, excludes, false ); 
107         this.files = fileList;
108         this.includes = includes;
109         this.excludes = excludes;
110     }
111 
112     /**
113      * Create a file set with files from basefile, using includes provided and default excludes.
114      *
115      * @param basedir  directory files are relative to
116      * @param includes Ant pattern for files to include
117      * @throws IOException if any
118      * @since 1.0
119      */
120     public ScmFileSet( File basedir, String includes )
121         throws IOException
122     {
123         this( basedir, includes, null );
124     }
125 
126     /**
127      * Create a file set with the files provided, relative to basedir.
128      *
129      * @param basedir directory files are relative to
130      * @param files   files that the set will contain, have to be relative to basedir
131      * @deprecated use ScmFileSet( File, List )
132      */
133     public ScmFileSet( File basedir, File[] files )
134     {
135         this( basedir, Arrays.asList( files ) );
136     }
137 
138     /**
139      * Create a file set with the files provided, relative to basedir.
140      *
141      * @param basedir directory files are relative to
142      * @param files   list of File objects, files that the set will contain, have to be relative to basedir
143      */
144     public ScmFileSet( File basedir, List<File> files )
145     {
146         if ( basedir == null )
147         {
148             throw new NullPointerException( "basedir must not be null" );
149         }
150 
151         if ( files == null )
152         {
153             throw new NullPointerException( "files must not be null" );
154         }
155 
156         this.basedir = basedir;
157         this.files = files;
158     }
159 
160     /**
161      * Get the base directory of the file set. It's the directory files in the set are relative to.
162      *
163      * @return base directory
164      */
165     public File getBasedir()
166     {
167         return basedir;
168     }
169 
170     /**
171      * Get the list of files in the set, relative to basedir
172      *
173      * @return files in this set
174      * @deprecated use getFileList() instead
175      */
176     public File[] getFiles()
177     {
178         return this.files.toArray( new File[this.files.size()] );
179     }
180 
181     /**
182      * Get the list of files in the set, relative to basedir
183      *
184      * @return List of File objects
185      */
186     public List<File> getFileList()
187     {
188         return this.files;
189     }
190 
191 
192     /**
193      * @return the includes files as a comma separated string
194      */
195     public String getIncludes()
196     {
197         return this.includes;
198     }
199 
200 
201     /**
202      * @return the excludes files as a comma separated string
203      */
204     public String getExcludes()
205     {
206         return this.excludes;
207     }
208 
209     /** {@inheritDoc} */
210     public String toString()
211     {
212         return "basedir = " + basedir + "; files = " + files;
213     }
214 }