Coverage Report - org.mule.expression.MapPayloadExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MapPayloadExpressionEvaluator
0%
0/27
0%
0/16
0
 
 1  
 /*
 2  
  * $Id: MapPayloadExpressionEvaluator.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.expression;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.expression.ExpressionEvaluator;
 15  
 import org.mule.api.expression.RequiredValueException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 /**
 23  
  * If the message payload is a map this extractor will look up the property value in
 24  
  * the map
 25  
  */
 26  0
 public class MapPayloadExpressionEvaluator implements ExpressionEvaluator, ExpressionConstants
 27  
 {
 28  
     public static final String NAME = "map-payload";
 29  
 
 30  
     public Object evaluate(String expression, MuleMessage message)
 31  
     {
 32  0
         Object payload = message.getPayload();
 33  
 
 34  0
         if (!(payload instanceof Map))
 35  
         {
 36  0
             return null;
 37  
         }
 38  
         
 39  0
         if (expression.indexOf(DELIM) > -1)
 40  
         {
 41  
 
 42  0
             String[] strings = StringUtils.splitAndTrim(expression, DELIM);
 43  0
             Map<String, Object> result = new HashMap<String, Object>(strings.length);
 44  
 
 45  0
             for (String s : strings)
 46  
             {
 47  0
                 Object val = getValue(s, (Map) payload);
 48  0
                 if (val != null)
 49  
                 {
 50  0
                     if (s.endsWith(OPTIONAL_ARGUMENT))
 51  
                     {
 52  0
                         s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
 53  
                     }
 54  0
                     result.put(s, val);
 55  
                 }
 56  
             }
 57  0
             return result;
 58  
         }
 59  
         else
 60  
         {
 61  0
             return getValue(expression, (Map)payload);
 62  
         }
 63  
     }
 64  
 
 65  
     protected Object getValue(String key, Map map)
 66  
     {
 67  
         boolean required;
 68  0
         if (key.endsWith(OPTIONAL_ARGUMENT))
 69  
         {
 70  0
             key = key.substring(0, key.length() - OPTIONAL_ARGUMENT.length());
 71  0
             required = false;
 72  
         }
 73  
         else
 74  
         {
 75  0
             required = true;
 76  
         }
 77  0
         Object val = map.get(key);
 78  0
         if (val != null)
 79  
         {
 80  0
             return val;
 81  
         }
 82  0
         else if (required)
 83  
         {
 84  0
             throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, key));
 85  
         }
 86  0
         return null;
 87  
     }
 88  
 
 89  
     /**
 90  
      * {@inheritDoc}
 91  
      */
 92  
     public String getName()
 93  
     {
 94  0
         return NAME;
 95  
     }
 96  
 
 97  
     /**
 98  
      * {@inheritDoc}
 99  
      */
 100  
     public void setName(String name)
 101  
     {
 102  0
         throw new UnsupportedOperationException();
 103  
     }
 104  
 
 105  
 }