1
2 package de.juplo.plugins.hibernate;
3
4 import org.apache.maven.plugin.AbstractMojo;
5 import org.apache.maven.plugin.MojoExecutionException;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11 import org.xml.sax.SAXException;
12
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20
21
22
23
24
25
26
27
28
29 public class HelpMojo
30 extends AbstractMojo
31 {
32
33
34
35
36
37 private boolean detail;
38
39
40
41
42
43
44 private java.lang.String goal;
45
46
47
48
49
50
51 private int lineLength;
52
53
54
55
56
57
58 private int indentSize;
59
60
61 private static final String PLUGIN_HELP_PATH =
62 "/META-INF/maven/de.juplo/hibernate-maven-plugin/plugin-help.xml";
63
64 private static final int DEFAULT_LINE_LENGTH = 80;
65
66 private Document build()
67 throws MojoExecutionException
68 {
69 getLog().debug( "load plugin-help.xml: " + PLUGIN_HELP_PATH );
70 InputStream is = null;
71 try
72 {
73 is = getClass().getResourceAsStream( PLUGIN_HELP_PATH );
74 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
75 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
76 return dBuilder.parse( is );
77 }
78 catch ( IOException e )
79 {
80 throw new MojoExecutionException( e.getMessage(), e );
81 }
82 catch ( ParserConfigurationException e )
83 {
84 throw new MojoExecutionException( e.getMessage(), e );
85 }
86 catch ( SAXException e )
87 {
88 throw new MojoExecutionException( e.getMessage(), e );
89 }
90 finally
91 {
92 if ( is != null )
93 {
94 try
95 {
96 is.close();
97 }
98 catch ( IOException e )
99 {
100 throw new MojoExecutionException( e.getMessage(), e );
101 }
102 }
103 }
104 }
105
106
107
108
109 public void execute()
110 throws MojoExecutionException
111 {
112 if ( lineLength <= 0 )
113 {
114 getLog().warn( "The parameter 'lineLength' should be positive, using '80' as default." );
115 lineLength = DEFAULT_LINE_LENGTH;
116 }
117 if ( indentSize <= 0 )
118 {
119 getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
120 indentSize = 2;
121 }
122
123 Document doc = build();
124
125 StringBuilder sb = new StringBuilder();
126 Node plugin = getSingleChild( doc, "plugin" );
127
128
129 String name = getValue( plugin, "name" );
130 String version = getValue( plugin, "version" );
131 String id = getValue( plugin, "groupId" ) + ":" + getValue( plugin, "artifactId" ) + ":" + version;
132 if ( isNotEmpty( name ) && !name.contains( id ) )
133 {
134 append( sb, name + " " + version, 0 );
135 }
136 else
137 {
138 if ( isNotEmpty( name ) )
139 {
140 append( sb, name, 0 );
141 }
142 else
143 {
144 append( sb, id, 0 );
145 }
146 }
147 append( sb, getValue( plugin, "description" ), 1 );
148 append( sb, "", 0 );
149
150
151 String goalPrefix = getValue( plugin, "goalPrefix" );
152
153 Node mojos1 = getSingleChild( plugin, "mojos" );
154
155 List<Node> mojos = findNamedChild( mojos1, "mojo" );
156
157 if ( goal == null || goal.length() <= 0 )
158 {
159 append( sb, "This plugin has " + mojos.size() + ( mojos.size() > 1 ? " goals:" : " goal:" ), 0 );
160 append( sb, "", 0 );
161 }
162
163 for ( Node mojo : mojos )
164 {
165 writeGoal( sb, goalPrefix, (Element) mojo );
166 }
167
168 if ( getLog().isInfoEnabled() )
169 {
170 getLog().info( sb.toString() );
171 }
172 }
173
174
175 private static boolean isNotEmpty( String string )
176 {
177 return string != null && string.length() > 0;
178 }
179
180 private String getValue( Node node, String elementName )
181 throws MojoExecutionException
182 {
183 return getSingleChild( node, elementName ).getTextContent();
184 }
185
186 private Node getSingleChild( Node node, String elementName )
187 throws MojoExecutionException
188 {
189 List<Node> namedChild = findNamedChild( node, elementName );
190 if ( namedChild.isEmpty() )
191 {
192 throw new MojoExecutionException( "Could not find " + elementName + " in plugin-help.xml" );
193 }
194 if ( namedChild.size() > 1 )
195 {
196 throw new MojoExecutionException( "Multiple " + elementName + " in plugin-help.xml" );
197 }
198 return namedChild.get( 0 );
199 }
200
201 private List<Node> findNamedChild( Node node, String elementName )
202 {
203 List<Node> result = new ArrayList<Node>();
204 NodeList childNodes = node.getChildNodes();
205 for ( int i = 0; i < childNodes.getLength(); i++ )
206 {
207 Node item = childNodes.item( i );
208 if ( elementName.equals( item.getNodeName() ) )
209 {
210 result.add( item );
211 }
212 }
213 return result;
214 }
215
216 private Node findSingleChild( Node node, String elementName )
217 throws MojoExecutionException
218 {
219 List<Node> elementsByTagName = findNamedChild( node, elementName );
220 if ( elementsByTagName.isEmpty() )
221 {
222 return null;
223 }
224 if ( elementsByTagName.size() > 1 )
225 {
226 throw new MojoExecutionException( "Multiple " + elementName + "in plugin-help.xml" );
227 }
228 return elementsByTagName.get( 0 );
229 }
230
231 private void writeGoal( StringBuilder sb, String goalPrefix, Element mojo )
232 throws MojoExecutionException
233 {
234 String mojoGoal = getValue( mojo, "goal" );
235 Node configurationElement = findSingleChild( mojo, "configuration" );
236 Node description = findSingleChild( mojo, "description" );
237 if ( goal == null || goal.length() <= 0 || mojoGoal.equals( goal ) )
238 {
239 append( sb, goalPrefix + ":" + mojoGoal, 0 );
240 Node deprecated = findSingleChild( mojo, "deprecated" );
241 if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
242 {
243 append( sb, "Deprecated. " + deprecated.getTextContent(), 1 );
244 if ( detail && description != null )
245 {
246 append( sb, "", 0 );
247 append( sb, description.getTextContent(), 1 );
248 }
249 }
250 else if ( description != null )
251 {
252 append( sb, description.getTextContent(), 1 );
253 }
254 append( sb, "", 0 );
255
256 if ( detail )
257 {
258 Node parametersNode = getSingleChild( mojo, "parameters" );
259 List<Node> parameters = findNamedChild( parametersNode, "parameter" );
260 append( sb, "Available parameters:", 1 );
261 append( sb, "", 0 );
262
263 for ( Node parameter : parameters )
264 {
265 writeParameter( sb, parameter, configurationElement );
266 }
267 }
268 }
269 }
270
271 private void writeParameter( StringBuilder sb, Node parameter, Node configurationElement )
272 throws MojoExecutionException
273 {
274 String parameterName = getValue( parameter, "name" );
275 String parameterDescription = getValue( parameter, "description" );
276
277 Element fieldConfigurationElement = (Element) findSingleChild( configurationElement, parameterName );
278
279 String parameterDefaultValue = "";
280 if ( fieldConfigurationElement != null && fieldConfigurationElement.hasAttribute( "default-value" ) )
281 {
282 parameterDefaultValue = " (Default: " + fieldConfigurationElement.getAttribute( "default-value" ) + ")";
283 }
284 append( sb, parameterName + parameterDefaultValue, 2 );
285 Node deprecated = findSingleChild( parameter, "deprecated" );
286 if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
287 {
288 append( sb, "Deprecated. " + deprecated.getTextContent(), 3 );
289 append( sb, "", 0 );
290 }
291 append( sb, parameterDescription, 3 );
292 if ( "true".equals( getValue( parameter, "required" ) ) )
293 {
294 append( sb, "Required: Yes", 3 );
295 }
296 if ( ( fieldConfigurationElement != null ) && isNotEmpty( fieldConfigurationElement.getTextContent() ) )
297 {
298 String property = getPropertyFromExpression( fieldConfigurationElement.getTextContent() );
299 append( sb, "User property: " + property, 3 );
300 }
301
302 append( sb, "", 0 );
303 }
304
305
306
307
308
309
310
311
312
313
314 private static String repeat( String str, int repeat )
315 {
316 StringBuilder buffer = new StringBuilder( repeat * str.length() );
317
318 for ( int i = 0; i < repeat; i++ )
319 {
320 buffer.append( str );
321 }
322
323 return buffer.toString();
324 }
325
326
327
328
329
330
331
332
333
334 private void append( StringBuilder sb, String description, int indent )
335 {
336 for ( String line : toLines( description, indent, indentSize, lineLength ) )
337 {
338 sb.append( line ).append( '\n' );
339 }
340 }
341
342
343
344
345
346
347
348
349
350
351
352 private static List<String> toLines( String text, int indent, int indentSize, int lineLength )
353 {
354 List<String> lines = new ArrayList<String>();
355
356 String ind = repeat( "\t", indent );
357
358 String[] plainLines = text.split( "(\r\n)|(\r)|(\n)" );
359
360 for ( String plainLine : plainLines )
361 {
362 toLines( lines, ind + plainLine, indentSize, lineLength );
363 }
364
365 return lines;
366 }
367
368
369
370
371
372
373
374
375
376 private static void toLines( List<String> lines, String line, int indentSize, int lineLength )
377 {
378 int lineIndent = getIndentLevel( line );
379 StringBuilder buf = new StringBuilder( 256 );
380
381 String[] tokens = line.split( " +" );
382
383 for ( String token : tokens )
384 {
385 if ( buf.length() > 0 )
386 {
387 if ( buf.length() + token.length() >= lineLength )
388 {
389 lines.add( buf.toString() );
390 buf.setLength( 0 );
391 buf.append( repeat( " ", lineIndent * indentSize ) );
392 }
393 else
394 {
395 buf.append( ' ' );
396 }
397 }
398
399 for ( int j = 0; j < token.length(); j++ )
400 {
401 char c = token.charAt( j );
402 if ( c == '\t' )
403 {
404 buf.append( repeat( " ", indentSize - buf.length() % indentSize ) );
405 }
406 else if ( c == '\u00A0' )
407 {
408 buf.append( ' ' );
409 }
410 else
411 {
412 buf.append( c );
413 }
414 }
415 }
416 lines.add( buf.toString() );
417 }
418
419
420
421
422
423
424
425 private static int getIndentLevel( String line )
426 {
427 int level = 0;
428 for ( int i = 0; i < line.length() && line.charAt( i ) == '\t'; i++ )
429 {
430 level++;
431 }
432 for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
433 {
434 if ( line.charAt( i ) == '\t' )
435 {
436 level++;
437 break;
438 }
439 }
440 return level;
441 }
442
443 private String getPropertyFromExpression( String expression )
444 {
445 if ( expression != null && expression.startsWith( "${" ) && expression.endsWith( "}" )
446 && !expression.substring( 2 ).contains( "${" ) )
447 {
448
449 return expression.substring( 2, expression.length() - 1 );
450 }
451
452 return null;
453 }
454 }