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 *
038 */
039public class ScmFileSet implements Serializable {
040    private static final long serialVersionUID = -5978597349974797556L;
041
042    private static final String DELIMITER = ",";
043
044    /** @see DirectoryScanner#DEFAULTEXCLUDES */
045    private static final String DEFAULT_EXCLUDES = StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, DELIMITER);
046
047    private final File basedir;
048
049    private String includes;
050
051    private String excludes;
052
053    /**
054     * List of File objects, all relative to the basedir.
055     */
056    private final List<File> files;
057
058    /**
059     * Create a file set with no files, only the base directory.
060     *
061     * @param basedir directory files in the set are relative to
062     */
063    public ScmFileSet(File basedir) {
064        this(basedir, new ArrayList<>(0));
065    }
066
067    /**
068     * Create a file set with only the file provided, relative to basedir.
069     *
070     * @param basedir directory file is relative to
071     * @param file    file that the set will contain, has to be relative to basedir
072     */
073    public ScmFileSet(File basedir, File file) {
074        this(basedir, new File[] {file});
075    }
076
077    /**
078     * Create a file set with only files (not directories) from basefile,
079     * using includes and excludes provided.
080     *
081     * @param basedir  directory files are relative to
082     * @param includes Ant pattern for files to include
083     * @param excludes Ant pattern for files to exclude,
084     *                 if null DEFAULT_EXCLUDES is used, else DEFAULT_EXCLUDES is added.
085     * @throws IOException if any
086     */
087    public ScmFileSet(File basedir, String includes, String excludes) throws IOException {
088        this.basedir = basedir;
089
090        if (excludes != null && excludes.length() > 0) {
091            excludes += DELIMITER + DEFAULT_EXCLUDES;
092        } else {
093            excludes = DEFAULT_EXCLUDES;
094        }
095        this.files = FileUtils.getFiles(basedir, includes, excludes, false);
096        this.includes = includes;
097        this.excludes = excludes;
098    }
099
100    /**
101     * Create a file set with files from basefile, using includes provided and default excludes.
102     *
103     * @param basedir  directory files are relative to
104     * @param includes Ant pattern for files to include
105     * @throws IOException if any
106     * @since 1.0
107     */
108    public ScmFileSet(File basedir, String includes) throws IOException {
109        this(basedir, includes, null);
110    }
111
112    /**
113     * Create a file set with the files provided, relative to basedir.
114     *
115     * @param basedir directory files are relative to
116     * @param files   files that the set will contain, have to be relative to basedir
117     * @deprecated use ScmFileSet( File, List )
118     */
119    public ScmFileSet(File basedir, File[] files) {
120        this(basedir, Arrays.asList(files));
121    }
122
123    /**
124     * Create a file set with the files provided, relative to basedir.
125     *
126     * @param basedir directory files are relative to
127     * @param files   list of File objects, files that the set will contain, have to be relative to basedir
128     */
129    public ScmFileSet(File basedir, List<File> files) {
130        if (basedir == null) {
131            throw new NullPointerException("basedir must not be null");
132        }
133
134        if (files == null) {
135            throw new NullPointerException("files must not be null");
136        }
137
138        this.basedir = basedir;
139        this.files = files;
140    }
141
142    /**
143     * Get the base directory of the file set. It's the directory files in the set are relative to.
144     *
145     * @return base directory
146     */
147    public File getBasedir() {
148        return basedir;
149    }
150
151    /**
152     * Get the list of files in the set, relative to basedir
153     *
154     * @return files in this set
155     * @deprecated use getFileList() instead
156     */
157    public File[] getFiles() {
158        return this.files.toArray(new File[this.files.size()]);
159    }
160
161    /**
162     * Get the list of files in the set, relative to basedir
163     *
164     * @return List of File objects
165     */
166    public List<File> getFileList() {
167        return this.files;
168    }
169
170    /**
171     * @return the includes files as a comma separated string
172     */
173    public String getIncludes() {
174        return this.includes;
175    }
176
177    /**
178     * @return the excludes files as a comma separated string
179     */
180    public String getExcludes() {
181        return this.excludes;
182    }
183
184    /** {@inheritDoc} */
185    public String toString() {
186        return "basedir = " + basedir + "; files = " + files;
187    }
188}