Coverage Report - org.apache.maven.plugin.antrun.components.AntTargetConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
AntTargetConverter
84%
63/75
69%
22/32
4.5
 
 1  
 package org.apache.maven.plugin.antrun.components;
 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.tools.ant.ComponentHelper;
 23  
 import org.apache.tools.ant.Project;
 24  
 import org.apache.tools.ant.ProjectHelper;
 25  
 import org.apache.tools.ant.RuntimeConfigurable;
 26  
 import org.apache.tools.ant.Target;
 27  
 import org.apache.tools.ant.UnknownElement;
 28  
 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
 29  
 import org.codehaus.plexus.component.configurator.ConfigurationListener;
 30  
 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
 31  
 import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
 32  
 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
 33  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 34  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 35  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 
 38  
 /**
 39  
  * Plexus ConfigurationConverter to set up Ant Target component fields.
 40  
  *
 41  
  * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
 42  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 43  
  * @version $Id$
 44  
  */
 45  3
 public class AntTargetConverter
 46  
     extends AbstractConfigurationConverter
 47  
 {
 48  
     public static final String MAVEN_EXPRESSION_EVALUATOR_ID = "maven.expressionEvaluator";
 49  
 
 50  3
     public static final String ROLE = ConfigurationConverter.class.getName();
 51  
 
 52  
     /**
 53  
      * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
 54  
      */
 55  
     public boolean canConvert( Class type )
 56  
     {
 57  12
         return Target.class.isAssignableFrom( type );
 58  
     }
 59  
 
 60  
     /**
 61  
      * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#fromConfiguration(org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup, org.codehaus.plexus.configuration.PlexusConfiguration, java.lang.Class, java.lang.Class, java.lang.ClassLoader, org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator, org.codehaus.plexus.component.configurator.ConfigurationListener)
 62  
      */
 63  
     public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type,
 64  
                                     Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator,
 65  
                                     ConfigurationListener listener )
 66  
         throws ComponentConfigurationException
 67  
     {
 68  3
         Object retValue = fromExpression( configuration, expressionEvaluator, type );
 69  3
         if ( retValue != null )
 70  
         {
 71  0
             return retValue;
 72  
         }
 73  
 
 74  3
         Class implementation = getClassForImplementationHint( type, configuration, classLoader );
 75  
 
 76  3
         retValue = instantiateObject( implementation );
 77  
 
 78  3
         if ( !( retValue instanceof Target ) )
 79  
         {
 80  3
             retValue = new Target();
 81  
         }
 82  
 
 83  3
         processConfiguration( (Target) retValue, configuration, expressionEvaluator );
 84  
 
 85  3
         return retValue;
 86  
     }
 87  
 
 88  
     private void processConfiguration( Target target, PlexusConfiguration configuration,
 89  
                                       ExpressionEvaluator expressionEvaluator )
 90  
         throws ComponentConfigurationException
 91  
     {
 92  3
         Project project = new Project();
 93  3
         project.setName( "DummyProject" );
 94  
 
 95  3
         target.setName( "" );
 96  3
         target.setProject( project );
 97  3
         String[] attributeNames = configuration.getAttributeNames();
 98  8
         for ( int i = 0; i < attributeNames.length; i++ )
 99  
         {
 100  5
             String attributeName = attributeNames[i];
 101  5
             String attributeValue = configuration.getAttribute( attributeNames[i], null );
 102  
 
 103  5
             addAttributes( target, attributeName, attributeValue );
 104  
         }
 105  3
         project.addTarget( target );
 106  
 
 107  3
         project.addReference( MAVEN_EXPRESSION_EVALUATOR_ID, expressionEvaluator );
 108  
 
 109  3
         initDefinitions( project, target );
 110  
 
 111  3
         processConfiguration( null, project, target, configuration );
 112  
 
 113  3
         project.init();
 114  3
     }
 115  
 
 116  
     private void processConfiguration( RuntimeConfigurable parentWrapper, Project project, Target target,
 117  
                                       PlexusConfiguration configuration )
 118  
         throws ComponentConfigurationException
 119  
     {
 120  6
         int items = configuration.getChildCount();
 121  
 
 122  6
         Object parent = parentWrapper == null ? null : parentWrapper.getProxy();
 123  
 
 124  9
         for ( int i = 0; i < items; i++ )
 125  
         {
 126  3
             PlexusConfiguration childConfiguration = configuration.getChild( i );
 127  3
             UnknownElement task = new UnknownElement( childConfiguration.getName() );
 128  3
             task.setProject( project );
 129  3
             task.setNamespace( "" );
 130  3
             task.setQName( childConfiguration.getName() );
 131  3
             task.setTaskType( ProjectHelper.genComponentName( task.getNamespace(), childConfiguration.getName() ) );
 132  3
             task.setTaskName( childConfiguration.getName() );
 133  3
             task.setOwningTarget( target );
 134  3
             task.init();
 135  
 
 136  3
             if ( parent != null )
 137  
             {
 138  0
                 ( (UnknownElement) parent ).addChild( task );
 139  
             }
 140  
             else
 141  
             {
 142  3
                 target.addTask( task );
 143  
             }
 144  
 
 145  3
             RuntimeConfigurable wrapper = new RuntimeConfigurable( task, task.getTaskName() );
 146  
 
 147  
             try
 148  
             {
 149  3
                 if ( childConfiguration.getValue() != null )
 150  
                 {
 151  3
                     wrapper.addText( childConfiguration.getValue() );
 152  
                 }
 153  
             }
 154  0
             catch ( PlexusConfigurationException e )
 155  
             {
 156  0
                 throw new ComponentConfigurationException( "Error reading text value from element '"
 157  
                     + childConfiguration.getName() + "'", e );
 158  3
             }
 159  
 
 160  3
             String[] attrNames = childConfiguration.getAttributeNames();
 161  
 
 162  4
             for ( int a = 0; a < attrNames.length; a++ )
 163  
             {
 164  
                 try
 165  
                 {
 166  1
                     String v = childConfiguration.getAttribute( attrNames[a] );
 167  1
                     wrapper.setAttribute( attrNames[a], v );
 168  
                 }
 169  0
                 catch ( PlexusConfigurationException e )
 170  
                 {
 171  0
                     throw new ComponentConfigurationException( "Error getting attribute '" + attrNames[a]
 172  
                         + "' of tag '" + childConfiguration.getName() + "'", e );
 173  1
                 }
 174  
             }
 175  
 
 176  3
             if ( parentWrapper != null )
 177  
             {
 178  0
                 parentWrapper.addChild( wrapper );
 179  
             }
 180  
 
 181  3
             processConfiguration( wrapper, project, target, childConfiguration );
 182  
         }
 183  6
     }
 184  
 
 185  
     protected void initDefinitions( Project project, Target unused )
 186  
     {
 187  3
         ComponentHelper componentHelper = ComponentHelper.getComponentHelper( project );
 188  
 
 189  3
         componentHelper.initDefaultDefinitions();
 190  3
     }
 191  
 
 192  
     /**
 193  
      * Add specific <code>attributeValue</code> to the tasks for given <code>attributeName</code> like
 194  
      * <code>"if"</code>, <code>"unless"</code>, <code>"name"</code> and <code>"description"</code>.
 195  
      * <br/>
 196  
      * <b>Note:</b> <code>"depends"</code> from Ant tasks is not be used.
 197  
      *
 198  
      * @see <a href="http://ant.apache.org/manual/using.html#targets">Ant targets</a>
 199  
      *
 200  
      * @param tasks should be not null
 201  
      * @param attributeName if empty, skipped
 202  
      * @param attributeValue if empty, skipped
 203  
      */
 204  
     private static void addAttributes( Target tasks, String attributeName, String attributeValue )
 205  
     {
 206  5
         if ( StringUtils.isEmpty( attributeName ) )
 207  
         {
 208  0
             return;
 209  
         }
 210  5
         if ( StringUtils.isEmpty( attributeValue ) )
 211  
         {
 212  0
             return;
 213  
         }
 214  
 
 215  5
         if ( attributeName.toLowerCase().equals( "name" ) )
 216  
         {
 217  0
             tasks.setName( attributeValue );
 218  
         }
 219  5
         if ( attributeName.toLowerCase().equals( "unless" ) )
 220  
         {
 221  2
             tasks.setUnless( attributeValue );
 222  
         }
 223  5
         if ( attributeName.toLowerCase().equals( "description" ) )
 224  
         {
 225  0
             tasks.setDescription( attributeValue );
 226  
         }
 227  5
         if ( attributeName.toLowerCase().equals( "if" ) )
 228  
         {
 229  0
             tasks.setIf( attributeValue );
 230  
         }
 231  5
     }
 232  
 }