001package org.apache.maven.plugin.descriptor;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.io.Reader;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.codehaus.plexus.component.repository.ComponentDependency;
028import org.codehaus.plexus.component.repository.ComponentRequirement;
029import org.codehaus.plexus.configuration.PlexusConfiguration;
030import org.codehaus.plexus.configuration.PlexusConfigurationException;
031import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
032import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
033import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
034
035/**
036 * @author Jason van Zyl
037 */
038public class PluginDescriptorBuilder
039{
040    public PluginDescriptor build( Reader reader )
041        throws PlexusConfigurationException
042    {
043        return build( reader, null );
044    }
045
046    public PluginDescriptor build( Reader reader, String source )
047        throws PlexusConfigurationException
048    {
049        PlexusConfiguration c = buildConfiguration( reader );
050
051        PluginDescriptor pluginDescriptor = new PluginDescriptor();
052
053        pluginDescriptor.setSource( source );
054        pluginDescriptor.setGroupId( c.getChild( "groupId" ).getValue() );
055        pluginDescriptor.setArtifactId( c.getChild( "artifactId" ).getValue() );
056        pluginDescriptor.setVersion( c.getChild( "version" ).getValue() );
057        pluginDescriptor.setGoalPrefix( c.getChild( "goalPrefix" ).getValue() );
058
059        pluginDescriptor.setName( c.getChild( "name" ).getValue() );
060        pluginDescriptor.setDescription( c.getChild( "description" ).getValue() );
061
062        String isolatedRealm = c.getChild( "isolatedRealm" ).getValue();
063
064        if ( isolatedRealm != null )
065        {
066            pluginDescriptor.setIsolatedRealm( Boolean.parseBoolean( isolatedRealm ) );
067        }
068
069        String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
070
071        if ( inheritedByDefault != null )
072        {
073            pluginDescriptor.setInheritedByDefault( Boolean.parseBoolean( inheritedByDefault ) );
074        }
075
076        // ----------------------------------------------------------------------
077        // Components
078        // ----------------------------------------------------------------------
079
080        PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" );
081
082        for ( PlexusConfiguration component : mojoConfigurations )
083        {
084            MojoDescriptor mojoDescriptor = buildComponentDescriptor( component, pluginDescriptor );
085
086            pluginDescriptor.addMojo( mojoDescriptor );
087        }
088
089        // ----------------------------------------------------------------------
090        // Dependencies
091        // ----------------------------------------------------------------------
092
093        PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" );
094
095        List<ComponentDependency> dependencies = new ArrayList<ComponentDependency>();
096
097        for ( PlexusConfiguration d : dependencyConfigurations )
098        {
099            ComponentDependency cd = new ComponentDependency();
100
101            cd.setArtifactId( d.getChild( "artifactId" ).getValue() );
102
103            cd.setGroupId( d.getChild( "groupId" ).getValue() );
104
105            cd.setType( d.getChild( "type" ).getValue() );
106
107            cd.setVersion( d.getChild( "version" ).getValue() );
108
109            dependencies.add( cd );
110        }
111
112        pluginDescriptor.setDependencies( dependencies );
113
114        return pluginDescriptor;
115    }
116
117    public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
118        throws PlexusConfigurationException
119    {
120        MojoDescriptor mojo = new MojoDescriptor();
121        mojo.setPluginDescriptor( pluginDescriptor );
122
123        mojo.setGoal( c.getChild( "goal" ).getValue() );
124
125        mojo.setImplementation( c.getChild( "implementation" ).getValue() );
126
127        PlexusConfiguration langConfig = c.getChild( "language" );
128
129        if ( langConfig != null )
130        {
131            mojo.setLanguage( langConfig.getValue() );
132        }
133
134        PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
135
136        if ( configuratorConfig != null )
137        {
138            mojo.setComponentConfigurator( configuratorConfig.getValue() );
139        }
140
141        PlexusConfiguration composerConfig = c.getChild( "composer" );
142
143        if ( composerConfig != null )
144        {
145            mojo.setComponentComposer( composerConfig.getValue() );
146        }
147
148        String since = c.getChild( "since" ).getValue();
149
150        if ( since != null )
151        {
152            mojo.setSince( since );
153        }
154
155        PlexusConfiguration deprecated = c.getChild( "deprecated", false );
156
157        if ( deprecated != null )
158        {
159            mojo.setDeprecated( deprecated.getValue() );
160        }
161
162        String phase = c.getChild( "phase" ).getValue();
163
164        if ( phase != null )
165        {
166            mojo.setPhase( phase );
167        }
168
169        String executePhase = c.getChild( "executePhase" ).getValue();
170
171        if ( executePhase != null )
172        {
173            mojo.setExecutePhase( executePhase );
174        }
175
176        String executeMojo = c.getChild( "executeGoal" ).getValue();
177
178        if ( executeMojo != null )
179        {
180            mojo.setExecuteGoal( executeMojo );
181        }
182
183        String executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
184
185        if ( executeLifecycle != null )
186        {
187            mojo.setExecuteLifecycle( executeLifecycle );
188        }
189
190        mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
191
192        mojo.setDescription( c.getChild( "description" ).getValue() );
193
194        PlexusConfiguration dependencyResolution = c.getChild( "requiresDependencyResolution", false );
195
196        if ( dependencyResolution != null )
197        {
198            mojo.setDependencyResolutionRequired( dependencyResolution.getValue() );
199        }
200
201        PlexusConfiguration dependencyCollection = c.getChild( "requiresDependencyCollection", false );
202
203        if ( dependencyCollection != null )
204        {
205            mojo.setDependencyCollectionRequired( dependencyCollection.getValue() );
206        }
207
208        String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
209
210        if ( directInvocationOnly != null )
211        {
212            mojo.setDirectInvocationOnly( Boolean.parseBoolean( directInvocationOnly ) );
213        }
214
215        String requiresProject = c.getChild( "requiresProject" ).getValue();
216
217        if ( requiresProject != null )
218        {
219            mojo.setProjectRequired( Boolean.parseBoolean( requiresProject ) );
220        }
221
222        String requiresReports = c.getChild( "requiresReports" ).getValue();
223
224        if ( requiresReports != null )
225        {
226            mojo.setRequiresReports( Boolean.parseBoolean( requiresReports ) );
227        }
228
229        String aggregator = c.getChild( "aggregator" ).getValue();
230
231        if ( aggregator != null )
232        {
233            mojo.setAggregator( Boolean.parseBoolean( aggregator ) );
234        }
235
236        String requiresOnline = c.getChild( "requiresOnline" ).getValue();
237
238        if ( requiresOnline != null )
239        {
240            mojo.setOnlineRequired( Boolean.parseBoolean( requiresOnline ) );
241        }
242
243        String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
244
245        if ( inheritedByDefault != null )
246        {
247            mojo.setInheritedByDefault( Boolean.parseBoolean( inheritedByDefault ) );
248        }
249
250        String threadSafe = c.getChild( "threadSafe" ).getValue();
251
252        if ( threadSafe != null )
253        {
254            mojo.setThreadSafe( Boolean.parseBoolean( threadSafe ) );
255        }
256
257        // ----------------------------------------------------------------------
258        // Configuration
259        // ----------------------------------------------------------------------
260
261        PlexusConfiguration mojoConfig = c.getChild( "configuration" );
262        mojo.setMojoConfiguration( mojoConfig );
263
264        // ----------------------------------------------------------------------
265        // Parameters
266        // ----------------------------------------------------------------------
267
268        PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
269
270        List<Parameter> parameters = new ArrayList<Parameter>();
271
272        for ( PlexusConfiguration d : parameterConfigurations )
273        {
274            Parameter parameter = new Parameter();
275
276            parameter.setName( d.getChild( "name" ).getValue() );
277
278            parameter.setAlias( d.getChild( "alias" ).getValue() );
279
280            parameter.setType( d.getChild( "type" ).getValue() );
281
282            String required = d.getChild( "required" ).getValue();
283
284            parameter.setRequired( Boolean.parseBoolean( required ) );
285
286            PlexusConfiguration editableConfig = d.getChild( "editable" );
287
288            // we need the null check for pre-build legacy plugins...
289            if ( editableConfig != null )
290            {
291                String editable = d.getChild( "editable" ).getValue();
292
293                parameter.setEditable( editable == null || Boolean.parseBoolean( editable ) );
294            }
295
296            parameter.setDescription( d.getChild( "description" ).getValue() );
297
298            parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
299
300            parameter.setImplementation( d.getChild( "implementation" ).getValue() );
301
302            PlexusConfiguration paramConfig = mojoConfig.getChild( parameter.getName(), false );
303            if ( paramConfig != null )
304            {
305                parameter.setExpression( paramConfig.getValue( null ) );
306                parameter.setDefaultValue( paramConfig.getAttribute( "default-value" ) );
307            }
308
309            parameters.add( parameter );
310        }
311
312        mojo.setParameters( parameters );
313
314        // TODO: this should not need to be handed off...
315
316        // ----------------------------------------------------------------------
317        // Requirements
318        // ----------------------------------------------------------------------
319
320        PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
321
322        for ( PlexusConfiguration requirement : requirements )
323        {
324            ComponentRequirement cr = new ComponentRequirement();
325
326            cr.setRole( requirement.getChild( "role" ).getValue() );
327
328            cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
329
330            cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
331
332            mojo.addRequirement( cr );
333        }
334
335        return mojo;
336    }
337
338    // ----------------------------------------------------------------------
339    //
340    // ----------------------------------------------------------------------
341
342    public PlexusConfiguration buildConfiguration( Reader configuration )
343        throws PlexusConfigurationException
344    {
345        try
346        {
347            return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
348        }
349        catch ( IOException e )
350        {
351            throw new PlexusConfigurationException( e.getMessage(), e );
352        }
353        catch ( XmlPullParserException e )
354        {
355            throw new PlexusConfigurationException( e.getMessage(), e );
356        }
357    }
358}