KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > generator > PluginXdocGenerator


1 package org.apache.maven.tools.plugin.generator;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.plugin.descriptor.MojoDescriptor;
20 import org.apache.maven.plugin.descriptor.Parameter;
21 import org.apache.maven.plugin.descriptor.PluginDescriptor;
22 import org.codehaus.plexus.util.IOUtil;
23 import org.codehaus.plexus.util.StringUtils;
24 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
25 import org.codehaus.plexus.util.xml.XMLWriter;
26
27 import java.io.File JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * @todo add example usage tag that can be shown in the doco
35  */

36 public class PluginXdocGenerator
37     implements Generator
38 {
39     public void execute( File JavaDoc destinationDirectory, PluginDescriptor pluginDescriptor )
40         throws IOException JavaDoc
41     {
42         writeOverview( destinationDirectory, pluginDescriptor );
43
44         for ( Iterator JavaDoc it = pluginDescriptor.getMojos().iterator(); it.hasNext(); )
45         {
46             MojoDescriptor descriptor = (MojoDescriptor) it.next();
47             processMojoDescriptor( descriptor, destinationDirectory );
48         }
49     }
50
51     protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, File JavaDoc destinationDirectory )
52         throws IOException JavaDoc
53     {
54         FileWriter JavaDoc writer = null;
55         try
56         {
57             writer = new FileWriter JavaDoc( new File JavaDoc( destinationDirectory, getMojoFilename( mojoDescriptor, "xml" ) ) );
58
59             writeBody( writer, mojoDescriptor );
60
61             writer.flush();
62         }
63         finally
64         {
65             IOUtil.close( writer );
66         }
67     }
68
69     private String JavaDoc getMojoFilename( MojoDescriptor mojo, String JavaDoc ext )
70     {
71         return mojo.getGoal() + "-mojo." + ext;
72     }
73
74     private void writeOverview( File JavaDoc destinationDirectory, PluginDescriptor pluginDescriptor )
75         throws IOException JavaDoc
76     {
77         FileWriter JavaDoc writer = null;
78         try
79         {
80             writer = new FileWriter JavaDoc( new File JavaDoc( destinationDirectory, "index.xml" ) );
81
82             writeOverview( writer, pluginDescriptor );
83
84             writer.flush();
85         }
86         finally
87         {
88             IOUtil.close( writer );
89         }
90     }
91
92     private void writeOverview( FileWriter JavaDoc writer, PluginDescriptor pluginDescriptor )
93     {
94         XMLWriter w = new PrettyPrintXMLWriter( writer );
95
96         w.startElement( "document" );
97
98         // ----------------------------------------------------------------------
99
//
100
// ----------------------------------------------------------------------
101

102         w.startElement( "properties" );
103
104         w.startElement( "title" );
105
106         // TODO: need a friendly name for a plugin
107
w.writeText( pluginDescriptor.getArtifactId() + " - Overview" );
108
109         w.endElement();
110
111         w.endElement();
112
113         // ----------------------------------------------------------------------
114
//
115
// ----------------------------------------------------------------------
116

117         w.startElement( "body" );
118
119         w.startElement( "section" );
120
121         // TODO: need a friendly name for a plugin
122
w.addAttribute( "name", pluginDescriptor.getArtifactId() );
123
124         // TODO: description of plugin, examples?
125

126         w.startElement( "p" );
127
128         w.writeText( "Goals available: " );
129
130         w.endElement();
131
132         writeGoalTable( pluginDescriptor, w );
133
134         w.endElement();
135
136         w.endElement();
137     }
138
139     private void writeGoalTable( PluginDescriptor pluginDescriptor, XMLWriter w )
140     {
141         w.startElement( "table" );
142
143         w.startElement( "tr" );
144
145         w.startElement( "th" );
146
147         w.writeText( "Goal" );
148
149         w.endElement();
150
151         w.startElement( "th" );
152
153         w.writeText( "Description" );
154
155         w.endElement();
156
157         w.endElement();
158
159         List JavaDoc mojos = pluginDescriptor.getMojos();
160
161         if ( mojos != null )
162         {
163             for ( Iterator JavaDoc i = mojos.iterator(); i.hasNext(); )
164             {
165                 MojoDescriptor mojo = (MojoDescriptor) i.next();
166
167                 w.startElement( "tr" );
168
169                 // ----------------------------------------------------------------------
170
//
171
// ----------------------------------------------------------------------
172

173                 w.startElement( "td" );
174
175                 String JavaDoc paramName = mojo.getFullGoalName();
176
177                 w.startElement( "a" );
178
179                 w.addAttribute( "href", getMojoFilename( mojo, "html" ) );
180
181                 w.startElement( "code" );
182
183                 w.writeText( paramName );
184
185                 w.endElement();
186
187                 w.endElement();
188
189                 w.endElement();
190
191                 // ----------------------------------------------------------------------
192
//
193
// ----------------------------------------------------------------------
194

195                 w.startElement( "td" );
196
197                 if ( StringUtils.isNotEmpty( mojo.getDescription() ) )
198                 {
199                     w.writeMarkup( mojo.getDescription() );
200                 }
201                 else
202                 {
203                     w.writeText( "No description." );
204                 }
205
206                 String JavaDoc deprecationWarning = mojo.getDeprecated();
207                 if ( deprecationWarning != null )
208                 {
209                     w.writeMarkup( "<br/><b>Deprecated:</b> " );
210                     w.writeMarkup( deprecationWarning );
211                     if ( deprecationWarning.length() == 0 )
212                     {
213                         w.writeText( "No reason given." );
214                     }
215
216                     w.endElement();
217                 }
218
219                 w.endElement();
220
221                 w.endElement();
222             }
223         }
224
225         w.endElement();
226
227         w.endElement();
228     }
229
230     private void writeBody( FileWriter JavaDoc writer, MojoDescriptor mojoDescriptor )
231     {
232         XMLWriter w = new PrettyPrintXMLWriter( writer );
233
234         w.startElement( "document" );
235
236         // ----------------------------------------------------------------------
237
//
238
// ----------------------------------------------------------------------
239

240         w.startElement( "properties" );
241
242         w.startElement( "title" );
243
244         // TODO: need a friendly name for a plugin
245
w.writeText( mojoDescriptor.getPluginDescriptor().getArtifactId() + " - " + mojoDescriptor.getFullGoalName() );
246
247         w.endElement(); // title
248

249         w.endElement(); // properties
250

251         // ----------------------------------------------------------------------
252
//
253
// ----------------------------------------------------------------------
254

255         w.startElement( "body" );
256
257         w.startElement( "section" );
258
259         w.addAttribute( "name", mojoDescriptor.getFullGoalName() );
260
261         w.startElement( "p" );
262
263         if ( mojoDescriptor.getDescription() != null )
264         {
265             w.writeMarkup( mojoDescriptor.getDescription() );
266         }
267         else
268         {
269             w.writeText( "No description." );
270         }
271
272         w.endElement(); // p
273

274         w.startElement( "p" );
275
276         w.writeText( "Parameters for the goal: " );
277
278         w.endElement(); // p
279

280         writeGoalParameterTable( mojoDescriptor, w );
281
282         w.endElement(); // section
283

284         w.endElement(); // body
285

286         w.endElement(); // document
287
}
288
289     private void writeGoalParameterTable( MojoDescriptor mojoDescriptor, XMLWriter w )
290     {
291         w.startElement( "table" );
292
293         w.startElement( "tr" );
294
295         w.startElement( "th" );
296
297         w.writeText( "Parameter" );
298
299         w.endElement(); // th
300

301         w.startElement( "th" );
302
303         w.writeText( "Type" );
304
305         w.endElement(); // th
306

307         w.startElement( "th" );
308
309         w.writeText( "Expression" );
310
311         w.endElement(); // th
312

313         w.startElement( "th" );
314
315         w.writeText( "Default Value" );
316
317         w.endElement(); // th
318

319         w.startElement( "th" );
320
321         w.writeText( "Description" );
322
323         w.endElement(); // th
324

325         w.endElement(); // tr
326

327         List JavaDoc parameters = mojoDescriptor.getParameters();
328
329         if ( parameters != null )
330         {
331             for ( int i = 0; i < parameters.size(); i++ )
332             {
333                 Parameter parameter = (Parameter) parameters.get( i );
334
335                 w.startElement( "tr" );
336
337                 // ----------------------------------------------------------------------
338
//
339
// ----------------------------------------------------------------------
340

341                 w.startElement( "td" );
342
343                 String JavaDoc paramName = parameter.getAlias();
344
345                 if ( StringUtils.isEmpty( paramName ) )
346                 {
347                     paramName = parameter.getName();
348                 }
349
350                 w.startElement( "code" );
351
352                 w.writeText( paramName );
353
354                 w.endElement(); // code
355

356                 if ( !parameter.isRequired() )
357                 {
358                     w.writeMarkup( " <i>(Optional)</i>" );
359                 }
360
361                 if ( parameter.getExpression() != null && parameter.getExpression().startsWith( "${component." ) )
362                 {
363                     w.writeMarkup( " <i>(Discovered)</i>" );
364                 }
365                 else if ( parameter.getRequirement() != null )
366                 {
367                     w.writeMarkup( " <i>(Discovered)</i>" );
368                 }
369
370                 w.endElement(); // td
371

372                 // ----------------------------------------------------------------------
373
//
374
// ----------------------------------------------------------------------
375

376                 w.startElement( "td" );
377
378                 w.startElement( "code" );
379
380                 w.addAttribute( "title", parameter.getType() );
381
382                 int index = parameter.getType().lastIndexOf( "." );
383                 if ( index >= 0 )
384                 {
385                     w.writeText( parameter.getType().substring( index + 1 ) );
386                 }
387                 else
388                 {
389                     w.writeText( parameter.getType() );
390                 }
391
392                 w.endElement(); // code
393

394                 w.endElement(); // td
395

396                 // ----------------------------------------------------------------------
397
//
398
// ----------------------------------------------------------------------
399

400                 w.startElement( "td" );
401
402                 w.startElement( "code" );
403
404                 if ( StringUtils.isNotEmpty( parameter.getExpression() ) &&
405                     !parameter.getExpression().startsWith( "${component." ) )
406                 {
407                     w.writeText( parameter.getExpression() );
408                 }
409                 else
410                 {
411                     w.writeText( "-" );
412                 }
413
414                 w.endElement(); // code
415

416                 w.endElement(); // td
417

418                 // ----------------------------------------------------------------------
419
//
420
// ----------------------------------------------------------------------
421

422                 w.startElement( "td" );
423
424                 w.startElement( "code" );
425
426                 if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) )
427                 {
428                     w.writeText( parameter.getDefaultValue() );
429                 }
430                 else
431                 {
432                     w.writeText( "-" );
433                 }
434
435                 w.endElement(); // code
436

437                 w.endElement(); // td
438

439                 // ----------------------------------------------------------------------
440
//
441
// ----------------------------------------------------------------------
442

443                 w.startElement( "td" );
444
445                 if ( StringUtils.isNotEmpty( parameter.getDescription() ) )
446                 {
447                     w.writeMarkup( parameter.getDescription() );
448                 }
449                 else
450                 {
451                     w.writeText( "No description." );
452                 }
453
454                 String JavaDoc deprecationWarning = parameter.getDeprecated();
455                 if ( deprecationWarning != null )
456                 {
457                     w.writeMarkup( "<br/><b>Deprecated:</b> " );
458                     w.writeMarkup( deprecationWarning );
459                     if ( deprecationWarning.length() == 0 )
460                     {
461                         w.writeText( "No reason given." );
462                     }
463                 }
464
465                 w.endElement(); // td
466

467                 w.endElement(); // tr
468
}
469         }
470
471         w.endElement(); // table
472
}
473 }
Popular Tags