001package org.apache.maven.scm;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028
029import org.codehaus.plexus.util.DirectoryScanner;
030import org.codehaus.plexus.util.FileUtils;
031import org.codehaus.plexus.util.StringUtils;
032
033/**
034 * Set of files used for SCM operations.
035 * Consists of the base directory of the files and a list of files relative to that directory.
036 *
037 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
038 *
039 */
040public class ScmFileSet
041    implements Serializable
042{
043    private static final long serialVersionUID = -5978597349974797556L;
044
045    private static final String DELIMITER = ",";
046    
047    /** @see DirectoryScanner#DEFAULTEXCLUDES */
048    private static final String DEFAULT_EXCLUDES = StringUtils.join( DirectoryScanner.DEFAULTEXCLUDES, DELIMITER );
049
050    private final File basedir;
051
052    private String includes;
053
054    private String excludes;
055
056    /**
057     * List of File objects, all relative to the basedir.
058     */
059    private final List<File> files;
060
061    /**
062     * Create a file set with no files, only the base directory.
063     *
064     * @param basedir directory files in the set are relative to
065     */
066    public ScmFileSet( File basedir )
067    {
068        this( basedir, new ArrayList<File>( 0 ) );
069    }
070
071    /**
072     * Create a file set with only the file provided, relative to basedir.
073     *
074     * @param basedir directory file is relative to
075     * @param file    file that the set will contain, has to be relative to basedir
076     */
077    public ScmFileSet( File basedir, File file )
078    {
079        this( basedir, new File[]{file} );
080    }
081
082    /**
083     * Create a file set with only files (not directories) from basefile,
084     * using includes and excludes provided.
085     *
086     * @param basedir  directory files are relative to
087     * @param includes Ant pattern for files to include
088     * @param excludes Ant pattern for files to exclude,
089     *                 if null DEFAULT_EXCLUDES is used, else DEFAULT_EXCLUDES is added.
090     * @throws IOException if any
091     */
092    public ScmFileSet( File basedir, String includes, String excludes )
093        throws IOException
094    {
095        this.basedir = basedir;
096
097        if ( excludes != null && excludes.length() > 0 )
098        {
099            excludes += DELIMITER + DEFAULT_EXCLUDES;
100        }
101        else
102        {
103            excludes = DEFAULT_EXCLUDES;
104        }
105        List<File> fileList = FileUtils.getFiles( basedir, includes, excludes, false );
106        this.files = fileList;
107        this.includes = includes;
108        this.excludes = excludes;
109    }
110
111    /**
112     * Create a file set with files from basefile, using includes provided and default excludes.
113     *
114     * @param basedir  directory files are relative to
115     * @param includes Ant pattern for files to include
116     * @throws IOException if any
117     * @since 1.0
118     */
119    public ScmFileSet( File basedir, String includes )
120        throws IOException
121    {
122        this( basedir, includes, null );
123    }
124
125    /**
126     * Create a file set with the files provided, relative to basedir.
127     *
128     * @param basedir directory files are relative to
129     * @param files   files that the set will contain, have to be relative to basedir
130     * @deprecated use ScmFileSet( File, List )
131     */
132    public ScmFileSet( File basedir, File[] files )
133    {
134        this( basedir, Arrays.asList( files ) );
135    }
136
137    /**
138     * Create a file set with the files provided, relative to basedir.
139     *
140     * @param basedir directory files are relative to
141     * @param files   list of File objects, files that the set will contain, have to be relative to basedir
142     */
143    public ScmFileSet( File basedir, List<File> files )
144    {
145        if ( basedir == null )
146        {
147            throw new NullPointerException( "basedir must not be null" );
148        }
149
150        if ( files == null )
151        {
152            throw new NullPointerException( "files must not be null" );
153        }
154
155        this.basedir = basedir;
156        this.files = files;
157    }
158
159    /**
160     * Get the base directory of the file set. It's the directory files in the set are relative to.
161     *
162     * @return base directory
163     */
164    public File getBasedir()
165    {
166        return basedir;
167    }
168
169    /**
170     * Get the list of files in the set, relative to basedir
171     *
172     * @return files in this set
173     * @deprecated use getFileList() instead
174     */
175    public File[] getFiles()
176    {
177        return this.files.toArray( new File[this.files.size()] );
178    }
179
180    /**
181     * Get the list of files in the set, relative to basedir
182     *
183     * @return List of File objects
184     */
185    public List<File> getFileList()
186    {
187        return this.files;
188    }
189
190
191    /**
192     * @return the includes files as a comma separated string
193     */
194    public String getIncludes()
195    {
196        return this.includes;
197    }
198
199
200    /**
201     * @return the excludes files as a comma separated string
202     */
203    public String getExcludes()
204    {
205        return this.excludes;
206    }
207
208    /** {@inheritDoc} */
209    public String toString()
210    {
211        return "basedir = " + basedir + "; files = " + files;
212    }
213}