Coverage Report - org.codehaus.plexus.component.configurator.converters.basic.AbstractBasicConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractBasicConverter
54 %
18/33
55 %
11/20
6,667
 
 1  
 package org.codehaus.plexus.component.configurator.converters.basic;
 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.lookup.ConverterLookup;
 31  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 32  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
 33  
 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
 34  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 35  
 
 36  
 /**
 37  
  * @version $Id$
 38  
  */
 39  938
 public abstract class AbstractBasicConverter
 40  
     extends AbstractConfigurationConverter
 41  
 {
 42  
     protected abstract Object fromString( String str )
 43  
         throws ComponentConfigurationException;
 44  
 
 45  
     protected Object fromExpression( PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, Class type )
 46  
         throws ComponentConfigurationException
 47  
     {
 48  79
         Object v = null;
 49  
 
 50  79
         String value = configuration.getValue( null );
 51  
 
 52  79
         if ( value != null && value.length() > 0 )
 53  
         {
 54  
             // Object is provided by an expression
 55  
             // This seems a bit ugly... canConvert really should return false in this instance, but it doesn't have the
 56  
             //   configuration to know better
 57  
             try
 58  
             {
 59  77
                 if ( expressionEvaluator instanceof TypeAwareExpressionEvaluator )
 60  
                 {
 61  2
                     v = ( (TypeAwareExpressionEvaluator) expressionEvaluator ).evaluate( value, type );
 62  
                 }
 63  
                 else
 64  
                 {
 65  75
                     v = expressionEvaluator.evaluate( value );
 66  
                 }
 67  
             }
 68  0
             catch ( ExpressionEvaluationException e )
 69  
             {
 70  0
                 String msg = "Error evaluating the expression '" + value + "' for configuration value '" +
 71  
                     configuration.getName() + "'";
 72  0
                 throw new ComponentConfigurationException( configuration, msg, e );
 73  77
             }
 74  
         }
 75  
 
 76  79
         if ( v == null )
 77  
         {
 78  2
             value = configuration.getAttribute( "default-value", null );
 79  
 
 80  2
             if ( value != null && value.length() > 0 )
 81  
             {
 82  
                 try
 83  
                 {
 84  0
                     if ( expressionEvaluator instanceof TypeAwareExpressionEvaluator )
 85  
                     {
 86  0
                         v = ( (TypeAwareExpressionEvaluator) expressionEvaluator ).evaluate( value, type );
 87  
                     }
 88  
                     else
 89  
                     {
 90  0
                         v = expressionEvaluator.evaluate( value );
 91  
                     }
 92  
                 }
 93  0
                 catch ( ExpressionEvaluationException e )
 94  
                 {
 95  0
                     String msg = "Error evaluating the expression '" + value + "' for configuration value '" +
 96  
                         configuration.getName() + "'";
 97  0
                     throw new ComponentConfigurationException( configuration, msg, e );
 98  0
                 }
 99  
             }
 100  
         }
 101  
 
 102  
         /*
 103  
          * NOTE: We don't check the type here which would be ugly to do correctly (e.g. value=Short -> type=int), the
 104  
          * reflective setter/field injection will fail by itself when the type didn't match.
 105  
          */
 106  
 
 107  79
         return v;
 108  
     }
 109  
 
 110  
     public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type,
 111  
                                      Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator,
 112  
                                      ConfigurationListener listener )
 113  
         throws ComponentConfigurationException
 114  
     {
 115  79
         if ( configuration.getChildCount() > 0 )
 116  
         {
 117  0
             throw new ComponentConfigurationException( "When configuring a basic element the configuration cannot " +
 118  
                 "contain any child elements. " + "Configuration element '" + configuration.getName() + "'." );
 119  
         }
 120  
 
 121  79
         Object retValue = fromExpression( configuration, expressionEvaluator, type );
 122  
 
 123  79
         if ( retValue instanceof String )
 124  
         {
 125  
             try
 126  
             {
 127  76
                 retValue = fromString( (String) retValue );
 128  
             }
 129  0
             catch ( ComponentConfigurationException e )
 130  
             {
 131  0
                 if ( e.getFailedConfiguration() == null )
 132  
                 {
 133  0
                     e.setFailedConfiguration( configuration );
 134  
                 }
 135  
 
 136  0
                 throw e;
 137  76
             }
 138  
         }
 139  
 
 140  79
         return retValue;
 141  
     }
 142  
 }