Coverage Report - org.apache.camel.builder.ExpressionBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionBuilder
64% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.StringTokenizer;
 25  
 import java.util.Arrays;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 /**
 29  
  * @version $Revision: $
 30  
  */
 31  0
 public class ExpressionBuilder {
 32  
 
 33  
 
 34  
     /**
 35  
      * Returns an expression for the header value with the given name
 36  
      *
 37  
      * @param headerName the name of the header the expression will return
 38  
      * @return an expression object which will return the header value
 39  
      */
 40  
     public static <E extends Exchange> Expression<E> headerExpression(final String headerName) {
 41  35
         return new Expression<E>() {
 42  
             public Object evaluate(E exchange) {
 43  41
                 Object header = exchange.getIn().getHeader(headerName);
 44  41
                 if (header == null) {
 45  
                     // lets try the exchange header
 46  0
                     header = exchange.getProperty(headerName);
 47  
                 }
 48  41
                 return header;
 49  
             }
 50  
             
 51  
 
 52  
             @Override
 53  35
             public String toString() {
 54  68
                 return "header(" + headerName + ")";
 55  
             }
 56  
         };
 57  
     }
 58  
 
 59  
     /**
 60  
      * Returns an expression for the property value with the given name
 61  
      *
 62  
      * @param propertyName the name of the property the expression will return
 63  
      * @return an expression object which will return the property value
 64  
      */
 65  
     public static <E extends Exchange> Expression<E> propertyExpression(final String propertyName) {
 66  0
         return new Expression<E>() {
 67  
             public Object evaluate(E exchange) {
 68  0
                 return exchange.getProperty(propertyName);
 69  
             }
 70  
 
 71  
             @Override
 72  0
             public String toString() {
 73  0
                 return "property(" + propertyName + ")";
 74  
             }
 75  
         };
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns an expression for the contant value
 80  
      *
 81  
      * @param value the value the expression will return
 82  
      * @return an expression object which will return the constant value
 83  
      */
 84  
     public static <E extends Exchange> Expression<E> constantExpression(final Object value) {
 85  108
         return new Expression<E>() {
 86  
             public Object evaluate(E exchange) {
 87  26
                 return value;
 88  
             }
 89  
 
 90  
             @Override
 91  108
             public String toString() {
 92  184
                 return "" + value;
 93  
             }
 94  
         };
 95  
     }
 96  
 
 97  
     /**
 98  
      * Returns the expression for the exchanges inbound message body
 99  
      */
 100  
     public static <E extends Exchange> Expression<E> bodyExpression() {
 101  4
         return new Expression<E>() {
 102  
             public Object evaluate(E exchange) {
 103  12
                 return exchange.getIn().getBody();
 104  
             }
 105  
 
 106  
             @Override
 107  4
             public String toString() {
 108  5
                 return "body";
 109  
             }
 110  
         };
 111  
     }
 112  
 
 113  
 
 114  
     /**
 115  
      * Returns the expression for the exchanges inbound message body converted to the given type
 116  
      */
 117  
     public static <E extends Exchange, T> Expression<E> bodyExpression(final Class<T> type) {
 118  1
         return new Expression<E>() {
 119  
             public Object evaluate(E exchange) {
 120  0
                 return exchange.getIn().getBody(type);
 121  
             }
 122  
 
 123  
             @Override
 124  1
             public String toString() {
 125  4
                 return "bodyAs[" + type.getName() + "]";
 126  
             }
 127  
         };
 128  
     }
 129  
 
 130  
     /**
 131  
      * Returns the expression for the out messages body
 132  
      */
 133  
     public static <E extends Exchange> Expression<E> outBodyExpression() {
 134  0
         return new Expression<E>() {
 135  
             public Object evaluate(E exchange) {
 136  0
                 return exchange.getOut().getBody();
 137  
             }
 138  
 
 139  
             @Override
 140  0
             public String toString() {
 141  0
                 return "outBody";
 142  
             }
 143  
         };
 144  
     }
 145  
 
 146  
     /**
 147  
      * Returns the expression for the exchange
 148  
      */
 149  
     public static <E extends Exchange> Expression<E> exchangeExpression() {
 150  0
         return new Expression<E>() {
 151  
             public Object evaluate(E exchange) {
 152  0
                 return exchange;
 153  
             }
 154  
 
 155  
             @Override
 156  0
             public String toString() {
 157  0
                 return "exchange";
 158  
             }
 159  
         };
 160  
     }
 161  
 
 162  
     /**
 163  
      * Returns the expression for the IN message
 164  
      */
 165  
     public static <E extends Exchange> Expression<E> inMessageExpression() {
 166  0
         return new Expression<E>() {
 167  
             public Object evaluate(E exchange) {
 168  0
                 return exchange.getIn();
 169  
             }
 170  
 
 171  
             @Override
 172  0
             public String toString() {
 173  0
                 return "inMessage";
 174  
             }
 175  
         };
 176  
     }
 177  
 
 178  
     /**
 179  
      * Returns an expression which converts the given expression to the given type
 180  
      */
 181  
     public static <E extends Exchange> Expression<E> convertTo(final Expression expression, final Class type) {
 182  0
         return new Expression<E>() {
 183  
             public Object evaluate(E exchange) {
 184  0
                 Object value = expression.evaluate(exchange);
 185  0
                 return exchange.getContext().getTypeConverter().convertTo(type, value);
 186  
             }
 187  
 
 188  
             @Override
 189  0
             public String toString() {
 190  0
                 return "convertTo(" + expression + ", " + type + ")";
 191  
             }
 192  
         };
 193  
     }
 194  
 
 195  
     /**
 196  
      * Returns a tokenize expression which will tokenize the string with the given token
 197  
      */
 198  
     public static <E extends Exchange> Expression<E> tokenizeExpression(final Expression<E> expression, final String token) {
 199  3
         return new Expression<E>() {
 200  
             public Object evaluate(E exchange) {
 201  2
                 String text = evaluateStringExpression(expression, exchange);
 202  2
                 if (text == null) {
 203  0
                     return null;
 204  
                 }
 205  2
                 StringTokenizer iter = new StringTokenizer(text, token);
 206  2
                 List<String> answer = new ArrayList<String>();
 207  9
                 while (iter.hasMoreTokens()) {
 208  7
                     answer.add(iter.nextToken());
 209  7
                 }
 210  2
                 return answer;
 211  
             }
 212  
 
 213  
             @Override
 214  3
             public String toString() {
 215  6
                 return "tokenize(" + expression + ", " + token + ")";
 216  
             }
 217  
         };
 218  
     }
 219  
 
 220  
     /**
 221  
      * Returns a tokenize expression which will tokenize the string with the given regex
 222  
      */
 223  
     public static <E extends Exchange> Expression<E> regexTokenize(final Expression<E> expression, String regexTokenizer) {
 224  3
         final Pattern pattern = Pattern.compile(regexTokenizer);
 225  3
         return new Expression<E>() {
 226  
             public Object evaluate(E exchange) {
 227  4
                 String text = evaluateStringExpression(expression, exchange);
 228  4
                 if (text == null) {
 229  0
                     return null;
 230  
                 }
 231  4
                 return Arrays.asList(pattern.split(text));
 232  
             }
 233  
 
 234  
             @Override
 235  3
             public String toString() {
 236  6
                 return "regexTokenize(" + expression + ", " + pattern.pattern() + ")";
 237  
             }
 238  
         };
 239  
     }
 240  
 
 241  
     /**
 242  
      * Transforms the expression into a String then performs the regex replaceAll to transform the String and return the result
 243  
      */
 244  
     public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E> expression, String regex, final String replacement) {
 245  1
         final Pattern pattern = Pattern.compile(regex);
 246  1
         return new Expression<E>() {
 247  
             public Object evaluate(E exchange) {
 248  1
                 String text = evaluateStringExpression(expression, exchange);
 249  1
                 if (text == null) {
 250  0
                     return null;
 251  
                 }
 252  1
                 return pattern.matcher(text).replaceAll(replacement);
 253  
             }
 254  
 
 255  
             @Override
 256  1
             public String toString() {
 257  2
                 return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
 258  
             }
 259  
         };
 260  
     }
 261  
 
 262  
     /**
 263  
      * Transforms the expression into a String then performs the regex replaceAll to transform the String and return the result
 264  
      */
 265  
     public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E> expression, String regex, final Expression<E> replacementExpression) {
 266  1
         final Pattern pattern = Pattern.compile(regex);
 267  1
         return new Expression<E>() {
 268  
             public Object evaluate(E exchange) {
 269  1
                 String text = evaluateStringExpression(expression, exchange);
 270  1
                 String replacement = evaluateStringExpression(replacementExpression, exchange);;
 271  1
                 if (text == null || replacement == null) {
 272  0
                     return null;
 273  
                 }
 274  1
                 return pattern.matcher(text).replaceAll(replacement);
 275  
             }
 276  
 
 277  
             @Override
 278  1
             public String toString() {
 279  2
                 return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
 280  
             }
 281  
         };
 282  
     }
 283  
 
 284  
 
 285  
     /**
 286  
      * Appends the String evaluations of the two expressions together
 287  
      */
 288  
     public static <E extends Exchange> Expression<E> append(final Expression<E> left, final Expression<E> right) {
 289  1
         return new Expression<E>() {
 290  
             public Object evaluate(E exchange) {
 291  1
                 return evaluateStringExpression(left, exchange) + evaluateStringExpression(right, exchange);
 292  
             }
 293  
 
 294  
             @Override
 295  1
             public String toString() {
 296  1
                 return "append(" + left + ", " + right + ")";
 297  
             }
 298  
         };
 299  
     }
 300  
 
 301  
     /**
 302  
      * Evaluates the expression on the given exchange and returns the String representation
 303  
      *
 304  
      * @param expression the expression to evaluate
 305  
      * @param exchange the exchange to use to evaluate the expression
 306  
      * @return the String representation of the expression or null if it could not be evaluated
 307  
      */
 308  
     public static <E extends Exchange> String evaluateStringExpression(Expression<E> expression, E exchange) {
 309  11
         Object value = expression.evaluate(exchange);
 310  11
         return exchange.getContext().getTypeConverter().convertTo(String.class, value);
 311  
     }
 312  
 
 313  
     /**
 314  
      * Returns an expression for the given system property
 315  
      */
 316  
     public static <E extends Exchange> Expression<E> systemProperty(final String name) {
 317  0
         return systemProperty(name, null);
 318  
     }
 319  
 
 320  
     /**
 321  
      * Returns an expression for the given system property
 322  
      */
 323  
     public static <E extends Exchange> Expression<E> systemProperty(final String name, final String defaultValue) {
 324  0
         return new Expression<E>() {
 325  0
             public Object evaluate(E exchange) {
 326  0
                 return System.getProperty(name, defaultValue);
 327  
             }
 328  
         };
 329  
     }
 330  
 }