/*
* 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 final static List<String> JXR_PROPRIETARY_FILENAMES = getJXRProprietaryFilenames();
private java.io.File classesFile;
private java.io.File testClassesFile;
private JXRFilesFilter filter;
protected boolean initialize( MavenProject project ) {
classesFile = findAllClassesFile( project );
testClassesFile = findAllTestClassesFile( project );
filter = new JXRFilesFilter();
return classesFile != null || testClassesFile != null;
}
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;
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;
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 static List<String> getJXRProprietaryFilenames() {
List<String> filenames = new ArrayList<String>();
filenames.add( "index.html" );
filenames.add( "allclasses-frame.html" );
filenames.add( "overview-frame.html" );
filenames.add( "overview-summary.html" );
filenames.add( "package-frame.html" );
filenames.add( "package-summary.html" );
return filenames;
}
private class JXRFilesFilter implements FileFilter {
public boolean accept( java.io.File pathname ) {
String filenname = pathname.getName();
return pathname.isDirectory() ||
(filenname.endsWith(".html") && !JXR_PROPRIETARY_FILENAMES.contains(filenname));
}
}
}