KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.maven.plugin.descriptor.Requirement;
23 import org.apache.maven.tools.plugin.util.PluginUtils;
24 import org.codehaus.plexus.util.IOUtil;
25 import org.codehaus.plexus.util.StringUtils;
26 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
27 import org.codehaus.plexus.util.xml.XMLWriter;
28
29 import java.io.File JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * @todo add example usage tag that can be shown in the doco
41  * @todo need to add validation directives so that systems embedding maven2 can
42  * get validation directives to help users in IDEs.
43  */

44 public class PluginDescriptorGenerator
45     implements Generator
46 {
47     public void execute( File JavaDoc destinationDirectory, PluginDescriptor pluginDescriptor )
48         throws IOException JavaDoc
49     {
50         File JavaDoc f = new File JavaDoc( destinationDirectory, "plugin.xml" );
51
52         if ( !f.getParentFile().exists() )
53         {
54             f.getParentFile().mkdirs();
55         }
56
57         FileWriter JavaDoc writer = null;
58         try
59         {
60             writer = new FileWriter JavaDoc( f );
61
62             XMLWriter w = new PrettyPrintXMLWriter( writer );
63
64             w.startElement( "plugin" );
65
66             element( w, "description", pluginDescriptor.getDescription() );
67
68             element( w, "groupId", pluginDescriptor.getGroupId() );
69
70             element( w, "artifactId", pluginDescriptor.getArtifactId() );
71
72             element( w, "version", pluginDescriptor.getVersion() );
73
74             element( w, "goalPrefix", pluginDescriptor.getGoalPrefix() );
75
76             element( w, "isolatedRealm", "" + pluginDescriptor.isIsolatedRealm() );
77
78             element( w, "inheritedByDefault", "" + pluginDescriptor.isInheritedByDefault() );
79
80             w.startElement( "mojos" );
81
82             if ( pluginDescriptor.getMojos() != null )
83             {
84                 for ( Iterator JavaDoc it = pluginDescriptor.getMojos().iterator(); it.hasNext(); )
85                 {
86                     MojoDescriptor descriptor = (MojoDescriptor) it.next();
87                     processMojoDescriptor( descriptor, w );
88                 }
89             }
90
91             w.endElement();
92
93             PluginUtils.writeDependencies( w, pluginDescriptor );
94
95             w.endElement();
96
97             writer.flush();
98         }
99         finally
100         {
101             IOUtil.close( writer );
102         }
103     }
104
105     protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w )
106     {
107         w.startElement( "mojo" );
108
109         // ----------------------------------------------------------------------
110
//
111
// ----------------------------------------------------------------------
112

113         w.startElement( "goal" );
114
115         w.writeText( mojoDescriptor.getGoal() );
116
117         w.endElement();
118
119         // ----------------------------------------------------------------------
120
//
121
// ----------------------------------------------------------------------
122

123         String JavaDoc description = mojoDescriptor.getDescription();
124         
125         if ( description != null )
126         {
127             w.startElement( "description" );
128
129             w.writeText( mojoDescriptor.getDescription() );
130
131             w.endElement();
132         }
133
134         // ----------------------------------------------------------------------
135
//
136
// ----------------------------------------------------------------------
137

138         if ( mojoDescriptor.isDependencyResolutionRequired() != null )
139         {
140             element( w, "requiresDependencyResolution", mojoDescriptor.isDependencyResolutionRequired() );
141         }
142
143         // ----------------------------------------------------------------------
144
//
145
// ----------------------------------------------------------------------
146

147         element( w, "requiresDirectInvocation", "" + mojoDescriptor.isDirectInvocationOnly() );
148
149         // ----------------------------------------------------------------------
150
//
151
// ----------------------------------------------------------------------
152

153         element( w, "requiresProject", "" + mojoDescriptor.isProjectRequired() );
154
155         // ----------------------------------------------------------------------
156
//
157
// ----------------------------------------------------------------------
158

159         element( w, "requiresReports", "" + mojoDescriptor.isRequiresReports() );
160
161         // ----------------------------------------------------------------------
162
//
163
// ----------------------------------------------------------------------
164

165         element( w, "aggregator", "" + mojoDescriptor.isAggregator() );
166
167         // ----------------------------------------------------------------------
168
//
169
// ----------------------------------------------------------------------
170

171         element( w, "requiresOnline", "" + mojoDescriptor.isOnlineRequired() );
172
173         // ----------------------------------------------------------------------
174
//
175
// ----------------------------------------------------------------------
176

177         element( w, "inheritedByDefault", "" + mojoDescriptor.isInheritedByDefault() );
178
179         // ----------------------------------------------------------------------
180
//
181
// ----------------------------------------------------------------------
182

183         if ( mojoDescriptor.getPhase() != null )
184         {
185             element( w, "phase", mojoDescriptor.getPhase() );
186         }
187
188         // ----------------------------------------------------------------------
189
//
190
// ----------------------------------------------------------------------
191

192         if ( mojoDescriptor.getExecutePhase() != null )
193         {
194             element( w, "executePhase", mojoDescriptor.getExecutePhase() );
195         }
196
197         if ( mojoDescriptor.getExecuteGoal() != null )
198         {
199             element( w, "executeGoal", mojoDescriptor.getExecuteGoal() );
200         }
201
202         if ( mojoDescriptor.getExecuteLifecycle() != null )
203         {
204             element( w, "executeLifecycle", mojoDescriptor.getExecuteLifecycle() );
205         }
206
207         // ----------------------------------------------------------------------
208
//
209
// ----------------------------------------------------------------------
210

211         w.startElement( "implementation" );
212
213         w.writeText( mojoDescriptor.getImplementation() );
214
215         w.endElement();
216
217         // ----------------------------------------------------------------------
218
//
219
// ----------------------------------------------------------------------
220

221         w.startElement( "language" );
222
223         w.writeText( mojoDescriptor.getLanguage() );
224
225         w.endElement();
226
227         // ----------------------------------------------------------------------
228
//
229
// ----------------------------------------------------------------------
230

231         if ( mojoDescriptor.getComponentConfigurator() != null )
232         {
233             w.startElement( "configurator" );
234
235             w.writeText( mojoDescriptor.getComponentConfigurator() );
236
237             w.endElement();
238         }
239
240         // ----------------------------------------------------------------------
241
//
242
// ----------------------------------------------------------------------
243

244         if ( mojoDescriptor.getComponentComposer() != null )
245         {
246             w.startElement( "composer" );
247
248             w.writeText( mojoDescriptor.getComponentComposer() );
249
250             w.endElement();
251         }
252
253         // ----------------------------------------------------------------------
254
//
255
// ----------------------------------------------------------------------
256

257         w.startElement( "instantiationStrategy" );
258
259         w.writeText( mojoDescriptor.getInstantiationStrategy() );
260
261         w.endElement();
262
263         // ----------------------------------------------------------------------
264
// Strategy for handling repeated reference to mojo in
265
// the calculated (decorated, resolved) execution stack
266
// ----------------------------------------------------------------------
267
w.startElement( "executionStrategy" );
268
269         w.writeText( mojoDescriptor.getExecutionStrategy() );
270
271         w.endElement();
272
273         // ----------------------------------------------------------------------
274
// Parameters
275
// ----------------------------------------------------------------------
276

277         List JavaDoc parameters = mojoDescriptor.getParameters();
278
279         w.startElement( "parameters" );
280
281         Map JavaDoc requirements = new HashMap JavaDoc();
282
283         Set JavaDoc configuration = new HashSet JavaDoc();
284
285         if ( parameters != null )
286         {
287             for ( int j = 0; j < parameters.size(); j++ )
288             {
289                 Parameter parameter = (Parameter) parameters.get( j );
290
291                 String JavaDoc expression = parameter.getExpression();
292
293                 if ( StringUtils.isNotEmpty( expression ) && expression.startsWith( "${component." ) )
294                 {
295                     // treat it as a component...a requirement, in other words.
296

297                     // remove "component." plus expression delimiters
298
String JavaDoc role = expression.substring( "${component.".length(), expression.length() - 1 );
299
300                     String JavaDoc roleHint = null;
301
302                     int posRoleHintSeparator;
303
304                     if ( ( posRoleHintSeparator = role.indexOf( "#" ) ) > 0 )
305                     {
306                         roleHint = role.substring( posRoleHintSeparator + 1 );
307
308                         role = role.substring( 0, posRoleHintSeparator );
309                     }
310
311                     // TODO: remove deprecated expression
312
requirements.put( parameter.getName(), new Requirement( role, roleHint ) );
313                 }
314                 else if ( parameter.getRequirement() != null )
315                 {
316                     requirements.put( parameter.getName(), parameter.getRequirement() );
317                 }
318                 else
319                 {
320                     // treat it as a normal parameter.
321

322                     w.startElement( "parameter" );
323
324                     element( w, "name", parameter.getName() );
325
326                     if ( parameter.getAlias() != null )
327                     {
328                         element( w, "alias", parameter.getAlias() );
329                     }
330
331                     element( w, "type", parameter.getType() );
332
333                     if ( parameter.getDeprecated() != null )
334                     {
335                         element( w, "deprecated", parameter.getDeprecated() );
336                     }
337
338                     element( w, "required", Boolean.toString( parameter.isRequired() ) );
339
340                     element( w, "editable", Boolean.toString( parameter.isEditable() ) );
341
342                     element( w, "description", parameter.getDescription() );
343
344                     if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) ||
345                         StringUtils.isNotEmpty( parameter.getExpression() ) )
346                     {
347                         configuration.add( parameter );
348                     }
349
350                     w.endElement();
351                 }
352
353             }
354         }
355
356         w.endElement();
357
358         // ----------------------------------------------------------------------
359
// Coinfiguration
360
// ----------------------------------------------------------------------
361

