View Javadoc

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  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              m = ExpressionDocumenter.load();
63          }
64          catch ( ExpressionDocumentationException e )
65          {
66              throw new MojoExecutionException( "ExpressionDocumentationException: " + e.getMessage(), e );
67          }
68  
69          StringBuffer sb = new StringBuffer();
70          sb.append( "Maven supports the following Plugin expressions:\n\n" );
71          for ( Iterator it = getExpressionsRoot().iterator(); it.hasNext(); )
72          {
73              String expression = (String) it.next();
74  
75              sb.append( "${" ).append( expression ).append( "}: " );
76              sb.append( NO_DESCRIPTION_AVAILABLE );
77              sb.append( "\n\n" );
78          }
79  
80          for ( Iterator it = m.keySet().iterator(); it.hasNext(); )
81          {
82              String key = (String) it.next();
83              Expression expression = (Expression) m.get( key );
84  
85              sb.append( "${" ).append( key ).append( "}: " );
86              sb.append( trimCDATA( expression.getDescription() ) );
87              sb.append( "\n\n" );
88          }
89  
90          if ( output != null )
91          {
92              try
93              {
94                  writeFile( output, sb );
95              }
96              catch ( IOException e )
97              {
98                  throw new MojoExecutionException( "Cannot write plugins expressions description to output: "
99                      + output, e );
100             }
101 
102             if ( getLog().isInfoEnabled() )
103             {
104                 getLog().info( "Wrote descriptions to: " + output );
105             }
106         }
107         else
108         {
109             if ( getLog().isInfoEnabled() )
110             {
111                 getLog().info( sb.toString() );
112             }
113         }
114     }
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             Field f = ExpressionDocumenter.class.getDeclaredField( "EXPRESSION_ROOTS" );
131             f.setAccessible( true );
132             String[] value = (String[]) f.get( new ExpressionDocumenter() );
133             if ( value == null )
134             {
135                 throw new MojoExecutionException( "org.apache.maven.usability.plugin.ExpressionDocumenter#"
136                     + "EXPRESSION_ROOTS has no value." );
137             }
138 
139             return Arrays.asList( value );
140         }
141         catch ( SecurityException e )
142         {
143             throw new MojoFailureException( "SecurityException: " + e.getMessage() );
144         }
145         catch ( IllegalArgumentException e )
146         {
147             throw new MojoFailureException( "IllegalArgumentException: " + e.getMessage() );
148         }
149         catch ( NoSuchFieldException e )
150         {
151             throw new MojoFailureException( "NoSuchFieldException: " + e.getMessage() );
152         }
153         catch ( IllegalAccessException e )
154         {
155             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         if ( StringUtils.isEmpty( description ) )
168         {
169             return NO_DESCRIPTION_AVAILABLE;
170         }
171 
172         StringBuffer sb = new StringBuffer();
173         String[] lines = StringUtils.split( description, "\r\n" );
174         for ( int i = 0; i < lines.length; i++ )
175         {
176             sb.append( StringUtils.trim( lines[i] ) ).append( " " );
177         }
178 
179         return sb.toString();
180     }
181 }