/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.mvn.mc;
import ch.hortis.sonar.model.Collectable;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.FileSource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JXRCollector extends BaseMeasuresCollector {
private java.io.File classesFile;
private java.io.File testClassesFile;
private JXRFilesFilter filter;
protected boolean initialize( MavenProject project ) throws MojoExecutionException {
classesFile = findAllClassesFile( project );
testClassesFile = findAllTestClassesFile( project );
filter = new JXRFilesFilter();
if ( classesFile != null || testClassesFile != null ) {
return true;
}
return false;
}
public List<Collectable> collect() throws MojoExecutionException {
List<Collectable> collectables = new ArrayList<Collectable>();
if ( classesFile != null ) {
collectables.addAll(collectFiles( classesFile ));
}
if ( testClassesFile != null ) {
collectables.addAll(collectFiles( testClassesFile ));
}
return collectables;
}
private List<Collectable> collectFiles( java.io.File allClassesFile ) throws MojoExecutionException {
java.io.File JXRDirectory = allClassesFile.getParentFile();
String rootJXRDirectoryPath = null;
try {
rootJXRDirectoryPath = JXRDirectory.getCanonicalPath().replace( '\\', '/' );
} catch ( IOException e ) {
throw new MojoExecutionException( "Unable to retreive JXR root directory canonical path", e );
}
return walkDirectory( JXRDirectory, rootJXRDirectoryPath );
}
private List<Collectable> walkDirectory( java.io.File JXRDirectory, String rootJXRDirectoryPath ) throws MojoExecutionException {
List<Collectable> result = new ArrayList<Collectable>();
java.io.File[] jxrDirContent = JXRDirectory.listFiles(filter);
if ( jxrDirContent != null ) {
for ( java.io.File file : jxrDirContent ) {
if ( file.isDirectory() ) {
result.addAll( walkDirectory( file, rootJXRDirectoryPath ) );
} else {
File jxrSource = toFile( file, rootJXRDirectoryPath );
result.add( jxrSource );
filesRepository.addFile( jxrSource );
}
}
}
return result;
}
private java.io.File findAllClassesFile( final MavenProject project ) {
return findClassesFile( project, "/site/xref");
}
private java.io.File findAllTestClassesFile( final MavenProject project ) {
return findClassesFile( project, "/site/xref-test");
}
private java.io.File findClassesFile( final MavenProject project, final String base ) {
String xhtmlFile = project.getBuild().getDirectory() + base + "/allclasses-frame.html";
java.io.File result = new java.io.File( xhtmlFile );
return ( result.exists() ? result : null );
}
private File toFile( java.io.File jxrSourceFile, String rootJXRDirectoryPath ) throws MojoExecutionException {
String path = null;
try {
path = jxrSourceFile.getCanonicalPath().replace( '\\', '/' );
} catch ( IOException e ) {
throw new MojoExecutionException( "Unable to retreive file canonical path", e );
}
String filename = StringUtils.substringAfterLast( path, "/" );
filename = StringUtils.replaceOnce( filename, ".html", ".java" );
String namespace = StringUtils.removeStart( path, rootJXRDirectoryPath );
namespace = StringUtils.substringBeforeLast( namespace, "/" );
namespace = StringUtils.replaceChars( namespace, '/', '.' );
if ( namespace.endsWith( "." ) ) namespace = StringUtils.chop( namespace );
if ( namespace.startsWith( "." ) ) namespace = StringUtils.substringAfter( namespace, "." );
File file = new File( filename, namespace, null );
String source;
try {
source = FileUtils.readFileToString( jxrSourceFile );
} catch ( IOException e ) {
throw new MojoExecutionException( "IO error during file " + jxrSourceFile.getPath() + " reading", e);
}
FileSource fileSource = new FileSource( source );
fileSource.setFile(file);
file.setFileSource( fileSource );
return file;
}
private class JXRFilesFilter implements FileFilter {
public boolean accept( java.io.File pathname ) {
String name = pathname.getName().toLowerCase();
return pathname.isDirectory() || (
name.endsWith( ".html" ) &&
!name.equals( "index.html" ) &&
!name.endsWith( "frame.html" ) &&
!name.endsWith( "summary.html" ) );
}
}
}