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