View Javadoc
1   package org.apache.maven.shared.io.scan;
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.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.shared.io.scan.mapping.SourceMapping;
29  import org.apache.maven.shared.utils.io.DirectoryScanner;
30  
31  /**
32   * @author jdcasey
33   * @version $Id: AbstractResourceInclusionScanner.java 1719477 2015-12-11 17:33:58Z khmarbaise $
34   */
35  public abstract class AbstractResourceInclusionScanner
36      implements ResourceInclusionScanner
37  {
38      private final List<SourceMapping> sourceMappings = new ArrayList<SourceMapping>();
39  
40      /** {@inheritDoc} */
41      public final void addSourceMapping( SourceMapping sourceMapping )
42      {
43          sourceMappings.add( sourceMapping );
44      }
45  
46      /**
47       * @return The source mapping.
48       */
49      protected final List<SourceMapping> getSourceMappings()
50      {
51          return Collections.unmodifiableList( sourceMappings );
52      }
53  
54      /**
55       * @param sourceDir {@link File}
56       * @param sourceIncludes source includes.
57       * @param sourceExcludes source excludes.
58       * @return The resulting sources.
59       */
60      protected String[] scanForSources( File sourceDir, Set<String> sourceIncludes, Set<String> sourceExcludes )
61      {
62          DirectoryScanner ds = new DirectoryScanner();
63          ds.setFollowSymlinks( true );
64          ds.setBasedir( sourceDir );
65  
66          String[] includes;
67          if ( sourceIncludes.isEmpty() )
68          {
69              includes = new String[0];
70          }
71          else
72          {
73              includes = (String[]) sourceIncludes.toArray( new String[sourceIncludes.size()] );
74          }
75  
76          ds.setIncludes( includes );
77  
78          String[] excludes;
79          if ( sourceExcludes.isEmpty() )
80          {
81              excludes = new String[0];
82          }
83          else
84          {
85              excludes = (String[]) sourceExcludes.toArray( new String[sourceExcludes.size()] );
86          }
87  
88          ds.setExcludes( excludes );
89          ds.addDefaultExcludes();
90  
91          ds.scan();
92  
93          return ds.getIncludedFiles();
94      }
95  }