KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > descriptor > PluginDescriptorBuilder


1 package org.apache.maven.plugin.descriptor;
2
3 import org.codehaus.plexus.component.repository.ComponentDependency;
4 import org.codehaus.plexus.component.repository.ComponentRequirement;
5 import org.codehaus.plexus.configuration.PlexusConfiguration;
6 import org.codehaus.plexus.configuration.PlexusConfigurationException;
7 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
8 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
9 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
10
11 import java.io.IOException JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl</a>
18  * @version $Id: PluginDescriptorBuilder.java 315026 2005-10-12 20:22:36Z jdcasey $
19  */

20 public class PluginDescriptorBuilder
21 {
22     public PluginDescriptor build( Reader JavaDoc reader )
23         throws PlexusConfigurationException
24     {
25         return build( reader, null );
26     }
27
28     public PluginDescriptor build( Reader JavaDoc reader, String JavaDoc source )
29         throws PlexusConfigurationException
30     {
31         PlexusConfiguration c = buildConfiguration( reader );
32
33         PluginDescriptor pluginDescriptor = new PluginDescriptor();
34
35         pluginDescriptor.setSource( source );
36         pluginDescriptor.setGroupId( c.getChild( "groupId" ).getValue() );
37         pluginDescriptor.setArtifactId( c.getChild( "artifactId" ).getValue() );
38         pluginDescriptor.setVersion( c.getChild( "version" ).getValue() );
39         pluginDescriptor.setGoalPrefix( c.getChild( "goalPrefix" ).getValue() );
40         
41         pluginDescriptor.setName( c.getChild( "name" ).getValue() );
42         pluginDescriptor.setDescription( c.getChild( "description" ).getValue() );
43
44         String JavaDoc isolatedRealm = c.getChild( "isolatedRealm" ).getValue();
45
46         if ( isolatedRealm != null )
47         {
48             pluginDescriptor.setIsolatedRealm( Boolean.valueOf( isolatedRealm ).booleanValue() );
49         }
50
51         String JavaDoc inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
52
53         if ( inheritedByDefault != null )
54         {
55             pluginDescriptor.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
56         }
57
58         // ----------------------------------------------------------------------
59
// Components
60
// ----------------------------------------------------------------------
61

62         PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" );
63
64         for ( int i = 0; i < mojoConfigurations.length; i++ )
65         {
66             PlexusConfiguration component = mojoConfigurations[i];
67
68             MojoDescriptor mojoDescriptor = buildComponentDescriptor( component, pluginDescriptor );
69
70             pluginDescriptor.addMojo( mojoDescriptor );
71         }
72
73         // ----------------------------------------------------------------------
74
// Dependencies
75
// ----------------------------------------------------------------------
76

77         PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" );
78
79         List JavaDoc dependencies = new ArrayList JavaDoc();
80
81         for ( int i = 0; i < dependencyConfigurations.length; i++ )
82         {
83             PlexusConfiguration d = dependencyConfigurations[i];
84
85             ComponentDependency cd = new ComponentDependency();
86
87             cd.setArtifactId( d.getChild( "artifactId" ).getValue() );
88
89             cd.setGroupId( d.getChild( "groupId" ).getValue() );
90
91             cd.setType( d.getChild( "type" ).getValue() );
92
93             cd.setVersion( d.getChild( "version" ).getValue() );
94
95             dependencies.add( cd );
96         }
97
98         pluginDescriptor.setDependencies( dependencies );
99
100         return pluginDescriptor;
101     }
102
103     public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
104         throws PlexusConfigurationException
105     {
106         MojoDescriptor mojo = new MojoDescriptor();
107         mojo.setPluginDescriptor( pluginDescriptor );
108
109         mojo.setGoal( c.getChild( "goal" ).getValue() );
110         
111         mojo.setImplementation( c.getChild( "implementation" ).getValue() );
112
113         PlexusConfiguration langConfig = c.getChild( "language" );
114
115         if ( langConfig != null )
116         {
117             mojo.setLanguage( langConfig.getValue() );
118         }
119
120         PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
121
122         if ( configuratorConfig != null )
123         {
124             mojo.setComponentConfigurator( configuratorConfig.getValue() );
125         }
126
127         PlexusConfiguration composerConfig = c.getChild( "composer" );
128
129         if ( composerConfig != null )
130         {
131             mojo.setComponentComposer( composerConfig.getValue() );
132         }
133
134         String JavaDoc phase = c.getChild( "phase" ).getValue();
135
136         if ( phase != null )
137         {
138             mojo.setPhase( phase );
139         }
140
141         String JavaDoc executePhase = c.getChild( "executePhase" ).getValue();
142
143         if ( executePhase != null )
144         {
145             mojo.setExecutePhase( executePhase );
146         }
147
148         String JavaDoc executeMojo = c.getChild( "executeGoal" ).getValue();
149
150         if ( executeMojo != null )
151         {
152             mojo.setExecuteGoal( executeMojo );
153         }
154
155         String JavaDoc executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
156
157         if ( executeLifecycle != null )
158         {
159             mojo.setExecuteLifecycle( executeLifecycle );
160         }
161
162         mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
163
164         mojo.setDescription( c.getChild( "description" ).getValue() );
165
166         String JavaDoc dependencyResolution = c.getChild( "requiresDependencyResolution" ).getValue();
167
168         if ( dependencyResolution != null )
169         {
170             mojo.setDependencyResolutionRequired( dependencyResolution );
171         }
172
173         String JavaDoc directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
174
175         if ( directInvocationOnly != null )
176         {
177             mojo.setDirectInvocationOnly( Boolean.valueOf( directInvocationOnly ).booleanValue() );
178         }
179
180         String JavaDoc requiresProject = c.getChild( "requiresProject" ).getValue();
181
182         if ( requiresProject != null )
183         {
184             mojo.setProjectRequired( Boolean.valueOf( requiresProject ).booleanValue() );
185         }
186
187         String JavaDoc requiresReports = c.getChild( "requiresReports" ).getValue();
188
189         if ( requiresReports != null )
190         {
191             mojo.setRequiresReports( Boolean.valueOf( requiresReports ).booleanValue() );
192         }
193
194         String JavaDoc aggregator = c.getChild( "aggregator" ).getValue();
195
196         if ( aggregator != null )
197         {
198             mojo.setAggregator( Boolean.valueOf( aggregator ).booleanValue() );
199         }
200
201         String JavaDoc requiresOnline = c.getChild( "requiresOnline" ).getValue();
202
203         if ( requiresOnline != null )
204         {
205             mojo.setOnlineRequired( Boolean.valueOf( requiresOnline ).booleanValue() );
206         }
207
208         String JavaDoc inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
209
210         if ( inheritedByDefault != null )
211         {
212             mojo.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
213         }
214
215         // ----------------------------------------------------------------------
216
// Parameters
217
// ----------------------------------------------------------------------
218

219         PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
220
221         List JavaDoc parameters = new ArrayList JavaDoc();
222
223         for ( int i = 0; i < parameterConfigurations.length; i++ )
224         {
225             PlexusConfiguration d = parameterConfigurations[i];
226
227             Parameter parameter = new Parameter();
228
229             parameter.setName( d.getChild( "name" ).getValue() );
230
231             parameter.setAlias( d.getChild( "alias" ).getValue() );
232
233             parameter.setType( d.getChild( "type" ).getValue() );
234
235             String JavaDoc required = d.getChild( "required" ).getValue();
236
237             parameter.setRequired( Boolean.valueOf( required ).booleanValue() );
238
239             PlexusConfiguration editableConfig = d.getChild( "editable" );
240
241             // we need the null check for pre-build legacy plugins...
242
if ( editableConfig != null )
243             {
244                 String JavaDoc editable = d.getChild( "editable" ).getValue();
245
246                 parameter.setEditable( editable == null || Boolean.valueOf( editable ).booleanValue() );
247             }
248
249             parameter.setDescription( d.getChild( "description" ).getValue() );
250
251             parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
252
253             parameters.add( parameter );
254         }
255
256         mojo.setParameters( parameters );
257
258         // TODO: this should not need to be handed off...
259

260         // ----------------------------------------------------------------------
261
// Configuration
262
// ----------------------------------------------------------------------
263

264         mojo.setMojoConfiguration( c.getChild( "configuration" ) );
265
266         // TODO: Go back to this when we get the container ready to configure mojos...
267
// mojo.setConfiguration( c.getChild( "configuration" ) );
268

269         // ----------------------------------------------------------------------
270
// Requirements
271
// ----------------------------------------------------------------------
272

273         PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
274
275         for ( int i = 0; i < requirements.length; i++ )
276         {
277             PlexusConfiguration requirement = requirements[i];
278
279             ComponentRequirement cr = new ComponentRequirement();
280
281             cr.setRole( requirement.getChild( "role" ).getValue() );
282
283             cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
284
285             cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
286
287             mojo.addRequirement( cr );
288         }
289
290         return mojo;
291     }
292
293     // ----------------------------------------------------------------------
294
//
295
// ----------------------------------------------------------------------
296

297     public PlexusConfiguration buildConfiguration( Reader JavaDoc configuration )
298         throws PlexusConfigurationException
299     {
300         try
301         {
302             return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
303         }
304         catch ( IOException JavaDoc e )
305         {
306             throw new PlexusConfigurationException( "Error creating configuration", e );
307         }
308         catch ( XmlPullParserException e )
309         {
310             throw new PlexusConfigurationException( "Error creating configuration", e );
311         }
312     }
313 }
314
Popular Tags