Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package br.com.objectos.sql.meta.maven.plugin;
  
  
 
 import java.util.List;

Display help information on sql-meta-maven-plugin.
Call mvn sql:help -Ddetail=true -Dgoal=<goal-name> to display parameter details.

Author(s):
maven-plugin-tools
 
 @Mojo( name = "help", requiresProject = false, threadSafe = true )
 public class HelpMojo
     extends AbstractMojo
 {
    
If true, display all settable properties for each goal.
 
     @Parameter( property = "detail", defaultValue = "false" )
     private boolean detail;

    
The name of the goal for which to show help. If unspecified, all goals will be displayed.
 
     @Parameter( property = "goal" )
     private java.lang.String goal;

    
The maximum length of a display line, should be positive.
 
     @Parameter( property = "lineLength", defaultValue = "80" )
     private int lineLength;

    
The number of spaces per indentation level, should be positive.
 
     @Parameter( property = "indentSize", defaultValue = "2" )
     private int indentSize;
 
     // groupId/artifactId/plugin-help.xml
     private static final String PLUGIN_HELP_PATH = "/META-INF/maven/br.com.objectos/sql-meta-maven-plugin/plugin-help.xml";
 
     private Document build()
         throws MojoExecutionException
     {
         getLog().debug"load plugin-help.xml: " +  );
         InputStream is = null;
         try
         {
             is = getClass().getResourceAsStream );
             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
             return dBuilder.parseis );
         }
         catch ( IOException e )
         {
             throw new MojoExecutionExceptione.getMessage(), e );
         }
         catch ( ParserConfigurationException e )
         {
             throw new MojoExecutionExceptione.getMessage(), e );
         }
         catch ( SAXException e )
         {
             throw new MojoExecutionExceptione.getMessage(), e );
         }
         finally
         {
             if ( is != null )
             {
                 try
                 {
                     is.close();
                 }
                 catch ( IOException e )
                 {
                     throw new MojoExecutionExceptione.getMessage(), e );
                 }
             }
        }
    }

    
    public void execute()
        throws MojoExecutionException
    {
        if (  <= 0 )
        {
            getLog().warn"The parameter 'lineLength' should be positive, using '80' as default." );
             = 80;
        }
        if (  <= 0 )
        {
            getLog().warn"The parameter 'indentSize' should be positive, using '2' as default." );
             = 2;
        }
        Document doc = build();
        StringBuilder sb = new StringBuilder();
        Node plugin = getSingleChilddoc"plugin" );
        String name = getValueplugin"name" );
        String version = getValueplugin"version" );
        String id = getValueplugin"groupId" ) + ":" + getValueplugin"artifactId" ) + ":" + version;
        if ( isNotEmptyname ) && !name.containsid ) )
        {
            appendsbname + " " + version, 0 );
        }
        else
        {
            if ( isNotEmptyname ) )
            {
                appendsbname, 0 );
            }
            else
            {
                appendsbid, 0 );
            }
        }
        appendsbgetValueplugin"description" ), 1 );
        appendsb"", 0 );
        //<goalPrefix>plugin</goalPrefix>
        String goalPrefix = getValueplugin"goalPrefix" );
        Node mojos1 = getSingleChildplugin"mojos" );
        List<Nodemojos = findNamedChildmojos1"mojo" );
        if (  == null || .length() <= 0 )
        {
            appendsb"This plugin has " + mojos.size() + ( mojos.size() > 1 ? " goals:" : " goal:" ), 0 );
            appendsb"", 0 );
        }
        for ( Node mojo : mojos )
        {
            writeGoalsbgoalPrefix, (Elementmojo );
        }
        if ( getLog().isInfoEnabled() )
        {
            getLog().infosb.toString() );
        }
    }
    private static boolean isNotEmptyString string )
    {
        return string != null && string.length() > 0;
    }
    private String getValueNode nodeString elementName )
        throws MojoExecutionException
    {
        return getSingleChildnodeelementName ).getTextContent();
    }
    private Node getSingleChildNode nodeString elementName )
        throws MojoExecutionException
    {
        List<NodenamedChild = findNamedChildnodeelementName );
        if ( namedChild.isEmpty() )
        {
            throw new MojoExecutionException"Could not find " + elementName + " in plugin-help.xml" );
        }
        if ( namedChild.size() > 1 )
        {
            throw new MojoExecutionException"Multiple " + elementName + " in plugin-help.xml" );
        }
        return namedChild.get( 0 );
    }
    private List<NodefindNamedChildNode nodeString elementName )
    {
        List<Noderesult = new ArrayList<Node>();
        NodeList childNodes = node.getChildNodes();
        for ( int i = 0; i < childNodes.getLength(); i++ )
        {
            Node item = childNodes.itemi );
            if ( elementName.equalsitem.getNodeName() ) )
            {
                result.additem );
            }
        }
        return result;
    }
    private Node findSingleChildNode nodeString elementName )
        throws MojoExecutionException
    {
        List<NodeelementsByTagName = findNamedChildnodeelementName );
        if ( elementsByTagName.isEmpty() )
        {
            return null;
        }
        if ( elementsByTagName.size() > 1 )
        {
            throw new MojoExecutionException"Multiple " + elementName + "in plugin-help.xml" );
        }
        return elementsByTagName.get( 0 );
    }
    private void writeGoalStringBuilder sbString goalPrefixElement mojo )
        throws MojoExecutionException
    {
        String mojoGoal = getValuemojo"goal" );
        Node configurationElement = findSingleChildmojo"configuration" );
		Node description = findSingleChildmojo"description" );
        if (  == null || .length() <= 0 || mojoGoal.equals ) )
        {
            appendsbgoalPrefix + ":" + mojoGoal, 0 );
            Node deprecated = findSingleChildmojo"deprecated" );
            if ( ( deprecated != null ) && isNotEmptydeprecated.getTextContent() ) )
            {
                appendsb"Deprecated. " + deprecated.getTextContent(), 1 );
                if (  && description != null )
                {
                    appendsb"", 0 );
                    appendsbdescription.getTextContent(), 1 );
                }
            }
            else if ( description != null )
            {
                appendsbdescription.getTextContent(), 1 );
            }
            appendsb"", 0 );
            if (  )
            {
                Node parametersNode = getSingleChildmojo"parameters" );
                List<Nodeparameters = findNamedChildparametersNode"parameter" );
                appendsb"Available parameters:", 1 );
                appendsb"", 0 );
                for ( Node parameter : parameters )
                {
                    writeParametersbparameterconfigurationElement );
                }
            }
        }
    }
    private void writeParameterStringBuilder sbNode parameterNode configurationElement )
        throws MojoExecutionException
    {
        String parameterName = getValueparameter"name" );
        String parameterDescription = getValueparameter"description" );
        Element fieldConfigurationElement = (Element)findSingleChildconfigurationElementparameterName );
        String parameterDefaultValue = "";
        if ( fieldConfigurationElement != null && fieldConfigurationElement.hasAttribute"default-value" ) )
        {
            parameterDefaultValue = " (Default: " + fieldConfigurationElement.getAttribute"default-value" ) + ")";
        }
        appendsbparameterName + parameterDefaultValue, 2 );
        Node deprecated = findSingleChildparameter"deprecated" );
        if ( ( deprecated != null ) && isNotEmptydeprecated.getTextContent() ) )
        {
            appendsb"Deprecated. " + deprecated.getTextContent(), 3 );
            appendsb"", 0 );
        }
        appendsbparameterDescription, 3 );
        if ( "true".equalsgetValueparameter"required" ) ) )
        {
            appendsb"Required: Yes", 3 );
        }
        if ( ( fieldConfigurationElement != null ) && isNotEmptyfieldConfigurationElement.getTextContent() ) )
        {
        	String property = getPropertyFromExpressionfieldConfigurationElement.getTextContent() );
            appendsb"User property: " + property, 3 );
        }
        appendsb"", 0 );
    }

    