362         if ( !configuration.isEmpty() )
363         {
364             w.startElement( "configuration" );
365
366             for ( Iterator JavaDoc i = configuration.iterator(); i.hasNext(); )
367             {
368                 Parameter parameter = (Parameter) i.next();
369
370                 w.startElement( parameter.getName() );
371
372                 String JavaDoc type = parameter.getType();
373                 if ( type != null )
374                 {
375                     w.addAttribute( "implementation", type );
376                 }
377
378                 if ( parameter.getDefaultValue() != null )
379                 {
380                     w.addAttribute( "default-value", parameter.getDefaultValue() );
381                 }
382
383                 if ( parameter.getExpression() != null )
384                 {
385                     w.writeText( parameter.getExpression() );
386                 }
387
388                 w.endElement();
389             }
390
391             w.endElement();
392         }
393
394         // ----------------------------------------------------------------------
395
// Requirements
396
// ----------------------------------------------------------------------
397

398         if ( !requirements.isEmpty() )
399         {
400             w.startElement( "requirements" );
401
402             for ( Iterator JavaDoc i = requirements.keySet().iterator(); i.hasNext(); )
403             {
404                 String JavaDoc key = (String JavaDoc) i.next();
405                 Requirement requirement = (Requirement) requirements.get( key );
406
407                 w.startElement( "requirement" );
408
409                 element( w, "role", requirement.getRole() );
410
411                 if ( requirement.getRoleHint() != null )
412                 {
413                     element( w, "role-hint", requirement.getRoleHint() );
414                 }
415
416                 element( w, "field-name", key );
417
418                 w.endElement();
419             }
420
421             w.endElement();
422         }
423
424         // ----------------------------------------------------------------------
425
//
426
// ----------------------------------------------------------------------
427

428         w.endElement();
429     }
430
431     public void element( XMLWriter w, String JavaDoc name, String JavaDoc value )
432     {
433         w.startElement( name );
434
435         if ( value == null )
436         {
437             value = "";
438         }
439
440         w.writeText( value );
441
442         w.endElement();
443     }
444 }
445
Popular Tags