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 | 0 | 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 | 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 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private static List getExpressionsRoot() |
126 | |
throws MojoFailureException, MojoExecutionException |
127 | |
{ |
128 | |
try |
129 | |
{ |
130 | 0 | Field f = ExpressionDocumenter.class.getDeclaredField( "EXPRESSION_ROOTS" ); |
131 | 0 | f.setAccessible( true ); |
132 | 0 | String[] value = (String[]) f.get( new ExpressionDocumenter() ); |
133 | 0 | if ( value == null ) |
134 | |
{ |
135 | 0 | throw new MojoExecutionException( "org.apache.maven.usability.plugin.ExpressionDocumenter#" |
136 | |
+ "EXPRESSION_ROOTS has no value." ); |
137 | |
} |
138 | |
|
139 | 0 | 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 | |
|
161 | |
|
162 | |
|
163 | |
|
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 | |
} |