Coverage Report - org.mule.expression.transformers.ExpressionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionTransformer
0%
0/28
0%
0/18
0
 
 1  
 /*
 2  
  * $Id: ExpressionTransformer.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  
 package org.mule.expression.transformers;
 11  
 
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.expression.ExpressionRuntimeException;
 14  
 import org.mule.api.expression.RequiredValueException;
 15  
 import org.mule.api.transformer.TransformerException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 
 18  
 import java.util.Iterator;
 19  
 
 20  
 /**
 21  
  * This transformer will evaluate one or more expressions on the current message and return the
 22  
  * results as an Array. If only one expression is defined it will return the object returned from
 23  
  * the expression.
 24  
  * <p/>
 25  
  * You can use expressions to extract
 26  
  * <ul>
 27  
  * <li>headers (single, map or list)</li>
 28  
  * <li>attachments (single, map or list)</li>
 29  
  * <li>payload</li>
 30  
  * <li>xpath</li>
 31  
  * <li>groovy</li>
 32  
  * <li>bean</li>
 33  
  * </ul>
 34  
  * and more.
 35  
  * <p/>
 36  
  * This transformer provides a very powerful way to pull different bits of information from the
 37  
  * message and pass them to the service.
 38  
  */
 39  0
 public class ExpressionTransformer extends AbstractExpressionTransformer
 40  
 {
 41  0
     private boolean returnSourceIfNull = false;
 42  
 
 43  
     @Override
 44  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 45  
     {
 46  0
         Object results[] = new Object[arguments.size()];
 47  0
         int i = 0;
 48  0
         for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext(); i++)
 49  
         {
 50  0
             ExpressionArgument argument = iterator.next();
 51  
             try
 52  
             {
 53  0
                 results[i] = argument.evaluate(message);
 54  
             }
 55  0
             catch (RequiredValueException e)
 56  
             {
 57  0
                 if(!argument.isOptional())
 58  
                 {
 59  0
                     throw e;
 60  
                 }
 61  0
                 logger.warn(e.getMessage());
 62  
             }
 63  0
             catch (ExpressionRuntimeException e)
 64  
             {
 65  0
                 throw new TransformerException(this, e);
 66  0
             }
 67  
 
 68  0
             if (!argument.isOptional() && results[i] == null)
 69  
             {
 70  0
                 throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
 71  
                         argument.getExpressionConfig().getEvaluator(), argument.getExpressionConfig().getExpression()), this);
 72  
 
 73  
             }
 74  
 
 75  
         }
 76  0
         if (isReturnSourceIfNull() && checkIfAllAreNull(results))
 77  
         {
 78  0
             return message;
 79  
         }
 80  
 
 81  0
         if (results.length == 1)
 82  
         {
 83  0
             return results[0];
 84  
         }
 85  
         else
 86  
         {
 87  0
             return results;
 88  
         }
 89  
     }
 90  
 
 91  
     private boolean checkIfAllAreNull(Object[] objects)
 92  
     {
 93  0
         for (int i = 0; i < objects.length; i++)
 94  
         {
 95  0
             if (objects[i] != null)
 96  
             {
 97  0
                 return false;
 98  
             }
 99  
         }
 100  0
         return true;
 101  
     }
 102  
 
 103  
     public boolean isReturnSourceIfNull()
 104  
     {
 105  0
         return returnSourceIfNull;
 106  
     }
 107  
 
 108  
     public void setReturnSourceIfNull(boolean returnSourceIfNull)
 109  
     {
 110  0
         this.returnSourceIfNull = returnSourceIfNull;
 111  0
     }
 112  
 }