Repeat a String n times to form a new string.

Parameters:
str String to repeat
repeat number of times to repeat str
Returns:
String with repeated String
Throws:
java.lang.NegativeArraySizeException if repeat < 0
java.lang.NullPointerException if str is null
    private static String repeatString strint repeat )
    {
        StringBuilder buffer = new StringBuilderrepeat * str.length() );
        for ( int i = 0; i < repeati++ )
        {
            buffer.appendstr );
        }
        return buffer.toString();
    }

    
Append a description to the buffer by respecting the indentSize and lineLength parameters. Note: The last character is always a new line.

Parameters:
sb The buffer to append the description, not null.
description The description, not null.
indent The base indentation level of each line, must not be negative.
    private void appendStringBuilder sbString descriptionint indent )
    {
        for ( String line : toLinesdescriptionindent ) )
        {
            sb.appendline ).append'\n' );
        }
    }

    
Splits the specified text into lines of convenient display length.

Parameters:
text The text to split into lines, must not be null.
indent The base indentation level of each line, must not be negative.
indentSize The size of each indentation, must not be negative.
lineLength The length of the line, must not be negative.
Returns:
The sequence of display lines, never null.
Throws:
java.lang.NegativeArraySizeException if indent < 0
    private static List<StringtoLinesString textint indentint indentSizeint lineLength )
    {
        List<Stringlines = new ArrayList<String>();
        String ind = repeat"\t"indent );
        String[] plainLines = text.split"(\r\n)|(\r)|(\n)" );
        for ( String plainLine : plainLines )
        {
            toLineslinesind + plainLineindentSizelineLength );
        }
        return lines;
    }

    
