Coverage Report - org.apache.maven.plugin.descriptor.PluginDescriptorBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginDescriptorBuilder
0 %
0/119
0 %
0/48
7,75
 
 1  
 package org.apache.maven.plugin.descriptor;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.codehaus.plexus.component.repository.ComponentDependency;
 23  
 import org.codehaus.plexus.component.repository.ComponentRequirement;
 24  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 25  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 26  
 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
 27  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 28  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 29  
 
 30  
 import java.io.IOException;
 31  
 import java.io.Reader;
 32  
 import java.util.ArrayList;
 33  
 import java.util.List;
 34  
 
 35  
 /**
 36  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 37  
  * @version $Id: PluginDescriptorBuilder.java 640545 2008-03-24 19:50:56Z bentmann $
 38  
  */
 39  0
 public class PluginDescriptorBuilder
 40  
 {
 41  
     public PluginDescriptor build( Reader reader )
 42  
         throws PlexusConfigurationException
 43  
     {
 44  0
         return build( reader, null );
 45  
     }
 46  
 
 47  
     public PluginDescriptor build( Reader reader, String source )
 48  
         throws PlexusConfigurationException
 49  
     {
 50  0
         PlexusConfiguration c = buildConfiguration( reader );
 51  
 
 52  0
         PluginDescriptor pluginDescriptor = new PluginDescriptor();
 53  
 
 54  0
         pluginDescriptor.setSource( source );
 55  0
         pluginDescriptor.setGroupId( c.getChild( "groupId" ).getValue() );
 56  0
         pluginDescriptor.setArtifactId( c.getChild( "artifactId" ).getValue() );
 57  0
         pluginDescriptor.setVersion( c.getChild( "version" ).getValue() );
 58  0
         pluginDescriptor.setGoalPrefix( c.getChild( "goalPrefix" ).getValue() );
 59  
         
 60  0
         pluginDescriptor.setName( c.getChild( "name" ).getValue() );
 61  0
         pluginDescriptor.setDescription( c.getChild( "description" ).getValue() );
 62  
 
 63  0
         String isolatedRealm = c.getChild( "isolatedRealm" ).getValue();
 64  
 
 65  0
         if ( isolatedRealm != null )
 66  
         {
 67  0
             pluginDescriptor.setIsolatedRealm( Boolean.valueOf( isolatedRealm ).booleanValue() );
 68  
         }
 69  
 
 70  0
         String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
 71  
 
 72  0
         if ( inheritedByDefault != null )
 73  
         {
 74  0
             pluginDescriptor.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
 75  
         }
 76  
 
 77  
         // ----------------------------------------------------------------------
 78  
         // Components
 79  
         // ----------------------------------------------------------------------
 80  
 
 81  0
         PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" );
 82  
 
 83  0
         for ( int i = 0; i < mojoConfigurations.length; i++ )
 84  
         {
 85  0
             PlexusConfiguration component = mojoConfigurations[i];
 86  
 
 87  0
             MojoDescriptor mojoDescriptor = buildComponentDescriptor( component, pluginDescriptor );
 88  
 
 89  0
             pluginDescriptor.addMojo( mojoDescriptor );
 90  
         }
 91  
 
 92  
         // ----------------------------------------------------------------------
 93  
         // Dependencies
 94  
         // ----------------------------------------------------------------------
 95  
 
 96  0
         PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" );
 97  
 
 98  0
         List dependencies = new ArrayList();
 99  
 
 100  0
         for ( int i = 0; i < dependencyConfigurations.length; i++ )
 101  
         {
 102  0
             PlexusConfiguration d = dependencyConfigurations[i];
 103  
 
 104  0
             ComponentDependency cd = new ComponentDependency();
 105  
 
 106  0
             cd.setArtifactId( d.getChild( "artifactId" ).getValue() );
 107  
 
 108  0
             cd.setGroupId( d.getChild( "groupId" ).getValue() );
 109  
 
 110  0
             cd.setType( d.getChild( "type" ).getValue() );
 111  
 
 112  0
             cd.setVersion( d.getChild( "version" ).getValue() );
 113  
 
 114  0
             dependencies.add( cd );
 115  
         }
 116  
 
 117  0
         pluginDescriptor.setDependencies( dependencies );
 118  
 
 119  0
         return pluginDescriptor;
 120  
     }
 121  
 
 122  
     public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
 123  
         throws PlexusConfigurationException
 124  
     {
 125  0
         MojoDescriptor mojo = new MojoDescriptor();
 126  0
         mojo.setPluginDescriptor( pluginDescriptor );
 127  
 
 128  0
         mojo.setGoal( c.getChild( "goal" ).getValue() );
 129  
         
 130  0
         mojo.setImplementation( c.getChild( "implementation" ).getValue() );
 131  
 
 132  0
         PlexusConfiguration langConfig = c.getChild( "language" );
 133  
 
 134  0
         if ( langConfig != null )
 135  
         {
 136  0
             mojo.setLanguage( langConfig.getValue() );
 137  
         }
 138  
 
 139  0
         PlexusConfiguration configuratorConfig = c.getChild( "configurator" );
 140  
 
 141  0
         if ( configuratorConfig != null )
 142  
         {
 143  0
             mojo.setComponentConfigurator( configuratorConfig.getValue() );
 144  
         }
 145  
 
 146  0
         PlexusConfiguration composerConfig = c.getChild( "composer" );
 147  
 
 148  0
         if ( composerConfig != null )
 149  
         {
 150  0
             mojo.setComponentComposer( composerConfig.getValue() );
 151  
         }
 152  
 
 153  0
         String since = c.getChild( "since" ).getValue();
 154  
 
 155  0
         if ( since != null )
 156  
         {
 157  0
             mojo.setSince( since );
 158  
         }
 159  
 
 160  0
         String phase = c.getChild( "phase" ).getValue();
 161  
 
 162  0
         if ( phase != null )
 163  
         {
 164  0
             mojo.setPhase( phase );
 165  
         }
 166  
 
 167  0
         String executePhase = c.getChild( "executePhase" ).getValue();
 168  
 
 169  0
         if ( executePhase != null )
 170  
         {
 171  0
             mojo.setExecutePhase( executePhase );
 172  
         }
 173  
 
 174  0
         String executeMojo = c.getChild( "executeGoal" ).getValue();
 175  
 
 176  0
         if ( executeMojo != null )
 177  
         {
 178  0
             mojo.setExecuteGoal( executeMojo );
 179  
         }
 180  
 
 181  0
         String executeLifecycle = c.getChild( "executeLifecycle" ).getValue();
 182  
 
 183  0
         if ( executeLifecycle != null )
 184  
         {
 185  0
             mojo.setExecuteLifecycle( executeLifecycle );
 186  
         }
 187  
 
 188  0
         mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() );
 189  
 
 190  0
         mojo.setDescription( c.getChild( "description" ).getValue() );
 191  
 
 192  0
         String dependencyResolution = c.getChild( "requiresDependencyResolution" ).getValue();
 193  
 
 194  0
         if ( dependencyResolution != null )
 195  
         {
 196  0
             mojo.setDependencyResolutionRequired( dependencyResolution );
 197  
         }
 198  
 
 199  0
         String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();
 200  
 
 201  0
         if ( directInvocationOnly != null )
 202  
         {
 203  0
             mojo.setDirectInvocationOnly( Boolean.valueOf( directInvocationOnly ).booleanValue() );
 204  
         }
 205  
 
 206  0
         String requiresProject = c.getChild( "requiresProject" ).getValue();
 207  
 
 208  0
         if ( requiresProject != null )
 209  
         {
 210  0
             mojo.setProjectRequired( Boolean.valueOf( requiresProject ).booleanValue() );
 211  
         }
 212  
 
 213  0
         String requiresReports = c.getChild( "requiresReports" ).getValue();
 214  
 
 215  0
         if ( requiresReports != null )
 216  
         {
 217  0
             mojo.setRequiresReports( Boolean.valueOf( requiresReports ).booleanValue() );
 218  
         }
 219  
 
 220  0
         String aggregator = c.getChild( "aggregator" ).getValue();
 221  
 
 222  0
         if ( aggregator != null )
 223  
         {
 224  0
             mojo.setAggregator( Boolean.valueOf( aggregator ).booleanValue() );
 225  
         }
 226  
 
 227  0
         String requiresOnline = c.getChild( "requiresOnline" ).getValue();
 228  
 
 229  0
         if ( requiresOnline != null )
 230  
         {
 231  0
             mojo.setOnlineRequired( Boolean.valueOf( requiresOnline ).booleanValue() );
 232  
         }
 233  
 
 234  0
         String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue();
 235  
 
 236  0
         if ( inheritedByDefault != null )
 237  
         {
 238  0
             mojo.setInheritedByDefault( Boolean.valueOf( inheritedByDefault ).booleanValue() );
 239  
         }
 240  
 
 241  
         // ----------------------------------------------------------------------
 242  
         // Parameters
 243  
         // ----------------------------------------------------------------------
 244  
 
 245  0
         PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
 246  
 
 247  0
         List parameters = new ArrayList();
 248  
 
 249  0
         for ( int i = 0; i < parameterConfigurations.length; i++ )
 250  
         {
 251  0
             PlexusConfiguration d = parameterConfigurations[i];
 252  
 
 253  0
             Parameter parameter = new Parameter();
 254  
 
 255  0
             parameter.setName( d.getChild( "name" ).getValue() );
 256  
 
 257  0
             parameter.setAlias( d.getChild( "alias" ).getValue() );
 258  
 
 259  0
             parameter.setType( d.getChild( "type" ).getValue() );
 260  
 
 261  0
             String required = d.getChild( "required" ).getValue();
 262  
 
 263  0
             parameter.setRequired( Boolean.valueOf( required ).booleanValue() );
 264  
 
 265  0
             PlexusConfiguration editableConfig = d.getChild( "editable" );
 266  
 
 267  
             // we need the null check for pre-build legacy plugins...
 268  0
             if ( editableConfig != null )
 269  
             {
 270  0
                 String editable = d.getChild( "editable" ).getValue();
 271  
 
 272  0
                 parameter.setEditable( editable == null || Boolean.valueOf( editable ).booleanValue() );
 273  
             }
 274  
 
 275  0
             parameter.setDescription( d.getChild( "description" ).getValue() );
 276  
 
 277  0
             parameter.setDeprecated( d.getChild( "deprecated" ).getValue() );
 278  
 
 279  0
             parameter.setImplementation( d.getChild( "implementation" ).getValue() );
 280  
 
 281  0
             parameters.add( parameter );
 282  
         }
 283  
 
 284  0
         mojo.setParameters( parameters );
 285  
 
 286  
         // TODO: this should not need to be handed off...
 287  
 
 288  
         // ----------------------------------------------------------------------
 289  
         // Configuration
 290  
         // ----------------------------------------------------------------------
 291  
 
 292  0
         mojo.setMojoConfiguration( c.getChild( "configuration" ) );
 293  
 
 294  
         // TODO: Go back to this when we get the container ready to configure mojos...
 295  
         //        mojo.setConfiguration( c.getChild( "configuration" ) );
 296  
 
 297  
         // ----------------------------------------------------------------------
 298  
         // Requirements
 299  
         // ----------------------------------------------------------------------
 300  
 
 301  0
         PlexusConfiguration[] requirements = c.getChild( "requirements" ).getChildren( "requirement" );
 302  
 
 303  0
         for ( int i = 0; i < requirements.length; i++ )
 304  
         {
 305  0
             PlexusConfiguration requirement = requirements[i];
 306  
 
 307  0
             ComponentRequirement cr = new ComponentRequirement();
 308  
 
 309  0
             cr.setRole( requirement.getChild( "role" ).getValue() );
 310  
 
 311  0
             cr.setRoleHint( requirement.getChild( "role-hint" ).getValue() );
 312  
 
 313  0
             cr.setFieldName( requirement.getChild( "field-name" ).getValue() );
 314  
 
 315  0
             mojo.addRequirement( cr );
 316  
         }
 317  
 
 318  0
         return mojo;
 319  
     }
 320  
 
 321  
     // ----------------------------------------------------------------------
 322  
     //
 323  
     // ----------------------------------------------------------------------
 324  
 
 325  
     public PlexusConfiguration buildConfiguration( Reader configuration )
 326  
         throws PlexusConfigurationException
 327  
     {
 328  
         try
 329  
         {
 330  0
             return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) );
 331  
         }
 332  0
         catch ( IOException e )
 333  
         {
 334  0
             throw new PlexusConfigurationException( "Error creating configuration", e );
 335  
         }
 336  0
         catch ( XmlPullParserException e )
 337  
         {
 338  0
             throw new PlexusConfigurationException( "Error creating configuration", e );
 339  
         }
 340  
     }
 341  
 }