Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java?rev=1337273&r1=1337272&r2=1337273&view=diff ============================================================================== --- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java (original) +++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java Fri May 11 16:44:59 2012 @@ -19,32 +19,32 @@ package org.apache.maven.tools.plugin.ge * under the License. */ +import org.apache.maven.plugin.descriptor.MojoDescriptor; +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.velocity.VelocityContext; +import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.console.ConsoleLogger; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.velocity.VelocityComponent; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.io.StringWriter; import java.io.Writer; -import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Properties; -import org.apache.maven.plugin.descriptor.MojoDescriptor; -import org.apache.maven.plugin.descriptor.Parameter; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; -import org.apache.maven.tools.plugin.PluginToolsRequest; -import org.apache.maven.tools.plugin.util.PluginUtils; -import org.codehaus.plexus.logging.AbstractLogEnabled; -import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; - /** * Generates an <code>HelpMojo</code> class. * @@ -56,20 +56,30 @@ public class PluginHelpGenerator extends AbstractLogEnabled implements Generator { - /** Line separator */ + /** + * Line separator + */ private static final String LS = System.getProperty( "line.separator" ); - /** Default generated class name */ + /** + * Default generated class name + */ private static final String HELP_MOJO_CLASS_NAME = "HelpMojo"; - /** Default goal */ + /** + * Default goal + */ private static final String HELP_GOAL = "help"; private String helpPackageName; - - /** Flag to indicate if the generated help mojo should use Java 5 features */ + + /** + * Flag to indicate if the generated help mojo should use Java 5 features + */ private boolean useJava5; + private VelocityComponent velocityComponent; + /** * Default constructor */ @@ -82,39 +92,29 @@ public class PluginHelpGenerator // Public methods // ---------------------------------------------------------------------- - /** {@inheritDoc} */ - public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) - throws IOException - { - execute( destinationDirectory, new DefaultPluginToolsRequest( null, pluginDescriptor ) ); - } - - /** {@inheritDoc} */ + + /** + * {@inheritDoc} + */ public void execute( File destinationDirectory, PluginToolsRequest request ) - throws IOException + throws GeneratorException { PluginDescriptor pluginDescriptor = request.getPluginDescriptor(); - - if ( pluginDescriptor.getMojos() == null || pluginDescriptor.getMojos().size() < 1 ) - { - return; - } MojoDescriptor helpDescriptor = makeHelpDescriptor( pluginDescriptor ); // Verify that no help goal already exists - for ( @SuppressWarnings( "unchecked" ) - Iterator<MojoDescriptor> it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) + for ( @SuppressWarnings( "unchecked" ) Iterator<MojoDescriptor> it = pluginDescriptor.getMojos().iterator(); + it.hasNext(); ) { MojoDescriptor descriptor = it.next(); - if ( descriptor.getGoal().equals( helpDescriptor.getGoal() ) - && !descriptor.getImplementation().equals( helpDescriptor.getImplementation() ) ) + if ( descriptor.getGoal().equals( helpDescriptor.getGoal() ) && !descriptor.getImplementation().equals( + helpDescriptor.getImplementation() ) ) { if ( getLogger().isWarnEnabled() ) { - getLogger().warn( - "\n\nA help goal (" + descriptor.getImplementation() + getLogger().warn( "\n\nA help goal (" + descriptor.getImplementation() + ") already exists in this plugin. SKIPPED THE " + helpDescriptor.getImplementation() + " GENERATION.\n" ); } @@ -123,6 +123,36 @@ public class PluginHelpGenerator } } + Properties properties = new Properties(); + properties.put( "helpPackageName", helpPackageName == null ? "" : helpPackageName ); + + MavenProject mavenProject = request.getProject(); + + String propertiesFilePath = "META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId(); + + File tmpPropertiesFile = + new File( request.getProject().getBuild().getDirectory(), "maven-plugin-help.properties" ); + if ( tmpPropertiesFile.exists() ) + { + tmpPropertiesFile.delete(); + } + else + { + if ( !tmpPropertiesFile.getParentFile().exists() ) + { + tmpPropertiesFile.getParentFile().mkdirs(); + } + } + + try + { + properties.store( new FileOutputStream( tmpPropertiesFile ), "maven plugin help generation informations" ); + } + catch ( IOException e ) + { + throw new GeneratorException( e.getMessage(), e ); + } + String sourcePath = helpDescriptor.getImplementation().replace( '.', File.separatorChar ) + ".java"; File helpClass = new File( destinationDirectory, sourcePath ); helpClass.getParentFile().mkdirs(); @@ -131,13 +161,19 @@ public class PluginHelpGenerator try { writer = new OutputStreamWriter( new FileOutputStream( helpClass ), request.getEncoding() ); - writeClass( writer, pluginDescriptor, helpDescriptor, useJava5 ); + writer.write( getHelpClassSources( propertiesFilePath ) ); writer.flush(); } + catch ( IOException e ) + { + throw new GeneratorException( e.getMessage(), e ); + } finally { IOUtil.close( writer ); } + + } public PluginHelpGenerator setHelpPackageName( String helpPackageName ) @@ -152,15 +188,52 @@ public class PluginHelpGenerator return this; } + public VelocityComponent getVelocityComponent() + { + return velocityComponent; + } + + public PluginHelpGenerator setVelocityComponent( VelocityComponent velocityComponent ) + { + this.velocityComponent = velocityComponent; + return this; + } + // ---------------------------------------------------------------------- // Private methods // ---------------------------------------------------------------------- + protected String getHelpClassSources( String propertiesFilePath ) + { + Properties properties = new Properties(); + VelocityContext context = new VelocityContext( properties ); + if ( this.helpPackageName != null ) + { + properties.put( "helpPackageName", this.helpPackageName ); + } + else + { + properties.put( "helpPackageName", "" ); + } + properties.put( "propertiesFilePath", propertiesFilePath + "/plugin-description.xml" ); + // FIXME encoding ! + + StringWriter stringWriter = new StringWriter(); + + InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( "help-class-source.vm" ); + InputStreamReader isReader = new InputStreamReader( is ); + velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader ); + + return stringWriter.toString(); + + } + + /** * Creates a minimalistic mojo descriptor for the generated help goal. * * @param pluginDescriptor The descriptor of the plugin for which to generate a help goal, must not be - * <code>null</code>. + * <code>null</code>. * @return The mojo descriptor for the generated help goal, never <code>null</code>. */ private MojoDescriptor makeHelpDescriptor( PluginDescriptor pluginDescriptor ) @@ -187,9 +260,10 @@ public class PluginHelpGenerator descriptor.setImplementation( HELP_MOJO_CLASS_NAME ); } - descriptor.setDescription( "Display help information on " + pluginDescriptor.getArtifactId() - + ".<br/> Call <pre> mvn " + descriptor.getFullGoalName() - + " -Ddetail=true -Dgoal=<goal-name></pre> to display parameter details." ); + descriptor.setDescription( + "Display help information on " + pluginDescriptor.getArtifactId() + ".<br/> Call <pre> mvn " + + descriptor.getFullGoalName() + + " -Ddetail=true -Dgoal=<goal-name></pre> to display parameter details." ); try { @@ -204,8 +278,8 @@ public class PluginHelpGenerator param = new Parameter(); param.setName( "goal" ); param.setType( "java.lang.String" ); - param.setDescription( "The name of the goal for which to show help." - + " If unspecified, all goals will be displayed." ); + param.setDescription( + "The name of the goal for which to show help." + " If unspecified, all goals will be displayed." ); param.setExpression( "${goal}" ); descriptor.addParameter( param ); @@ -241,595 +315,45 @@ public class PluginHelpGenerator */ private static String discoverPackageName( PluginDescriptor pluginDescriptor ) { - Map<String, Integer> packageNames = new HashMap<String, Integer>(); - for ( @SuppressWarnings( "unchecked" ) - Iterator<MojoDescriptor> it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) + Map packageNames = new HashMap(); + for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) { - MojoDescriptor descriptor = it.next(); - - String name = ""; - int next = 1; + MojoDescriptor descriptor = (MojoDescriptor) it.next(); String impl = descriptor.getImplementation(); if ( impl.lastIndexOf( '.' ) != -1 ) { - name = impl.substring( 0, impl.lastIndexOf( '.' ) ); - Integer count = packageNames.get( name ); - - if ( count != null ) + String name = impl.substring( 0, impl.lastIndexOf( '.' ) ); + if ( packageNames.get( name ) != null ) { - next = count.intValue() + 1; + int next = ( (Integer) packageNames.get( name ) ).intValue() + 1; + packageNames.put( name, new Integer( next ) ); + } + else + { + packageNames.put( name, new Integer( 1 ) ); } } - - packageNames.put( name, next ); + else + { + packageNames.put( "", new Integer( 1 ) ); + } } String packageName = ""; int max = 0; - for ( Map.Entry<String, Integer> entry : packageNames.entrySet() ) + for ( Iterator it = packageNames.keySet().iterator(); it.hasNext(); ) { - int value = entry.getValue().intValue(); + String key = it.next().toString(); + int value = ( (Integer) packageNames.get( key ) ).intValue(); if ( value > max ) { max = value; - packageName = entry.getKey(); + packageName = key; } } return packageName; } - /** - * Generates the <code>HelpMojo</code> class. - * - * @param writer not null - * @param pluginDescriptor not null - * @param helpDescriptor not null - * @param useJava5 If the generated code should use Java5 features - * @throws IOException if any - */ - private static void writeClass( Writer writer, PluginDescriptor pluginDescriptor, MojoDescriptor helpDescriptor, - boolean useJava5 ) - throws IOException - { - String packageName = ""; - String simpleName = helpDescriptor.getImplementation(); - int dot = simpleName.lastIndexOf( '.' ); - if ( dot >= 0 ) - { - packageName = simpleName.substring( 0, dot ); - simpleName = simpleName.substring( dot + 1 ); - } - - if ( packageName.length() > 0 ) - { - writer.write( "package " + packageName + ";" + LS ); - writer.write( LS ); - } - - writeImports( writer ); - writer.write( LS ); - - writeMojoJavadoc( writer, pluginDescriptor, helpDescriptor ); - - if ( useJava5 ) - { - writer.write( "@SuppressWarnings( \"all\" )" + LS ); - } - - writer.write( "public class " + simpleName + LS ); - writer.write( " extends AbstractMojo" + LS ); - writer.write( "{" + LS ); - - writeVariables( writer, helpDescriptor ); - - writer.write( LS ); - - writeExecute( writer, pluginDescriptor, helpDescriptor ); - - writer.write( LS ); - writeUtilities( writer, useJava5 ); - writer.write( "}" + LS ); - } - - /** - * @param writer not null - * @throws IOException if any - */ - private static void writeImports( Writer writer ) - throws IOException - { - writer.write( "import java.util.ArrayList;" + LS ); - writer.write( "import java.util.Iterator;" + LS ); - writer.write( "import java.util.List;" + LS ); - writer.write( LS ); - writer.write( "import org.apache.maven.plugin.AbstractMojo;" + LS ); - writer.write( "import org.apache.maven.plugin.MojoExecutionException;" + LS ); - } - - /** - * @param writer not null - * @param pluginDescriptor not null - * @param helpDescriptor not null - * @throws IOException if any - */ - private static void writeMojoJavadoc( Writer writer, PluginDescriptor pluginDescriptor, - MojoDescriptor helpDescriptor ) - throws IOException - { - StringBuffer author = new StringBuffer(); - author.append( PluginHelpGenerator.class.getName() ); - - String resource = "META-INF/maven/org.apache.maven.plugin-tools/maven-plugin-tools-api/pom.properties"; - InputStream resourceAsStream = PluginHelpGenerator.class.getClassLoader().getResourceAsStream( resource ); - - if ( resourceAsStream != null ) - { - try - { - Properties properties = new Properties(); - properties.load( resourceAsStream ); - - author.append( " (version " ).append( properties.getProperty( "version", "unknown" ) ).append( ")" ); - } - catch ( IOException e ) - { - // nope - } - finally - { - IOUtil.close( resourceAsStream ); - } - } - - writer.write( "/**" + LS ); - writer.write( " * " + helpDescriptor.getDescription() + LS ); - writer.write( " *" + LS ); - writer.write( " * @version generated on " + new Date() + LS ); - writer.write( " * @author " + author.toString() + LS ); - writer.write( " * @goal " + helpDescriptor.getGoal() + LS ); - writer.write( " * @requiresProject false" + LS ); - writer.write( " * @threadSafe" + LS ); - writer.write( " */" + LS ); - } - - /** - * @param writer not null - * @param helpDescriptor not null - * @throws IOException if any - */ - private static void writeVariables( Writer writer, MojoDescriptor helpDescriptor ) - throws IOException - { - for ( @SuppressWarnings( "unchecked" ) - Iterator<Parameter> it = helpDescriptor.getParameters().iterator(); it.hasNext(); ) - { - Parameter param = it.next(); - writer.write( " /**" + LS ); - writer.write( " * " + StringUtils.escape( param.getDescription() ) + LS ); - writer.write( " * " + LS ); - writer.write( " * @parameter" ); - if ( StringUtils.isNotEmpty( param.getExpression() ) ) - { - writer.write( " expression=\"" ); - writer.write( StringUtils.escape( param.getExpression() ) ); - writer.write( "\"" ); - } - if ( StringUtils.isNotEmpty( param.getDefaultValue() ) ) - { - writer.write( " default-value=\"" ); - writer.write( StringUtils.escape( param.getDefaultValue() ) ); - writer.write( "\"" ); - } - writer.write( LS ); - writer.write( " */" + LS ); - writer.write( " private " + param.getType() + " " + param.getName() + ";" + LS ); - writer.write( LS ); - } - } - - /** - * @param writer not null - * @param pluginDescriptor not null - * @param helpDescriptor not null - * @throws IOException if any - */ - private static void writeExecute( Writer writer, PluginDescriptor pluginDescriptor, MojoDescriptor helpDescriptor ) - throws IOException - { - List<MojoDescriptor> mojoDescriptors = new ArrayList<MojoDescriptor>(); - - mojoDescriptors.add( helpDescriptor ); - for ( @SuppressWarnings( "unchecked" ) - Iterator<MojoDescriptor> it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) - { - MojoDescriptor mojoDescriptor = it.next(); - - if ( !helpDescriptor.getGoal().equals( mojoDescriptor.getGoal() ) ) - { - mojoDescriptors.add( mojoDescriptor ); - } - } - - PluginUtils.sortMojos( mojoDescriptors ); - - writer.write( " /** {@inheritDoc} */" + LS ); - writer.write( " public void execute()" + LS ); - writer.write( " throws MojoExecutionException" + LS ); - writer.write( " {" + LS ); - - writer.write( " if ( lineLength <= 0 )" + LS ); - writer.write( " {" + LS ); - writer.write( " getLog().warn( \"The parameter 'lineLength' should be positive, using '80' as " - + "default.\" );" + LS ); - writer.write( " lineLength = 80;" + LS ); - writer.write( " }" + LS ); - writer.write( " if ( indentSize <= 0 )" + LS ); - writer.write( " {" + LS ); - writer.write( " getLog().warn( \"The parameter 'indentSize' should be positive, using '2' as " - + "default.\" );" + LS ); - writer.write( " indentSize = 2;" + LS ); - writer.write( " }" + LS ); - writer.write( LS ); - - writer.write( " StringBuffer sb = new StringBuffer();" + LS ); - writer.write( LS ); - - writer.write( " append( sb, \"" + StringUtils.escape( pluginDescriptor.getId() ) + "\", 0 );" + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - writer.write( LS ); - - if ( StringUtils.isNotEmpty( pluginDescriptor.getName() ) - && ( pluginDescriptor.getName().indexOf( pluginDescriptor.getId() ) != -1 ) ) - { - writer.write( " append( sb, \"" - + StringUtils.escape( pluginDescriptor.getName() + " " + pluginDescriptor.getVersion() ) - + "\", 0 );" + LS ); - } - else - { - if ( StringUtils.isNotEmpty( pluginDescriptor.getName() ) ) - { - writer.write( " append( sb, \"" + StringUtils.escape( pluginDescriptor.getName() ) - + "\", 0 );" + LS ); - } - else - { - writer.write( " append( sb, \"" + StringUtils.escape( pluginDescriptor.getId() ) - + "\", 0 );" + LS ); - } - } - writer.write( " append( sb, \"" + toDescription( pluginDescriptor.getDescription() ) + "\", 1 );" - + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - writer.write( LS ); - - writer.write( " if ( goal == null || goal.length() <= 0 )" + LS ); - writer.write( " {" + LS ); - writer.write( " append( sb, \"This plugin has " + mojoDescriptors.size() + " " - + ( mojoDescriptors.size() > 1 ? "goals" : "goal" ) + ":\", 0 );" + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - writer.write( " }" + LS ); - - writer.write( LS ); - - for ( MojoDescriptor descriptor : mojoDescriptors ) - { - writeGoal( writer, descriptor ); - } - - writer.write( " if ( getLog().isInfoEnabled() )" + LS ); - writer.write( " {" + LS ); - writer.write( " getLog().info( sb.toString() );" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - } - - /** - * @param writer not null - * @param descriptor not null - * @throws IOException if any - */ - private static void writeGoal( Writer writer, MojoDescriptor descriptor ) - throws IOException - { - String goalDescription = toDescription( descriptor.getDescription() ); - - writer.write( " if ( goal == null || goal.length() <= 0 || \"" - + StringUtils.escape( descriptor.getGoal() ) + "\".equals( goal ) )" + LS ); - writer.write( " {" + LS ); - writer.write( " append( sb, \"" + StringUtils.escape( descriptor.getFullGoalName() ) + "\", 0 );" - + LS ); - if ( StringUtils.isNotEmpty( descriptor.getDeprecated() ) ) - { - writer.write( " append( sb, \"Deprecated. " + toDescription( descriptor.getDeprecated() ) - + "\", 1 );" + LS ); - writer.write( " if ( detail )" + LS ); - writer.write( " {" + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - writer.write( " append( sb, \"" + goalDescription + "\", 1 );" + LS ); - writer.write( " }" + LS ); - } - else - { - writer.write( " append( sb, \"" + goalDescription + "\", 1 );" + LS ); - } - writer.write( " append( sb, \"\", 0 );" + LS ); - - if ( descriptor.getParameters() != null && descriptor.getParameters().size() > 0 ) - { - @SuppressWarnings( "unchecked" ) - List<Parameter> params = descriptor.getParameters(); - - PluginUtils.sortMojoParameters( params ); - - writer.write( " if ( detail )" + LS ); - writer.write( " {" + LS ); - - writer.write( " append( sb, \"Available parameters:\", 1 );" + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - - for ( Parameter parameter : params ) - { - if ( parameter.isEditable() ) - { - writer.write( LS ); - writeParameter( writer, parameter ); - } - } - - writer.write( " }" + LS ); - } - - writer.write( " }" + LS ); - writer.write( LS ); - } - - /** - * @param writer not null - * @param parameter not null - * @throws IOException if any - */ - private static void writeParameter( Writer writer, Parameter parameter ) - throws IOException - { - String expression = parameter.getExpression(); - - if ( expression == null || !expression.startsWith( "${component." ) ) - { - String parameterName = StringUtils.escape( parameter.getName() ); - String parameterDescription = toDescription( parameter.getDescription() ); - String parameterDefaultValue = ""; - if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) ) - { - parameterDefaultValue = " (Default: " + StringUtils.escape( parameter.getDefaultValue() ) + ")"; - } - writer.write( " append( sb, \"" + parameterName + parameterDefaultValue + "\", 2 );" + LS ); - if ( StringUtils.isNotEmpty( parameter.getDeprecated() ) ) - { - writer.write( " append( sb, \"Deprecated. " + toDescription( parameter.getDeprecated() ) - + "\", 3 );" + LS ); - writer.write( " append( sb, \"\", 0 );" + LS ); - } - writer.write( " append( sb, \"" + parameterDescription + "\", 3 );" + LS ); - if ( parameter.isRequired() ) - { - writer.write( " append( sb, \"Required: Yes\", 3 );" + LS ); - } - if ( StringUtils.isNotEmpty( parameter.getExpression() ) ) - { - writer.write( " append( sb, \"Expression: " - + StringUtils.escape( parameter.getExpression() ) + "\", 3 );" + LS ); - } - writer.write( " append( sb, \"\", 0 );" + LS ); - } - } - - /** - * @param writer not null - * @param useJava5 If the generated code should use Java5 features - * @throws IOException if any - */ - private static void writeUtilities( Writer writer, boolean useJava5 ) - throws IOException - { - writer.write( " /**" + LS ); - writer.write( " * <p>Repeat a String <code>n</code> times to form a new string.</p>" + LS ); - writer.write( " *" + LS ); - writer.write( " * @param str String to repeat" + LS ); - writer.write( " * @param repeat number of times to repeat str" + LS ); - writer.write( " * @return String with repeated String" + LS ); - writer.write( " * @throws NegativeArraySizeException if <code>repeat < 0</code>" + LS ); - writer.write( " * @throws NullPointerException if str is <code>null</code>" + LS ); - writer.write( " */" + LS ); - writer.write( " private static String repeat( String str, int repeat )" + LS ); - writer.write( " {" + LS ); - writer.write( " StringBuffer buffer = new StringBuffer( repeat * str.length() );" + LS ); - writer.write( LS ); - writer.write( " for ( int i = 0; i < repeat; i++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " buffer.append( str );" + LS ); - writer.write( " }" + LS ); - writer.write( LS ); - writer.write( " return buffer.toString();" + LS ); - writer.write( " }" + LS ); - - writer.write( LS ); - writer.write( " /** " + LS ); - writer.write( " * Append a description to the buffer by respecting the indentSize and lineLength " - + "parameters." + LS ); - writer.write( " * <b>Note</b>: The last character is always a new line." + LS ); - writer.write( " * " + LS ); - writer.write( " * @param sb The buffer to append the description, not <code>null</code>." + LS ); - writer.write( " * @param description The description, not <code>null</code>." + LS ); - writer.write( " * @param indent The base indentation level of each line, must not be negative." + LS ); - writer.write( " */" + LS ); - writer.write( " private void append( StringBuffer sb, String description, int indent )" + LS ); - writer.write( " {" + LS ); - writer.write( " for ( Iterator it = toLines( description, indent, indentSize, lineLength )" - + ".iterator(); it.hasNext(); )" + LS ); - writer.write( " {" + LS ); - writer.write( " sb.append( it.next().toString() ).append( '\\n' );" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - - writer.write( LS ); - writer.write( " /** " + LS ); - writer.write( " * Splits the specified text into lines of convenient display length." + LS ); - writer.write( " * " + LS ); - writer.write( " * @param text The text to split into lines, must not be <code>null</code>." + LS ); - writer.write( " * @param indent The base indentation level of each line, must not be negative." + LS ); - writer.write( " * @param indentSize The size of each indentation, must not be negative." + LS ); - writer.write( " * @param lineLength The length of the line, must not be negative." + LS ); - writer.write( " * @return The sequence of display lines, never <code>null</code>." + LS ); - writer.write( " * @throws NegativeArraySizeException if <code>indent < 0</code>" + LS ); - writer.write( " */" + LS ); - writer.write( " private static List toLines( String text, int indent, int indentSize, int lineLength )" - + LS ); - writer.write( " {" + LS ); - if ( useJava5 ) - { - writer.write( " List<String> lines = new ArrayList<String>();" + LS ); - } - else - { - writer.write( " List lines = new ArrayList();" + LS ); - } - writer.write( LS ); - writer.write( " String ind = repeat( \"\\t\", indent );" + LS ); - writer.write( " String[] plainLines = text.split( \"(\\r\\n)|(\\r)|(\\n)\" );" + LS ); - writer.write( " for ( int i = 0; i < plainLines.length; i++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " toLines( lines, ind + plainLines[i], indentSize, lineLength );" + LS ); - writer.write( " }" + LS ); - writer.write( LS ); - writer.write( " return lines;" + LS ); - writer.write( " }" + LS ); - - writer.write( LS ); - writer.write( " /** " + LS ); - writer.write( " * Adds the specified line to the output sequence, performing line wrapping if necessary." - + LS ); - writer.write( " * " + LS ); - writer.write( " * @param lines The sequence of display lines, must not be <code>null</code>." + LS ); - writer.write( " * @param line The line to add, must not be <code>null</code>." + LS ); - writer.write( " * @param indentSize The size of each indentation, must not be negative." + LS ); - writer.write( " * @param lineLength The length of the line, must not be negative." + LS ); - writer.write( " */" + LS ); - if ( useJava5 ) - { - writer.write( " private static void toLines( List<String> lines, String line, int indentSize, int lineLength )" - + LS ); - } - else - { - writer.write( " private static void toLines( List lines, String line, int indentSize, int lineLength )" - + LS ); - } - writer.write( " {" + LS ); - writer.write( " int lineIndent = getIndentLevel( line );" + LS ); - writer.write( " StringBuffer buf = new StringBuffer( 256 );" + LS ); - writer.write( " String[] tokens = line.split( \" +\" );" + LS ); - writer.write( " for ( int i = 0; i < tokens.length; i++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " String token = tokens[i];" + LS ); - writer.write( " if ( i > 0 )" + LS ); - writer.write( " {" + LS ); - writer.write( " if ( buf.length() + token.length() >= lineLength )" + LS ); - writer.write( " {" + LS ); - writer.write( " lines.add( buf.toString() );" + LS ); - writer.write( " buf.setLength( 0 );" + LS ); - writer.write( " buf.append( repeat( \" \", lineIndent * indentSize ) );" + LS ); - writer.write( " }" + LS ); - writer.write( " else" + LS ); - writer.write( " {" + LS ); - writer.write( " buf.append( ' ' );" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - writer.write( " for ( int j = 0; j < token.length(); j++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " char c = token.charAt( j );" + LS ); - writer.write( " if ( c == '\\t' )" + LS ); - writer.write( " {" + LS ); - writer.write( " buf.append( repeat( \" \", indentSize - buf.length() % indentSize ) );" - + LS ); - writer.write( " }" + LS ); - writer.write( " else if ( c == '\\u00A0' )" + LS ); - writer.write( " {" + LS ); - writer.write( " buf.append( ' ' );" + LS ); - writer.write( " }" + LS ); - writer.write( " else" + LS ); - writer.write( " {" + LS ); - writer.write( " buf.append( c );" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - writer.write( " lines.add( buf.toString() );" + LS ); - writer.write( " }" + LS ); - - writer.write( LS ); - writer.write( " /** " + LS ); - writer.write( " * Gets the indentation level of the specified line." + LS ); - writer.write( " * " + LS ); - writer.write( " * @param line The line whose indentation level should be retrieved, must not be " - + "<code>null</code>." + LS ); - writer.write( " * @return The indentation level of the line." + LS ); - writer.write( " */" + LS ); - writer.write( " private static int getIndentLevel( String line )" + LS ); - writer.write( " {" + LS ); - writer.write( " int level = 0;" + LS ); - writer.write( " for ( int i = 0; i < line.length() && line.charAt( i ) == '\\t'; i++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " level++;" + LS ); - writer.write( " }" + LS ); - writer.write( " for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )" + LS ); - writer.write( " {" + LS ); - writer.write( " if ( line.charAt( i ) == '\\t' )" + LS ); - writer.write( " {" + LS ); - writer.write( " level++;" + LS ); - writer.write( " break;" + LS ); - writer.write( " }" + LS ); - writer.write( " }" + LS ); - writer.write( " return level;" + LS ); - writer.write( " }" + LS ); - } - - /** - * Gets the effective string to use for the plugin/mojo/parameter description. - * - * @param description The description of the element, may be <code>null</code>. - * @return The effective description string, never <code>null</code>. - */ - private static String toDescription( String description ) - { - if ( StringUtils.isNotEmpty( description ) ) - { - return StringUtils.escape( PluginUtils.toText( description ) ); - } - - return "(no description available)"; - } - - /** - * Converts a HTML fragment as extracted from a javadoc comment to a plain text string. This method tries to retain - * as much of the text formatting as possible by means of the following transformations: - * <ul> - * <li>List items are converted to leading tabs (U+0009), followed by the item number/bullet, another tab and - * finally the item contents. Each tab denotes an increase of indentation.</li> - * <li>Flow breaking elements as well as literal line terminators in preformatted text are converted to a newline - * (U+000A) to denote a mandatory line break.</li> - * <li>Consecutive spaces and line terminators from character data outside of preformatted text will be normalized - * to a single space. The resulting space denotes a possible point for line wrapping.</li> - * <li>Each space in preformatted text will be converted to a non-breaking space (U+00A0).</li> - * </ul> - * - * @param html The HTML fragment to convert to plain text, may be <code>null</code>. - * @return A string with HTML tags converted into pure text, never <code>null</code>. - * @deprecated since 2.4.3, using {@link PluginUtils#toText(String)} instead of. - */ - protected static String toText( String html ) - { - return PluginUtils.toText( html ); - } }
Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java?rev=1337273&r1=1337272&r2=1337273&view=diff ============================================================================== --- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java (original) +++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java Fri May 11 16:44:59 2012 @@ -19,6 +19,17 @@ package org.apache.maven.tools.plugin.ge * under the License. */ +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.Parameter; +import org.apache.maven.project.MavenProject; +import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; +import org.apache.maven.tools.plugin.PluginToolsRequest; +import org.apache.maven.tools.plugin.util.PluginUtils; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; +import org.codehaus.plexus.util.xml.XMLWriter; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -32,30 +43,21 @@ import java.util.List; import java.util.Locale; import java.util.ResourceBundle; -import org.apache.maven.plugin.descriptor.MojoDescriptor; -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.DefaultPluginToolsRequest; -import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; -import org.apache.maven.tools.plugin.PluginToolsRequest; -import org.apache.maven.tools.plugin.util.PluginUtils; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; -import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; -import org.codehaus.plexus.util.xml.XMLWriter; - /** - * @todo add example usage tag that can be shown in the doco * @version $Id$ + * @todo add example usage tag that can be shown in the doco */ public class PluginXdocGenerator implements Generator { - /** locale */ + /** + * locale + */ private final Locale locale; - /** project */ + /** + * project + */ private final MavenProject project; /** @@ -81,7 +83,7 @@ public class PluginXdocGenerator /** * @param project not null. - * @param locale not null wanted locale. + * @param locale not null wanted locale. */ public PluginXdocGenerator( MavenProject project, Locale locale ) { @@ -96,31 +98,35 @@ public class PluginXdocGenerator } } - /** {@inheritDoc} */ - public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) - throws IOException - { - execute( destinationDirectory, new DefaultPluginToolsRequest( project, pluginDescriptor ) ); - } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ public void execute( File destinationDirectory, PluginToolsRequest request ) - throws IOException + throws GeneratorException { - if ( request.getPluginDescriptor().getMojos() != null ) + try { - for ( @SuppressWarnings( "unchecked" ) - Iterator<MojoDescriptor> it = request.getPluginDescriptor().getMojos().iterator(); it.hasNext(); ) + if ( request.getPluginDescriptor().getMojos() != null ) { - MojoDescriptor descriptor = it.next(); + for ( @SuppressWarnings( "unchecked" ) Iterator<MojoDescriptor> it = + request.getPluginDescriptor().getMojos().iterator(); it.hasNext(); ) + { + MojoDescriptor descriptor = it.next(); - processMojoDescriptor( descriptor, destinationDirectory ); + processMojoDescriptor( descriptor, destinationDirectory ); + } } } + catch ( IOException e ) + { + throw new GeneratorException( e.getMessage(), e ); + } + } /** - * @param mojoDescriptor not null + * @param mojoDescriptor not null * @param destinationDirectory not null * @throws IOException if any */ @@ -147,7 +153,7 @@ public class PluginXdocGenerator /** * @param mojo not null - * @param ext not null + * @param ext not null * @return the output file name */ private String getMojoFilename( MojoDescriptor mojo, String ext ) @@ -157,7 +163,7 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param w not null + * @param w not null */ private void writeBody( MojoDescriptor mojoDescriptor, XMLWriter w ) { @@ -196,8 +202,8 @@ public class PluginXdocGenerator w.endElement(); //p w.startElement( "p" ); w.writeMarkup( mojoDescriptor.getPluginDescriptor().getGroupId() + ":" - + mojoDescriptor.getPluginDescriptor().getArtifactId() + ":" - + mojoDescriptor.getPluginDescriptor().getVersion() + ":" + mojoDescriptor.getGoal() ); + + mojoDescriptor.getPluginDescriptor().getArtifactId() + ":" + + mojoDescriptor.getPluginDescriptor().getVersion() + ":" + mojoDescriptor.getGoal() ); w.endElement(); //p if ( StringUtils.isNotEmpty( mojoDescriptor.getDeprecated() ) ) @@ -237,7 +243,7 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param w not null + * @param w not null */ private void writeReportNotice( MojoDescriptor mojoDescriptor, XMLWriter w ) { @@ -252,7 +258,7 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param w not null + * @param w not null */ private void writeGoalAttributes( MojoDescriptor mojoDescriptor, XMLWriter w ) { @@ -273,12 +279,12 @@ public class PluginXdocGenerator w.writeMarkup( getString( "pluginxdoc.mojodescriptor.projectRequired" ) ); w.endElement(); //li } - + if ( mojoDescriptor.isRequiresReports() ) { if ( !addedUl ) { - w.startElement( "ul" ); + w.startElement( "ul" ); addedUl = true; } w.startElement( "li" ); @@ -451,12 +457,11 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param w not null + * @param w not null */ private void writeGoalParameterTable( MojoDescriptor mojoDescriptor, XMLWriter w ) { - @SuppressWarnings( "unchecked" ) - List<Parameter> parameterList = mojoDescriptor.getParameters(); + @SuppressWarnings( "unchecked" ) List<Parameter> parameterList = mojoDescriptor.getParameters(); //remove components and read-only parameters List<Parameter> list = filterParameters( parameterList ); @@ -490,7 +495,7 @@ public class PluginXdocGenerator if ( parameterList != null ) { - for ( Parameter parameter : parameterList ) + for ( Parameter parameter : parameterList ) { if ( parameter.isEditable() ) { @@ -509,8 +514,8 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param parameterList not null - * @param w not null + * @param parameterList not null + * @param w not null */ private void writeParameterDetails( MojoDescriptor mojoDescriptor, List<Parameter> parameterList, XMLWriter w ) { @@ -570,8 +575,8 @@ public class PluginXdocGenerator w.startElement( "ul" ); addedUl = true; } - writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.since" ), - mojoDescriptor.getSince(), w ); + writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.since" ), mojoDescriptor.getSince(), + w ); } } @@ -582,8 +587,8 @@ public class PluginXdocGenerator w.startElement( "ul" ); addedUl = true; } - writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.required" ), - getString( "pluginxdoc.yes" ), w ); + writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.required" ), getString( "pluginxdoc.yes" ), + w ); } else { @@ -592,8 +597,8 @@ public class PluginXdocGenerator w.startElement( "ul" ); addedUl = true; } - writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.required" ), - getString( "pluginxdoc.no" ), w ); + writeDetail( getString( "pluginxdoc.mojodescriptor.parameter.required" ), getString( "pluginxdoc.no" ), + w ); } if ( !addedUl && StringUtils.isNotEmpty( parameter.getExpression() ) ) @@ -628,7 +633,7 @@ public class PluginXdocGenerator /** * @param param not null * @param value could be null - * @param w not null + * @param w not null */ private void writeDetail( String param, String value, XMLWriter w ) { @@ -642,8 +647,8 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param parameterList not null - * @param w not null + * @param parameterList not null + * @param w not null */ private void writeParameterSummary( MojoDescriptor mojoDescriptor, List<Parameter> parameterList, XMLWriter w ) { @@ -664,11 +669,12 @@ public class PluginXdocGenerator /** * @param mojoDescriptor not null - * @param title not null - * @param parameterList not null - * @param w not null + * @param title not null + * @param parameterList not null + * @param w not null */ - private void writeParameterList( MojoDescriptor mojoDescriptor, String title, List<Parameter> parameterList, XMLWriter w ) + private void writeParameterList( MojoDescriptor mojoDescriptor, String title, List<Parameter> parameterList, + XMLWriter w ) { w.startElement( "subsection" ); w.addAttribute( "name", title ); @@ -722,9 +728,8 @@ public class PluginXdocGenerator String description; if ( StringUtils.isNotEmpty( parameter.getDeprecated() ) ) { - description = - format( "pluginxdoc.mojodescriptor.parameter.deprecated", - PluginUtils.makeHtmlValid( parameter.getDeprecated() ) ); + description = format( "pluginxdoc.mojodescriptor.parameter.deprecated", + PluginUtils.makeHtmlValid( parameter.getDeprecated() ) ); } else if ( StringUtils.isNotEmpty( parameter.getDescription() ) ) { @@ -750,7 +755,7 @@ public class PluginXdocGenerator } /** - * @param required <code>true</code> for required parameters, <code>false</code> otherwise. + * @param required <code>true</code> for required parameters, <code>false</code> otherwise. * @param parameterList not null * @return list of parameters depending the value of <code>required</code> */ @@ -792,21 +797,21 @@ public class PluginXdocGenerator /** * Convenience method. * - * @param key not null + * @param key not null * @param arg1 not null * @return Localized, formatted text identified by <code>key</code>. * @see #format(String, Object[]) */ private String format( String key, Object arg1 ) { - return format( key, new Object[] { arg1 } ); + return format( key, new Object[]{ arg1 } ); } /** * Looks up the value for <code>key</code> in the <code>ResourceBundle</code>, * then formats that value for the specified <code>Locale</code> using <code>args</code>. * - * @param key not null + * @param key not null * @param args not null * @return Localized, formatted text identified by <code>key</code>. */ Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java?rev=1337273&r1=1337272&r2=1337273&view=diff ============================================================================== --- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java (original) +++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java Fri May 11 16:44:59 2012 @@ -19,11 +19,13 @@ package org.apache.maven.tools.plugin.ge * under the License. */ -import junit.framework.TestCase; +import org.apache.maven.model.Build; import org.apache.maven.plugin.descriptor.MojoDescriptor; 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.DefaultPluginToolsRequest; +import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.component.repository.ComponentDependency; import org.codehaus.plexus.util.FileUtils; @@ -38,7 +40,7 @@ import java.util.List; * jdcasey Exp $ */ public abstract class AbstractGeneratorTestCase - extends TestCase + extends PlexusTestCase { protected Generator generator; @@ -47,6 +49,7 @@ public abstract class AbstractGeneratorT protected void setUp() throws Exception { + super.setUp(); basedir = System.getProperty( "basedir" ); } @@ -93,7 +96,25 @@ public abstract class AbstractGeneratorT FileUtils.deleteDirectory( destinationDirectory ); destinationDirectory.mkdir(); - generator.execute( destinationDirectory, new DefaultPluginToolsRequest( null, pluginDescriptor ) ); + MavenProject mavenProject = new MavenProject(); + mavenProject.setGroupId( "foo" ); + mavenProject.setArtifactId( "bar" ); + mavenProject.setBuild( new Build() + { + @Override + public String getDirectory() + { + return basedir + "/target"; + } + + @Override + public String getOutputDirectory() + { + return basedir + "/target"; + } + } ); + + generator.execute( destinationDirectory, new DefaultPluginToolsRequest( mavenProject, pluginDescriptor ) ); validate( destinationDirectory ); @@ -120,8 +141,8 @@ public abstract class AbstractGeneratorT catch ( Exception e ) { throw new Exception( "Cannot find " + generatorClassName + - "! Make sure your test case is named in the form ${generatorClassName}Test " + - "or override the setupPlugin() method to instantiate the mojo yourself." ); + "! Make sure your test case is named in the form ${generatorClassName}Test " + + "or override the setupPlugin() method to instantiate the mojo yourself." ); } } Modified: maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/PluginHelpGeneratorTest.java URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/PluginHelpGeneratorTest.java?rev=1337273&r1=1337272&r2=1337273&view=diff ============================================================================== --- maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/PluginHelpGeneratorTest.java (original) +++ maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/generator/PluginHelpGeneratorTest.java Fri May 11 16:44:59 2012 @@ -19,6 +19,8 @@ package org.apache.maven.tools.plugin.ge * under the License. */ +import org.codehaus.plexus.velocity.VelocityComponent; + /** * @author <a href="mailto:[email protected]">Vincent Siveton</a> * @version $Id$ @@ -27,4 +29,12 @@ public class PluginHelpGeneratorTest extends AbstractGeneratorTestCase { // inherits tests from base class + protected void setupGenerator() + throws Exception + { + + generator = + new PluginHelpGenerator().setVelocityComponent( (VelocityComponent) lookup( VelocityComponent.ROLE ) ); + + } } Modified: maven/plugin-tools/branches/MPLUGIN-189/pom.xml URL: http://svn.apache.org/viewvc/maven/plugin-tools/branches/MPLUGIN-189/pom.xml?rev=1337273&r1=1337272&r2=1337273&view=diff ============================================================================== --- maven/plugin-tools/branches/MPLUGIN-189/pom.xml (original) +++ maven/plugin-tools/branches/MPLUGIN-189/pom.xml Fri May 11 16:44:59 2012 @@ -233,6 +233,24 @@ <artifactId>plexus-archiver</artifactId> <version>2.1.1</version> </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-velocity</artifactId> + <version>1.1.8</version> + <exclusions> + <exclusion> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + </exclusion> + </exclusions> + </dependency> + + <!-- other --> + <dependency> + <groupId>org.apache.velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.7</version> + </dependency> <dependency> <groupId>com.thoughtworks.qdox</groupId>
