Coverage Report - org.mule.transformer.simple.ExpressionTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionTransformer
0%
0/44
0%
0/22
2.333
ExpressionTransformer$Argument
0%
0/28
0%
0/20
2.333
 
 1  
 /*
 2  
  * $Id: ExpressionTransformer.java 11834 2008-05-22 12:06:47Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transformer.simple;
 11  
 
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.transformer.AbstractMessageAwareTransformer;
 17  
 import org.mule.util.expression.ExpressionEvaluatorManager;
 18  
 import org.mule.util.expression.ExpressionRuntimeException;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 /**
 25  
  * This transformer will evaluate one or more expressions on the current message and return the
 26  
  * results as an Array. If only one expression is defined it will return the object returned from
 27  
  * the expression.
 28  
  * <p/>
 29  
  * You can use expressions to extract
 30  
  * <ul>
 31  
  * <li>headers (single, map or list)</li>
 32  
  * <li>attachments (single, map or list)</li>
 33  
  * <li>payload</li>
 34  
  * <li>xpath</li>
 35  
  * <li>groovy</li>
 36  
  * <li>bean</li>
 37  
  * </ul>
 38  
  * and more.
 39  
  * <p/>
 40  
  * This transformer provides a very powerful way to pull different bits of information from the
 41  
  * message and pass them to the service.
 42  
  */
 43  
 public class ExpressionTransformer extends AbstractMessageAwareTransformer
 44  
 {
 45  
     private List arguments;
 46  0
     private boolean returnSourceIfNull = false;
 47  
 
 48  
     public ExpressionTransformer()
 49  0
     {
 50  
         //No type checking by default
 51  0
         registerSourceType(Object.class);
 52  0
         setReturnClass(Object.class);
 53  0
         arguments = new ArrayList(4);
 54  0
     }
 55  
 
 56  
     public void addArgument(Argument argument)
 57  
     {
 58  0
         arguments.add(argument);
 59  0
     }
 60  
 
 61  
     public boolean removeArgument(Argument argument)
 62  
     {
 63  0
         return arguments.remove(argument);
 64  
     }
 65  
 
 66  
     /**
 67  
      * Template method were deriving classes can do any initialisation after the
 68  
      * properties have been set on this transformer
 69  
      *
 70  
      * @throws org.mule.api.lifecycle.InitialisationException
 71  
      *
 72  
      */
 73  
     //@Override
 74  
     public void initialise() throws InitialisationException
 75  
     {
 76  0
         if (arguments == null || arguments.size() == 0)
 77  
         {
 78  0
             throw new InitialisationException(CoreMessages.objectIsNull("arguments[]"), this);
 79  
         }
 80  
 
 81  0
         for (Iterator iterator = arguments.iterator(); iterator.hasNext();)
 82  
         {
 83  0
             Argument argument = (Argument) iterator.next();
 84  
             try
 85  
             {
 86  0
                 argument.validate();
 87  
             }
 88  0
             catch (Exception e)
 89  
             {
 90  0
                 throw new InitialisationException(e, this);
 91  0
             }
 92  0
         }
 93  0
     }
 94  
 
 95  
     public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
 96  
     {
 97  0
         Object results[] = new Object[arguments.size()];
 98  0
         int i = 0;
 99  0
         for (Iterator iterator = arguments.iterator(); iterator.hasNext(); i++)
 100  
         {
 101  0
             Argument argument = (Argument) iterator.next();
 102  
             try
 103  
             {
 104  0
                 results[i] = ExpressionEvaluatorManager.evaluate(argument.getFullExpression(), message);
 105  
             }
 106  0
             catch (ExpressionRuntimeException e)
 107  
             {
 108  0
                 throw new TransformerException(this, e);
 109  0
             }
 110  
 
 111  0
             if (!argument.isOptional() && results[i] == null)
 112  
             {
 113  0
                 throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull(
 114  
                         argument.getEvaluator(), argument.getExpression()), this);
 115  
 
 116  
             }
 117  
 
 118  
         }
 119  0
         if (isReturnSourceIfNull() && checkIfAllAreNull(results))
 120  
         {
 121  0
             return message;
 122  
         }
 123  
 
 124  0
         if (results.length == 1)
 125  
         {
 126  0
             return results[0];
 127  
         }
 128  
         else
 129  
         {
 130  0
             return results;
 131  
         }
 132  
     }
 133  
 
 134  
     private boolean checkIfAllAreNull(Object[] objects)
 135  
     {
 136  0
         for (int i = 0; i < objects.length; i++)
 137  
         {
 138  0
             if (objects[i] != null)
 139  
             {
 140  0
                 return false;
 141  
             }
 142  
         }
 143  0
         return true;
 144  
     }
 145  
 
 146  
     public List getArguments()
 147  
     {
 148  0
         return arguments;
 149  
     }
 150  
 
 151  
     public void setArguments(List arguments)
 152  
     {
 153  0
         this.arguments = arguments;
 154  0
     }
 155  
 
 156  
     public boolean isReturnSourceIfNull()
 157  
     {
 158  0
         return returnSourceIfNull;
 159  
     }
 160  
 
 161  
     public void setReturnSourceIfNull(boolean returnSourceIfNull)
 162  
     {
 163  0
         this.returnSourceIfNull = returnSourceIfNull;
 164  0
     }
 165  
 
 166  
     public static class Argument
 167  
     {
 168  
         public static final String EVAL_TOKEN = ":";
 169  
         private String expression;
 170  
         private String evaluator;
 171  
         private String customEvaluator;
 172  
         private boolean optional;
 173  
 
 174  
         public Argument()
 175  0
         {
 176  0
         }
 177  
 
 178  
         public String getCustomEvaluator()
 179  
         {
 180  0
             return customEvaluator;
 181  
         }
 182  
 
 183  
         public void setCustomEvaluator(String customEvaluator)
 184  
         {
 185  0
             this.customEvaluator = customEvaluator;
 186  0
         }
 187  
 
 188  
         public String getEvaluator()
 189  
         {
 190  0
             return evaluator;
 191  
         }
 192  
 
 193  
         public void setEvaluator(String evaluator)
 194  
         {
 195  0
             this.evaluator = evaluator;
 196  0
         }
 197  
 
 198  
         public String getExpression()
 199  
         {
 200  0
             return expression;
 201  
         }
 202  
 
 203  
         public void setExpression(String expression)
 204  
         {
 205  0
             this.expression = expression;
 206  0
         }
 207  
 
 208  
         public boolean isOptional()
 209  
         {
 210  0
             return optional;
 211  
         }
 212  
 
 213  
         public void setOptional(boolean optional)
 214  
         {
 215  0
             this.optional = optional;
 216  0
         }
 217  
 
 218  
         protected String getFullExpression()
 219  
         {
 220  
             //Sprecial handling of these evaluators since they don't retuen nul if some headers or attachments were found
 221  0
             if (!optional && (evaluator.equals("headers") || evaluator.equals("headers-list") ||
 222  
                     (evaluator.equals("attachments") || evaluator.equals("attachments-list"))))
 223  
             {
 224  0
                 return evaluator + EVAL_TOKEN + expression + "required";
 225  
             }
 226  0
             return evaluator + EVAL_TOKEN + expression;
 227  
         }
 228  
 
 229  
         protected void validate()
 230  
         {
 231  0
             if (expression == null)
 232  
             {
 233  0
                 throw new IllegalArgumentException(CoreMessages.objectIsNull("expression").getMessage());
 234  
             }
 235  
 
 236  0
             if (evaluator == null)
 237  
             {
 238  0
                 throw new IllegalArgumentException(CoreMessages.objectIsNull("evaluator").getMessage());
 239  
             }
 240  
 
 241  0
             if (evaluator.equals("custom"))
 242  
             {
 243  0
                 if (customEvaluator == null)
 244  
                 {
 245  0
                     throw new IllegalArgumentException(CoreMessages.objectIsNull("customEvaluator").getMessage());
 246  
                 }
 247  
                 else
 248  
                 {
 249  0
                     evaluator = customEvaluator;
 250  
                 }
 251  
             }
 252  
 
 253  0
             if (!ExpressionEvaluatorManager.isEvaluatorRegistered(evaluator))
 254  
             {
 255  0
                 throw new IllegalArgumentException(CoreMessages.expressionEvaluatorNotRegistered(evaluator).getMessage());
 256  
             }
 257  0
         }
 258  
     }
 259  
 }