MPLUGIN-43: Adding validation in PluginXdocGenerator class?

o used jtidy to make HTML valid the javadoc comments from mojo/parameter description

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@617807 13f79535-47bb-0310-9956-ffa450edef68
master
Vincent Siveton 2008-02-02 14:42:59 +00:00
parent 20347131d3
commit 5ff84de952
3 changed files with 80 additions and 14 deletions

View File

@ -42,5 +42,10 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>4aug2000r7-dev</version>
</dependency>
</dependencies>
</project>

View File

@ -19,13 +19,6 @@ package org.apache.maven.tools.plugin.generator;
* 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.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.XMLWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@ -33,6 +26,16 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringInputStream;
import org.codehaus.plexus.util.StringOutputStream;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.XMLWriter;
import org.w3c.tidy.Tidy;
/**
* @todo add example usage tag that can be shown in the doco
* @version $Id$
@ -109,7 +112,7 @@ public class PluginXdocGenerator
if ( mojoDescriptor.getDescription() != null )
{
w.writeMarkup( mojoDescriptor.getDescription() );
w.writeMarkup( makeHtmlValid( mojoDescriptor.getDescription() ) );
}
else
{
@ -193,8 +196,8 @@ public class PluginXdocGenerator
if ( StringUtils.isNotEmpty( value ) )
{
w.startElement( "li" );
w.writeMarkup(
"Invokes the execution of the lifecycle phase <code>" + value + "</code> prior to executing itself." );
w.writeMarkup( "Invokes the execution of the lifecycle phase <code>" + value
+ "</code> prior to executing itself." );
w.endElement(); //li
}
@ -202,8 +205,8 @@ public class PluginXdocGenerator
if ( StringUtils.isNotEmpty( value ) )
{
w.startElement( "li" );
w.writeMarkup(
"Invokes the execution of this plugin's goal <code>" + value + "</code> prior to executing itself." );
w.writeMarkup( "Invokes the execution of this plugin's goal <code>" + value
+ "</code> prior to executing itself." );
w.endElement(); //li
}
@ -290,6 +293,10 @@ public class PluginXdocGenerator
{
description = "No Description.";
}
else
{
description = makeHtmlValid( description );
}
w.startElement( "p" );
w.writeMarkup( description );
w.endElement(); //p
@ -420,6 +427,10 @@ public class PluginXdocGenerator
{
description = "No description.";
}
else
{
description = makeHtmlValid( description );
}
if ( StringUtils.isNotEmpty( parameter.getDeprecated() ) )
{
description = "<b>Deprecated</b>. " + description;
@ -456,4 +467,40 @@ public class PluginXdocGenerator
return list;
}
/**
* @param description Javadoc description with HTML tags
* @return the description with valid HTML tags
*/
protected static String makeHtmlValid( String description )
{
if ( StringUtils.isEmpty( description ) )
{
return "";
}
StringOutputStream out = new StringOutputStream();
// Using jTidy to clean comment
Tidy tidy = new Tidy();
tidy.setDocType( "loose" );
tidy.setXHTML( true );
tidy.setXmlOut( true );
tidy.setMakeClean( true );
tidy.setQuiet( true );
tidy.setShowWarnings( false );
tidy.parse( new StringInputStream( description ), out );
// strip the header/body stuff
String LS = System.getProperty( "line.separator" );
String commentCleaned = out.toString();
if ( StringUtils.isEmpty( commentCleaned ) )
{
return "";
}
int startPos = commentCleaned.indexOf( "<body>" + LS ) + 6 + LS.length();
int endPos = commentCleaned.indexOf( LS + "</body>" );
return commentCleaned.substring( startPos, endPos );
}
}

View File

@ -21,10 +21,24 @@ package org.apache.maven.tools.plugin.generator;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
* @version $Id: PluginXdocGeneratorTest.java,v 1.1 2005/02/20 16:25:21 jdcasey
* Exp $
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
* @version $Id$
*/
public class PluginXdocGeneratorTest
extends AbstractGeneratorTestCase
{
public void testMakeHtmlValid()
{
String javadoc = "";
assertEquals( "", PluginXdocGenerator.makeHtmlValid( javadoc ) );
// true HTML
javadoc = "Generates <i>something</i> for the project.";
assertEquals( "Generates <i>something</i> for the project.", PluginXdocGenerator.makeHtmlValid( javadoc ) );
// wrong HTML
javadoc = "Generates <i>something</i> <b> for the project.";
assertEquals( "Generates <i>something</i> <b> for the project.</b>", PluginXdocGenerator
.makeHtmlValid( javadoc ) );
}
}