extracted method to PluginUtils instead of 2 copy/paste

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1337699 13f79535-47bb-0310-9956-ffa450edef68
master
Herve Boutemy 2012-05-12 21:52:23 +00:00
parent fb1a7dfed3
commit e5756b0911
3 changed files with 65 additions and 114 deletions

View File

@ -44,7 +44,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -214,7 +213,7 @@ public class PluginDescriptorGenerator
if ( StringUtils.isEmpty( packageName ) )
{
packageName = discoverPackageName( pluginDescriptor );
packageName = PluginUtils.discoverPackageName( pluginDescriptor );
}
if ( StringUtils.isNotEmpty( packageName ) )
{
@ -272,55 +271,6 @@ public class PluginDescriptorGenerator
return descriptor;
}
/**
* Find the best package name, based on the number of hits of actual Mojo classes.
*
* @param pluginDescriptor not null
* @return the best name of the package for the generated mojo
*/
private static String discoverPackageName( PluginDescriptor pluginDescriptor )
{
Map<String, Integer> packageNames = new HashMap<String, Integer>();
@SuppressWarnings( "unchecked" )
List<MojoDescriptor> descriptors = pluginDescriptor.getMojos();
for ( MojoDescriptor descriptor : descriptors )
{
String impl = descriptor.getImplementation();
if ( impl.lastIndexOf( '.' ) != -1 )
{
String name = impl.substring( 0, impl.lastIndexOf( '.' ) );
if ( packageNames.get( name ) != null )
{
int next = packageNames.get( name ).intValue() + 1;
packageNames.put( name, new Integer( next ) );
}
else
{
packageNames.put( name, new Integer( 1 ) );
}
}
else
{
packageNames.put( "", new Integer( 1 ) );
}
}
String packageName = "";
int max = 0;
for ( Map.Entry<String, Integer> entry : packageNames.entrySet() )
{
String key = entry.getKey();
int value = entry.getValue().intValue();
if ( value > max )
{
max = value;
packageName = key;
}
}
return packageName;
}
protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w )
{
processMojoDescriptor( mojoDescriptor, w, false );
@ -696,7 +646,7 @@ public class PluginDescriptorGenerator
protected String rewriteHelpClassToMojoPackage( PluginToolsRequest request )
throws GeneratorException
{
String destinationPackage = PluginHelpGenerator.discoverPackageName( request.getPluginDescriptor() );
String destinationPackage = PluginUtils.discoverPackageName( request.getPluginDescriptor() );
if ( StringUtils.isEmpty( destinationPackage ) )
{
return null;

View File

@ -24,6 +24,7 @@ import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.PluginToolsRequest;
import org.apache.maven.tools.plugin.util.PluginUtils;
import org.apache.velocity.VelocityContext;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
@ -40,10 +41,7 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
@ -104,6 +102,7 @@ public class PluginHelpGenerator
MojoDescriptor helpDescriptor = makeHelpDescriptor( pluginDescriptor );
@SuppressWarnings( "unchecked" )
List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
if ( mojoDescriptors != null )
@ -251,7 +250,7 @@ public class PluginHelpGenerator
String packageName = helpPackageName;
if ( StringUtils.isEmpty( packageName ) )
{
packageName = discoverPackageName( pluginDescriptor );
packageName = PluginUtils.discoverPackageName( pluginDescriptor );
}
if ( StringUtils.isNotEmpty( packageName ) )
{
@ -308,62 +307,4 @@ public class PluginHelpGenerator
return descriptor;
}
/**
* Find the best package name, based on the number of hits of actual Mojo classes.
*
* @param pluginDescriptor not null
* @return the best name of the package for the generated mojo
*/
protected static String discoverPackageName( PluginDescriptor pluginDescriptor )
{
Map<String, Integer> packageNames = new HashMap<String, Integer>();
List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
if ( mojoDescriptors == null )
{
return "";
}
for ( MojoDescriptor descriptor : mojoDescriptors )
{
String impl = descriptor.getImplementation();
if ( StringUtils.equals( descriptor.getGoal(), "help" ) && StringUtils.equals( "HelpMojo", impl ) )
{
continue;
}
if ( impl.lastIndexOf( '.' ) != -1 )
{
String name = impl.substring( 0, impl.lastIndexOf( '.' ) );
if ( packageNames.get( name ) != null )
{
int next = ( packageNames.get( name ) ).intValue() + 1;
packageNames.put( name, new Integer( next ) );
}
else
{
packageNames.put( name, new Integer( 1 ) );
}
}
else
{
packageNames.put( "", new Integer( 1 ) );
}
}
String packageName = "";
int max = 0;
for ( Iterator it = packageNames.keySet().iterator(); it.hasNext(); )
{
String key = it.next().toString();
int value = ( packageNames.get( key ) ).intValue();
if ( value > max )
{
max = value;
packageName = key;
}
}
return packageName;
}
}

View File

@ -31,8 +31,10 @@ import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -181,6 +183,7 @@ public final class PluginUtils
* <code>false</code> otherwise.
* @throws IllegalArgumentException if any
*/
@SuppressWarnings( "unchecked" )
public static boolean isMavenReport( String impl, MavenProject project )
throws IllegalArgumentException
{
@ -724,4 +727,61 @@ public final class PluginUtils
sb.append( text );
}
}
/**
* Find the best package name, based on the number of hits of actual Mojo classes.
*
* @param pluginDescriptor not null
* @return the best name of the package for the generated mojo
*/
public static String discoverPackageName( PluginDescriptor pluginDescriptor )
{
Map<String, Integer> packageNames = new HashMap<String, Integer>();
@SuppressWarnings( "unchecked" )
List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
if ( mojoDescriptors == null )
{
return "";
}
for ( MojoDescriptor descriptor : mojoDescriptors )
{
String impl = descriptor.getImplementation();
if ( StringUtils.equals( descriptor.getGoal(), "help" ) && StringUtils.equals( "HelpMojo", impl ) )
{
continue;
}
if ( impl.lastIndexOf( '.' ) != -1 )
{
String name = impl.substring( 0, impl.lastIndexOf( '.' ) );
if ( packageNames.get( name ) != null )
{
int next = ( packageNames.get( name ) ).intValue() + 1;
packageNames.put( name, new Integer( next ) );
}
else
{
packageNames.put( name, new Integer( 1 ) );
}
}
else
{
packageNames.put( "", new Integer( 1 ) );
}
}
String packageName = "";
int max = 0;
for ( Map.Entry<String, Integer> entry : packageNames.entrySet() )
{
int value = entry.getValue().intValue();
if ( value > max )
{
max = value;
packageName = entry.getKey();
}
}
return packageName;
}
}