1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
42
43
44
45 public class ExpressionsMojo
46 extends AbstractHelpMojo
47 {
48
49 private static final String NO_DESCRIPTION_AVAILABLE = "No description available.";
50
51
52
53
54
55
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
118
119
120
121
122
123
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
161
162
163
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 }