001package org.apache.maven.plugins.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 org.apache.maven.plugin.descriptor.MojoDescriptor;
023import org.apache.maven.plugin.descriptor.Parameter;
024import org.apache.maven.plugin.descriptor.PluginDescriptor;
025import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
026import org.apache.maven.rtinfo.RuntimeInformation;
027import org.apache.maven.tools.plugin.EnhancedParameterWrapper;
028import org.codehaus.plexus.configuration.PlexusConfiguration;
029import org.codehaus.plexus.configuration.PlexusConfigurationException;
030import org.apache.maven.plugin.plugin.report.PluginReport;
031
032import java.net.URI;
033
034/**
035 * Reads enhanced plugin.xml files as generated by
036 * {@link org.apache.maven.tools.plugin.generator.PluginDescriptorFilesGenerator} and
037 * used by {@link PluginReport}.
038 * Populates the slightly extended {@link Parameter} object {@link EnhancedParameterWrapper}.
039 */
040public class EnhancedPluginDescriptorBuilder extends PluginDescriptorBuilder
041{
042    private final boolean requireAddingMissingParameterSinceField;
043    
044    public EnhancedPluginDescriptorBuilder( RuntimeInformation rtInfo )
045    {
046        this( rtInfo.isMavenVersion( "[,3.3.9]" ) );
047    }
048
049    EnhancedPluginDescriptorBuilder( boolean requireAddingMissingParameterSinceField )
050    {
051        this.requireAddingMissingParameterSinceField = requireAddingMissingParameterSinceField;
052    }
053
054    @Override
055    public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor )
056        throws PlexusConfigurationException
057    {
058        MojoDescriptor mojoDescriptor = super.buildComponentDescriptor( c, pluginDescriptor );
059        
060        // ----------------------------------------------------------------------
061        // Parameters
062        // ----------------------------------------------------------------------
063
064        PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" );
065
066        for ( PlexusConfiguration d : parameterConfigurations )
067        {
068            String parameterName = d.getChild( "name" ).getValue();
069            // don't call getParameterMap() to not populate 
070            Parameter pd = mojoDescriptor.getParameterMap().get( parameterName );
071            if ( requireAddingMissingParameterSinceField )
072            {
073                addMissingParameterSinceField( pd, d );
074            }
075            PlexusConfiguration configTypeJavadocUrl = d.getChild( "typeJavadocUrl", false );
076            if ( configTypeJavadocUrl != null )
077            {
078                String parameterTypeJavadocUrl = configTypeJavadocUrl.getValue();
079                EnhancedParameterWrapper enhancedParameter = new EnhancedParameterWrapper( pd );
080                enhancedParameter.setTypeJavadocUrl( URI.create( parameterTypeJavadocUrl ) );
081                mojoDescriptor.getParameters().set( mojoDescriptor.getParameters().indexOf( pd ), enhancedParameter );
082                mojoDescriptor.getParameterMap().put( parameterName, enhancedParameter );
083            }
084        }
085        return mojoDescriptor;
086    }
087
088    /**
089     * Reads the plugin descriptor and adds the fix for <a href="https://issues.apache.org/jira/browse/MNG-6109">
090     * MNG-6109</a> when using Maven-3.3.9 and before.
091     * Method can be removed once Maven 3.5.0 is the prerequisite for this plugin.
092     * @throws PlexusConfigurationException 
093     * 
094     * @since 3.5.1
095     * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a>
096     * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a>
097     */
098     void addMissingParameterSinceField( Parameter pd, PlexusConfiguration d ) throws PlexusConfigurationException
099     {
100         String parameterSince = d.getChild( "since" ).getValue();
101         pd.setSince( parameterSince );
102     }
103}