Adds the specified line to the output sequence, performing line wrapping if necessary.

Parameters:
lines The sequence of display lines, must not be null.
line The line to add, must not be null.
indentSize The size of each indentation, must not be negative.
lineLength The length of the line, must not be negative.
    private static void toLinesList<StringlinesString lineint indentSizeint lineLength )
    {
        int lineIndent = getIndentLevelline );
        StringBuilder buf = new StringBuilder( 256 );
        String[] tokens = line.split" +" );
        for ( String token : tokens )
        {
            if ( buf.length() > 0 )
            {
                if ( buf.length() + token.length() >= lineLength )
                {
                    lines.addbuf.toString() );
                    buf.setLength( 0 );
                    buf.appendrepeat" "lineIndent * indentSize ) );
                }
                else
                {
                    buf.append' ' );
                }
            }
            for ( int j = 0; j < token.length(); j++ )
            {
                char c = token.charAtj );
                if ( c == '\t' )
                {
                    buf.appendrepeat" "indentSize - buf.length() % indentSize ) );
                }
                else if ( c == '\u00A0' )
                {
                    buf.append' ' );
                }
                else
                {
                    buf.appendc );
                }
            }
        }
        lines.addbuf.toString() );
    }

    
Gets the indentation level of the specified line.

Parameters:
line The line whose indentation level should be retrieved, must not be null.
Returns:
The indentation level of the line.
    private static int getIndentLevelString line )
    {
        int level = 0;
        for ( int i = 0; i < line.length() && line.charAti ) == '\t'i++ )
        {
            level++;
        }
        for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
        {
            if ( line.charAti ) == '\t' )
            {
                level++;
                break;
            }
        }
        return level;
    }
    
    private String getPropertyFromExpressionString expression )
    {
        if ( expression != null && expression.startsWith"${" ) && expression.endsWith"}" )
            && !expression.substring( 2 ).contains"${" ) )
        {
            // expression="${xxx}" -> property="xxx"
            return expression.substring( 2, expression.length() - 1 );
        }
        // no property can be extracted
        return null;
    }
New to GrepCode? Check out our FAQ X