Coverage Report - org.apache.maven.model.converter.plugins.AbstractPluginConfigurationConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPluginConfigurationConverter
16%
10/64
7%
2/30
2,25
 
 1  
 package org.apache.maven.model.converter.plugins;
 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.apache.maven.model.Build;
 23  
 import org.apache.maven.model.Model;
 24  
 import org.apache.maven.model.Plugin;
 25  
 import org.apache.maven.model.ReportPlugin;
 26  
 import org.apache.maven.model.Reporting;
 27  
 import org.apache.maven.model.converter.ConverterListener;
 28  
 import org.apache.maven.model.converter.ModelUtils;
 29  
 import org.apache.maven.model.converter.ProjectConverterException;
 30  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 31  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 32  
 
 33  
 import java.util.ArrayList;
 34  
 import java.util.Iterator;
 35  
 import java.util.List;
 36  
 import java.util.Properties;
 37  
 
 38  
 /**
 39  
  * @author Fabrizio Giustina
 40  
  * @author Dennis Lundberg
 41  
  * @version $Id: AbstractPluginConfigurationConverter.java 661727 2008-05-30 14:21:49Z bentmann $
 42  
  */
 43  26
 public abstract class AbstractPluginConfigurationConverter
 44  
     extends AbstractLogEnabled
 45  
     implements PluginConfigurationConverter
 46  
 {
 47  
     public static final String TYPE_BUILD_PLUGIN = "build plugin";
 48  
 
 49  
     public static final String TYPE_REPORT_PLUGIN = "report plugin";
 50  
 
 51  26
     private List listeners = new ArrayList();
 52  
 
 53  
     public abstract String getArtifactId();
 54  
 
 55  
     public String getGroupId()
 56  
     {
 57  0
         return "org.apache.maven.plugins";
 58  
     }
 59  
 
 60  
     public abstract String getType();
 61  
 
 62  
     public void addListeners( List listeners )
 63  
     {
 64  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 65  
         {
 66  0
             ConverterListener listener = (ConverterListener) i.next();
 67  0
             addListener( listener );
 68  
         }
 69  0
     }
 70  
 
 71  
     public void addListener( ConverterListener listener )
 72  
     {
 73  0
         if ( !listeners.contains( listener ) )
 74  
         {
 75  0
             listeners.add( listener );
 76  
         }
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Add a child element to the configuration.
 81  
      *
 82  
      * @param configuration     The configuration to add the element to
 83  
      * @param projectProperties The M1 properties
 84  
      * @param mavenOneProperty  The name of the Maven 1 property to convert
 85  
      * @param mavenTwoElement   The name of the Maven 2 configuration element
 86  
      */
 87  
     protected void addConfigurationChild( Xpp3Dom configuration, Properties projectProperties, String mavenOneProperty,
 88  
                                           String mavenTwoElement )
 89  
     {
 90  206
         String value = projectProperties.getProperty( mavenOneProperty );
 91  206
         addConfigurationChild( configuration, mavenTwoElement, value );
 92  206
     }
 93  
 
 94  
     /**
 95  
      * Add a child element to the configuration.
 96  
      *
 97  
      * @param configuration   The configuration to add the element to
 98  
      * @param mavenTwoElement The name of the Maven 2 configuration element
 99  
      * @param value           Set the value of the element to this
 100  
      */
 101  
     protected void addConfigurationChild( Xpp3Dom configuration, String mavenTwoElement, String value )
 102  
     {
 103  248
         if ( value != null )
 104  
         {
 105  163
             Xpp3Dom child = new Xpp3Dom( mavenTwoElement );
 106  163
             child.setValue( value );
 107  163
             configuration.addChild( child );
 108  
         }
 109  248
     }
 110  
 
 111  
     public void convertConfiguration( Model v4Model, org.apache.maven.model.v3_0_0.Model v3Model,
 112  
                                       Properties projectProperties )
 113  
         throws ProjectConverterException
 114  
     {
 115  0
         boolean addPlugin = false;
 116  
 
 117  0
         Xpp3Dom configuration = new Xpp3Dom( "configuration" );
 118  
 
 119  0
         buildConfiguration( configuration, v3Model, projectProperties );
 120  
 
 121  0
         if ( configuration.getChildCount() > 0 )
 122  
         {
 123  0
             if ( TYPE_BUILD_PLUGIN.equals( getType() ) )
 124  
             {
 125  0
                 Plugin plugin = ModelUtils.findBuildPlugin( v4Model, getGroupId(), getArtifactId() );
 126  0
                 if ( plugin == null )
 127  
                 {
 128  0
                     addPlugin = true;
 129  0
                     plugin = new Plugin();
 130  0
                     plugin.setGroupId( getGroupId() );
 131  0
                     plugin.setArtifactId( getArtifactId() );
 132  
                 }
 133  
 
 134  0
                 plugin.setConfiguration( configuration );
 135  
 
 136  0
                 if ( addPlugin )
 137  
                 {
 138  0
                     if ( v4Model.getBuild() == null )
 139  
                     {
 140  0
                         v4Model.setBuild( new Build() );
 141  
                     }
 142  0
                     v4Model.getBuild().addPlugin( plugin );
 143  0
                     sendInfoMessage( "Adding plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
 144  0
                     fireAddPluginEvent( plugin );
 145  
                 }
 146  
             }
 147  0
             else if ( TYPE_REPORT_PLUGIN.equals( getType() ) )
 148  
             {
 149  0
                 ReportPlugin plugin = ModelUtils.findReportPlugin( v4Model, getGroupId(), getArtifactId() );
 150  0
                 if ( plugin == null )
 151  
                 {
 152  0
                     addPlugin = true;
 153  0
                     plugin = new ReportPlugin();
 154  0
                     plugin.setGroupId( getGroupId() );
 155  0
                     plugin.setArtifactId( getArtifactId() );
 156  
                 }
 157  
 
 158  0
                 plugin.setConfiguration( configuration );
 159  
 
 160  0
                 if ( addPlugin )
 161  
                 {
 162  0
                     if ( v4Model.getReporting() == null )
 163  
                     {
 164  0
                         v4Model.setReporting( new Reporting() );
 165  
                     }
 166  0
                     v4Model.getReporting().addPlugin( plugin );
 167  0
                     sendInfoMessage( "Adding report " + plugin.getGroupId() + ":" + plugin.getArtifactId() );
 168  0
                     fireAddReportEvent( plugin );
 169  
                 }
 170  
             }
 171  
         }
 172  0
     }
 173  
 
 174  
     protected abstract void buildConfiguration( Xpp3Dom configuration, org.apache.maven.model.v3_0_0.Model v3Model,
 175  
                                                 Properties projectProperties )
 176  
         throws ProjectConverterException;
 177  
 
 178  
     private void sendInfoMessage( String message )
 179  
     {
 180  0
         getLogger().info( message );
 181  
 
 182  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 183  
         {
 184  0
             ConverterListener listener = (ConverterListener) i.next();
 185  0
             listener.info( message );
 186  
         }
 187  0
     }
 188  
 
 189  
     private void fireAddPluginEvent( Plugin plugin )
 190  
     {
 191  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 192  
         {
 193  0
             ConverterListener listener = (ConverterListener) i.next();
 194  0
             listener.addPluginEvent( plugin.getGroupId(), plugin.getArtifactId() );
 195  
         }
 196  0
     }
 197  
 
 198  
     private void fireAddReportEvent( ReportPlugin plugin )
 199  
     {
 200  0
         for ( Iterator i = listeners.iterator(); i.hasNext(); )
 201  
         {
 202  0
             ConverterListener listener = (ConverterListener) i.next();
 203  0
             listener.addReportEvent( plugin.getGroupId(), plugin.getArtifactId() );
 204  
         }
 205  0
     }
 206  
 }