Coverage Report - org.codehaus.plexus.component.configurator.converters.composite.ArrayConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayConverter
90 %
30/33
85 %
12/14
7,5
 
 1  
 package org.codehaus.plexus.component.configurator.converters.composite;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
 28  
 import org.codehaus.plexus.component.configurator.ConfigurationListener;
 29  
 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
 30  
 import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
 31  
 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
 32  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 33  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  
 import java.lang.reflect.Array;
 37  
 import java.util.ArrayList;
 38  
 import java.util.List;
 39  
 
 40  
 
 41  
 /**
 42  
  * @author <a href="mailto:kenney@codehaus.org">Kenney Westerhof</a>
 43  
  * @version $Id$
 44  
  */
 45  67
 public class ArrayConverter
 46  
     extends AbstractConfigurationConverter
 47  
 {
 48  
     public boolean canConvert( Class type )
 49  
     {
 50  78
         return type.isArray();
 51  
     }
 52  
 
 53  
     public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type,
 54  
                                      Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator,
 55  
                                      ConfigurationListener listener )
 56  
         throws ComponentConfigurationException
 57  
     {
 58  16
         Object retValue = fromExpression( configuration, expressionEvaluator, type );
 59  16
         if ( retValue != null )
 60  
         {
 61  0
             return retValue;
 62  
         }
 63  
 
 64  16
         List values = new ArrayList();
 65  
 
 66  48
         for ( int i = 0; i < configuration.getChildCount(); i++ )
 67  
         {
 68  32
             PlexusConfiguration childConfiguration = configuration.getChild( i );
 69  
 
 70  32
             String configEntry = childConfiguration.getName();
 71  
 
 72  32
             String name = fromXML( configEntry );
 73  
             
 74  32
             Class childType = getClassForImplementationHint( null, childConfiguration, classLoader );
 75  
             
 76  
             // check if the name is a fully qualified classname
 77  
 
 78  32
             if ( childType == null && name.indexOf( '.' ) > 0 )
 79  
             {
 80  
                 try
 81  
                 {
 82  4
                     childType = classLoader.loadClass( name );
 83  
                 }
 84  0
                 catch ( ClassNotFoundException e )
 85  
                 {
 86  
                     // doesn't exist - continue processing
 87  4
                 }
 88  
             }
 89  
 
 90  32
             if ( childType == null )
 91  
             {
 92  
                 // try to find the class in the package of the baseType
 93  
                 // (which is the component being configured)
 94  
 
 95  26
                 String baseTypeName = baseType.getName();
 96  
 
 97  26
                 int lastDot = baseTypeName.lastIndexOf( '.' );
 98  
 
 99  
                 String className;
 100  
 
 101  26
                 if ( lastDot == -1 )
 102  
                 {
 103  0
                     className = name;
 104  
                 }
 105  
                 else
 106  
                 {
 107  26
                     String basePackage = baseTypeName.substring( 0, lastDot );
 108  26
                     className = basePackage + "." + StringUtils.capitalizeFirstLetter( name );
 109  
                 }
 110  
                 
 111  
                 try
 112  
                 {
 113  26
                     childType = classLoader.loadClass( className );
 114  
                 }
 115  20
                 catch ( ClassNotFoundException e )
 116  
                 {
 117  
                     // doesn't exist, continue processing
 118  6
                 }                               
 119  
             }
 120  
 
 121  
             // finally just try the component type of the array
 122  
 
 123  32
             if ( childType == null )
 124  
             {
 125  20
                 childType = type.getComponentType();
 126  
             }
 127  
 
 128  32
             ConfigurationConverter converter = converterLookup.lookupConverterForType( childType );
 129  
             
 130  32
             Object object = converter.fromConfiguration( converterLookup, 
 131  
                                                          childConfiguration, 
 132  
                                                          childType, 
 133  
                                                          baseType, 
 134  
                                                          classLoader,
 135  
                                                          expressionEvaluator, 
 136  
                                                          listener );
 137  
 
 138  32
             values.add( object );
 139  
         }
 140  
 
 141  
         try
 142  
         {
 143  16
             return values.toArray( (Object[]) Array.newInstance( type.getComponentType(), 0 ) );
 144  
         }
 145  2
         catch ( ArrayStoreException e )
 146  
         {
 147  2
             throw new ComponentConfigurationException( "Cannot assign configuration values to array of type "
 148  
                 + type.getComponentType().getName() + ": " + values );
 149  
         }
 150  
     }
 151  
 
 152  
 }