Skip to content

Commit

Permalink
Merge pull request #56 from mojohaus/#12_includes_excludes
Browse files Browse the repository at this point in the history
#12 includes excludes
  • Loading branch information
bmarwell authored Jan 22, 2022
2 parents 82ffe28 + 55185a3 commit fbc0ea9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 24 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public class FileAnalyser
*/
private Collection sourceDirs;

/**
* The files to include, as a comma separated list of patterns.
*/
private String includes;

/**
* The files top exclude, as a comma separated list of patterns.
*/
private String excludes;

/**
* Log for debug output.
*/
Expand Down Expand Up @@ -119,11 +129,13 @@ public FileAnalyser( TagListReport report, List<TagClass> tagClasses )
multipleLineCommentsOn = report.isMultipleLineComments();
emptyCommentsOn = report.isEmptyComments();
log = report.getLog();
sourceDirs = report.constructSourceDirs();
sourceDirs = report.getSourceDirs();
encoding = report.getInputEncoding();
locale = report.getLocale();
noCommentString = report.getBundle().getString( "report.taglist.nocomment" );
this.tagClasses = tagClasses;
this.includes = report.getIncludesCommaSeparated();
this.excludes = report.getExcludesCommaSeparated();
}

/**
Expand Down Expand Up @@ -171,7 +183,7 @@ private List findFilesToScan()
{
for ( Iterator iter = sourceDirs.iterator(); iter.hasNext(); )
{
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), "**/*.java", null ) );
filesList.addAll( FileUtils.getFiles( new File( (String) iter.next() ), includes, excludes ) );
}
}
catch ( IOException e )
Expand Down
121 changes: 99 additions & 22 deletions src/main/java/org/codehaus/mojo/taglist/TagListReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.model.ReportPlugin;
Expand All @@ -37,6 +39,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.shared.utils.io.FileUtils;
import org.codehaus.mojo.taglist.beans.FileReport;
import org.codehaus.mojo.taglist.beans.TagReport;
import org.codehaus.mojo.taglist.output.TagListXMLComment;
Expand Down Expand Up @@ -72,6 +75,22 @@ public class TagListReport
/** Default locale used if the source file locale is null. */
private static final String DEFAULT_LOCALE = "en";

/**
* List of files to include. Specified as fileset patterns which are relative to the source directory.
*
* @since 3.0.0
*/
@Parameter( defaultValue = "**/*.java" )
private String[] includes;

/**
* List of files to exclude. Specified as fileset patterns which are relative to the source directory.
*
* @since 3.0.0
*/
@Parameter( defaultValue = "" )
private String[] excludes;

/**
* Specifies the directory where the xml output will be generated.
*
Expand Down Expand Up @@ -174,6 +193,8 @@ public class TagListReport

private String[] tags;

private AtomicReference<List> sourceDirs = new AtomicReference<>();

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -406,7 +427,7 @@ private String getRelativePath( File location )
*/
public boolean canGenerateReport()
{
boolean canGenerate = !constructSourceDirs().isEmpty();
boolean canGenerate = !getSourceDirs().isEmpty();
if ( aggregate && !getProject().isExecutionRoot() )
{
canGenerate = false;
Expand All @@ -416,11 +437,11 @@ public boolean canGenerateReport()

/**
* Removes empty dirs from the list.
*
*
* @param sourceDirectories the original list of directories.
* @return a new list containing only non empty dirs.
*/
private List pruneSourceDirs( List sourceDirectories )
private List pruneSourceDirs( List sourceDirectories ) throws IOException
{
List pruned = new ArrayList( sourceDirectories.size() );
for ( Iterator i = sourceDirectories.iterator(); i.hasNext(); )
Expand All @@ -435,43 +456,47 @@ private List pruneSourceDirs( List sourceDirectories )
}

/**
* Checks whether the given directory contains Java files.
*
* Checks whether the given directory contains source files.
*
* @param dir the source directory.
* @return true if the folder or one of its subfolders contains at least 1 Java file.
* @return true if the folder or one of its subfolders contains at least 1 source file that matches
* includes/excludes.
*/
private boolean hasSources( File dir )
private boolean hasSources( File dir ) throws IOException
{
boolean found = false;
if ( dir.exists() && dir.isDirectory() )
{
if ( !FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() )
{
return true;
}

File[] files = dir.listFiles();
for ( int i = 0; i < files.length && !found; i++ )
if ( files != null )
{
File currentFile = files[i];
if ( currentFile.isFile() && currentFile.getName().endsWith( ".java" ) )
{
found = true;
}
else if ( currentFile.isDirectory() )
for ( int i = 0; i < files.length; i++ )
{
boolean hasSources = hasSources( currentFile );
if ( hasSources )
File currentFile = files[i];
if ( currentFile.isDirectory() )
{
found = true;
boolean hasSources = hasSources( currentFile );
if ( hasSources )
{
return true;
}
}
}
}
}
return found;
return false;
}

/**
* Construct the list of source directories to analyze.
*
*
* @return the list of dirs.
*/
public List constructSourceDirs()
private List constructSourceDirs()
{
List dirs = new ArrayList( getProject().getCompileSourceRoots() );
if ( !skipTestSources )
Expand All @@ -496,11 +521,63 @@ public List constructSourceDirs()
}
}

dirs = pruneSourceDirs( dirs );
/*
* This try-catch is needed due to a missing declared exception in the
* 'canGenerateReport()' method. For this reason, neither the 'canGenerateReport()'
* nor the 'constructSourceDirs()' can throw exceptions.
* The exception itself is caused by a declaration from the FileUtils, but never used
* there. The FileUtils.getFiles() should be replaced by an NIO filter at some point.
*/
try
{
dirs = pruneSourceDirs( dirs );
}
catch ( IOException javaIoIOException )
{
getLog().warn( "Unable to prune source dirs.", javaIoIOException );
}

return dirs;
}

protected List getSourceDirs()
{
if ( sourceDirs.get() == null )
{
sourceDirs.compareAndSet( null, constructSourceDirs() );
}

return sourceDirs.get();
}

/**
* Get the files to include, as a comma separated list of patterns.
*/
String getIncludesCommaSeparated()
{
if ( includes != null )
{
return String.join( ",", includes );
}
else
{
return "";
}
}

/**
* Get the files to exclude, as a comma separated list of patterns.
*/
String getExcludesCommaSeparated()
{
if ( excludes != null ) {
return String.join(",", excludes);
} else {
return "";
}
}

/**
* Returns the Locale of the source files.
*
* @return The Locale of the source files.
Expand Down

0 comments on commit fbc0ea9

Please sign in to comment.