Coverage Report - org.apache.maven.plugins.help.ExpressionsMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionsMojo
10 %
5/51
11 %
2/18
8,667
 
 1  
 package org.apache.maven.plugins.help;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.lang.reflect.Field;
 24  
 import java.util.Arrays;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.maven.plugin.MojoFailureException;
 31  
 import org.apache.maven.usability.plugin.Expression;
 32  
 import org.apache.maven.usability.plugin.ExpressionDocumentationException;
 33  
 import org.apache.maven.usability.plugin.ExpressionDocumenter;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  
 /**
 37  
  * Displays the supported Plugin expressions used by Maven.
 38  
  *
 39  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 40  
  * @version $Id: ExpressionsMojo.java 689770 2008-08-28 09:47:18Z vsiveton $
 41  
  * @since 2.1
 42  
  * @goal expressions
 43  
  * @requiresProject false
 44  
  */
 45  0
 public class ExpressionsMojo
 46  
     extends AbstractHelpMojo
 47  
 {
 48  
     /** English sentence when no description exists */
 49  
     private static final String NO_DESCRIPTION_AVAILABLE = "No description available.";
 50  
 
 51  
     // ----------------------------------------------------------------------
 52  
     // Public methods
 53  
     // ----------------------------------------------------------------------
 54  
 
 55  
     /** {@inheritDoc} */
 56  
     public void execute()
 57  
         throws MojoExecutionException, MojoFailureException
 58  
     {
 59  
         Map m;
 60  
         try
 61  
         {
 62  0
             m = ExpressionDocumenter.load();
 63  
         }
 64  0
         catch ( ExpressionDocumentationException e )
 65  
         {
 66  0
             throw new MojoExecutionException( "ExpressionDocumentationException: " + e.getMessage(), e );
 67  0
         }
 68  
 
 69  0
         StringBuffer sb = new StringBuffer();
 70  0
         sb.append( "Maven supports the following Plugin expressions:\n\n" );
 71  0
         for ( Iterator it = getExpressionsRoot().iterator(); it.hasNext(); )
 72  
         {
 73  0
             String expression = (String) it.next();
 74  
 
 75  0
             sb.append( "${" ).append( expression ).append( "}: " );
 76  0
             sb.append( NO_DESCRIPTION_AVAILABLE );
 77  0
             sb.append( "\n\n" );
 78  0
         }
 79  
 
 80  0
         for ( Iterator it = m.keySet().iterator(); it.hasNext(); )
 81  
         {
 82  0
             String key = (String) it.next();
 83  0
             Expression expression = (Expression) m.get( key );
 84  
 
 85  0
             sb.append( "${" ).append( key ).append( "}: " );
 86  0
             sb.append( trimCDATA( expression.getDescription() ) );
 87  0
             sb.append( "\n\n" );
 88  0
         }
 89  
 
 90  0
         if ( output != null )
 91  
         {
 92  
             try
 93  
             {
 94  0
                 writeFile( output, sb );
 95  
             }
 96  0
             catch ( IOException e )
 97  
             {
 98  0
                 throw new MojoExecutionException( "Cannot write plugins expressions description to output: "
 99  
                     + output, e );
 100  0
             }
 101  
 
 102  0
             if ( getLog().isInfoEnabled() )
 103  
             {
 104  0
                 getLog().info( "Wrote descriptions to: " + output );
 105  
             }
 106  
         }
 107  
         else
 108  
         {
 109  0
             if ( getLog().isInfoEnabled() )
 110  
             {
 111  0
                 getLog().info( sb.toString() );
 112  
             }
 113  
         }
 114  0
     }
 115  
 
 116  
     // ----------------------------------------------------------------------
 117  
     // Private methods
 118  
     // ----------------------------------------------------------------------
 119  
 
 120  
     /**
 121  
      * @return the value of the private static field <code>ExpressionDocumenter#EXPRESSION_ROOTS</code>.
 122  
      * @throws MojoFailureException if any reflection exceptions occur
 123  
      * @throws MojoExecutionException if no value exists for <code>ExpressionDocumenter#EXPRESSION_ROOTS</code>
 124  
      */
 125  
     private static List getExpressionsRoot()
 126  
         throws MojoFailureException, MojoExecutionException
 127  
     {
 128  
         try
 129  
         {
 130  2
             Field f = ExpressionDocumenter.class.getDeclaredField( "EXPRESSION_ROOTS" );
 131  1
             f.setAccessible( true );
 132  1
             String[] value = (String[]) f.get( new ExpressionDocumenter() );
 133  1
             if ( value == null )
 134  
             {
 135  0
                 throw new MojoExecutionException( "org.apache.maven.usability.plugin.ExpressionDocumenter#"
 136  
                     + "EXPRESSION_ROOTS has no value." );
 137  
             }
 138  
 
 139  1
             return Arrays.asList( value );
 140  
         }
 141  0
         catch ( SecurityException e )
 142  
         {
 143  0
             throw new MojoFailureException( "SecurityException: " + e.getMessage() );
 144  
         }
 145  0
         catch ( IllegalArgumentException e )
 146  
         {
 147  0
             throw new MojoFailureException( "IllegalArgumentException: " + e.getMessage() );
 148  
         }
 149  0
         catch ( NoSuchFieldException e )
 150  
         {
 151  0
             throw new MojoFailureException( "NoSuchFieldException: " + e.getMessage() );
 152  
         }
 153  0
         catch ( IllegalAccessException e )
 154  
         {
 155  0
             throw new MojoFailureException( "IllegalAccessException: " + e.getMessage() );
 156  
         }
 157  
     }
 158  
 
 159  
     /**
 160  
      * @param description could be null
 161  
      * @return the given description without any new line, or <code>NO_DESCRIPTION_AVAILABLE</code> value if
 162  
      * <code>description</code> is null.
 163  
      * @see #NO_DESCRIPTION_AVAILABLE
 164  
      */
 165  
     private static String trimCDATA( String description )
 166  
     {
 167  0
         if ( StringUtils.isEmpty( description ) )
 168  
         {
 169  0
             return NO_DESCRIPTION_AVAILABLE;
 170  
         }
 171  
 
 172  0
         StringBuffer sb = new StringBuffer();
 173  0
         String[] lines = StringUtils.split( description, "\r\n" );
 174  0
         for ( int i = 0; i < lines.length; i++ )
 175  
         {
 176  0
             sb.append( StringUtils.trim( lines[i] ) ).append( " " );
 177  
         }
 178  
 
 179  0
         return sb.toString();
 180  
     }
 181  
 }