Coverage Report - org.apache.camel.builder.ExpressionBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionBuilder
61% 
86% 
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 java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.List;
 22  
 import java.util.StringTokenizer;
 23  
 import java.util.regex.Pattern;
 24  
 
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Expression;
 27  
 
 28  
 /**
 29  
  * @version $Revision: $
 30  
  */
 31  
 public class ExpressionBuilder {
 32  
 
 33  
 
 34  
     /**
 35  
      * Utility classes should not have a public constructor.
 36  
      */
 37  0
     private ExpressionBuilder() {        
 38  0
     }
 39  
     
 40  
     /**
 41  
      * Returns an expression for the header value with the given name
 42  
      * 
 43  
      * @param headerName the name of the header the expression will return
 44  
      * @return an expression object which will return the header value
 45  
      */
 46  
     public static <E extends Exchange> Expression<E> headerExpression(final String headerName) {
 47  165
         return new Expression<E>() {
 48  
             public Object evaluate(E exchange) {
 49  603
                 Object header = exchange.getIn().getHeader(headerName);
 50  603
                 if (header == null) {
 51  
                     // lets try the exchange header
 52  3
                     header = exchange.getProperty(headerName);
 53  
                 }
 54  603
                 return header;
 55  
             }
 56  
 
 57  
             @Override
 58  165
             public String toString() {
 59  216
                 return "header(" + headerName + ")";
 60  
             }
 61  
         };
 62  
     }
 63  
 
 64  
     /**
 65  
      * Returns an expression for the out header value with the given name
 66  
      * 
 67  
      * @param headerName the name of the header the expression will return
 68  
      * @return an expression object which will return the header value
 69  
      */
 70  
     public static <E extends Exchange> Expression<E> outHeaderExpression(final String headerName) {
 71  0
         return new Expression<E>() {
 72  
             public Object evaluate(E exchange) {
 73  0
                 Object header = exchange.getOut().getHeader(headerName);
 74  0
                 if (header == null) {
 75  
                     // lets try the exchange header
 76  0
                     header = exchange.getProperty(headerName);
 77  
                 }
 78  0
                 return header;
 79  
             }
 80  
 
 81  
             @Override
 82  0
             public String toString() {
 83  0
                 return "outHeader(" + headerName + ")";
 84  
             }
 85  
         };
 86  
     }
 87  
 
 88  
     /**
 89  
      * Returns an expression for the property value with the given name
 90  
      * 
 91  
      * @param propertyName the name of the property the expression will return
 92  
      * @return an expression object which will return the property value
 93  
      */
 94  
     public static <E extends Exchange> Expression<E> propertyExpression(final String propertyName) {
 95  0
         return new Expression<E>() {
 96  
             public Object evaluate(E exchange) {
 97  0
                 return exchange.getProperty(propertyName);
 98  
             }
 99  
 
 100  
             @Override
 101  0
             public String toString() {
 102  0
                 return "property(" + propertyName + ")";
 103  
             }
 104  
         };
 105  
     }
 106  
 
 107  
     /**
 108  
      * Returns an expression for a system property value with the given name
 109  
      * 
 110  
      * @param propertyName the name of the system property the expression will
 111  
      *                return
 112  
      * @return an expression object which will return the system property value
 113  
      */
 114  
     public static <E extends Exchange> Expression<E> systemPropertyExpression(final String propertyName) {
 115  0
         return systemPropertyExpression(propertyName, null);
 116  
     }
 117  
 
 118  
     /**
 119  
      * Returns an expression for a system property value with the given name
 120  
      * 
 121  
      * @param propertyName the name of the system property the expression will
 122  
      *                return
 123  
      * @return an expression object which will return the system property value
 124  
      */
 125  
     public static <E extends Exchange> Expression<E> systemPropertyExpression(final String propertyName,
 126  
                                                                               final String defaultValue) {
 127  0
         return new Expression<E>() {
 128  
             public Object evaluate(E exchange) {
 129  0
                 return System.getProperty(propertyName, defaultValue);
 130  
             }
 131  
 
 132  
             @Override
 133  0
             public String toString() {
 134  0
                 return "systemProperty(" + propertyName + ")";
 135  
             }
 136  
         };
 137  
     }
 138  
 
 139  
     /**
 140  
      * Returns an expression for the contant value
 141  
      * 
 142  
      * @param value the value the expression will return
 143  
      * @return an expression object which will return the constant value
 144  
      */
 145  
     public static <E extends Exchange> Expression<E> constantExpression(final Object value) {
 146  351
         return new Expression<E>() {
 147  
             public Object evaluate(E exchange) {
 148  102
                 return value;
 149  
             }
 150  
 
 151  
             @Override
 152  351
             public String toString() {
 153  414
                 return "" + value;
 154  
             }
 155  
         };
 156  
     }
 157  
 
 158  
     /**
 159  
      * Returns the expression for the exchanges inbound message body
 160  
      */
 161  
     public static <E extends Exchange> Expression<E> bodyExpression() {
 162  21
         return new Expression<E>() {
 163  
             public Object evaluate(E exchange) {
 164  48
                 return exchange.getIn().getBody();
 165  
             }
 166  
 
 167  
             @Override
 168  21
             public String toString() {
 169  33
                 return "body";
 170  
             }
 171  
         };
 172  
     }
 173  
 
 174  
     /**
 175  
      * Returns the expression for the exchanges inbound message body converted
 176  
      * to the given type
 177  
      */
 178  
     public static <E extends Exchange, T> Expression<E> bodyExpression(final Class<T> type) {
 179  6
         return new Expression<E>() {
 180  
             public Object evaluate(E exchange) {
 181  0
                 return exchange.getIn().getBody(type);
 182  
             }
 183  
 
 184  
             @Override
 185  6
             public String toString() {
 186  9
                 return "bodyAs[" + type.getName() + "]";
 187  
             }
 188  
         };
 189  
     }
 190  
 
 191  
     /**
 192  
      * Returns the expression for the out messages body
 193  
      */
 194  
     public static <E extends Exchange> Expression<E> outBodyExpression() {
 195  0
         return new Expression<E>() {
 196  
             public Object evaluate(E exchange) {
 197  0
                 return exchange.getOut().getBody();
 198  
             }
 199  
 
 200  
             @Override
 201  0
             public String toString() {
 202  0
                 return "outBody";
 203  
             }
 204  
         };
 205  
     }
 206  
 
 207  
     /**
 208  
      * Returns the expression for the exchange
 209  
      */
 210  
     public static <E extends Exchange> Expression<E> exchangeExpression() {
 211  0
         return new Expression<E>() {
 212  
             public Object evaluate(E exchange) {
 213  0
                 return exchange;
 214  
             }
 215  
 
 216  
             @Override
 217  0
             public String toString() {
 218  0
                 return "exchange";
 219  
             }
 220  
         };
 221  
     }
 222  
 
 223  
     /**
 224  
      * Returns the expression for the IN message
 225  
      */
 226  
     public static <E extends Exchange> Expression<E> inMessageExpression() {
 227  0
         return new Expression<E>() {
 228  
             public Object evaluate(E exchange) {
 229  0
                 return exchange.getIn();
 230  
             }
 231  
 
 232  
             @Override
 233  0
             public String toString() {
 234  0
                 return "inMessage";
 235  
             }
 236  
         };
 237  
     }
 238  
 
 239  
     /**
 240  
      * Returns an expression which converts the given expression to the given
 241  
      * type
 242  
      */
 243  
     public static <E extends Exchange> Expression<E> convertTo(final Expression expression, final Class type) {
 244  12
         return new Expression<E>() {
 245  
             public Object evaluate(E exchange) {
 246  108
                 Object value = expression.evaluate(exchange);
 247  108
                 return exchange.getContext().getTypeConverter().convertTo(type, value);
 248  
             }
 249  
 
 250  
             @Override
 251  12
             public String toString() {
 252  6
                 return "" + expression + ".convertTo(" + type.getName() + ".class)";
 253  
             }
 254  
         };
 255  
     }
 256  
 
 257  
     /**
 258  
      * Returns a tokenize expression which will tokenize the string with the
 259  
      * given token
 260  
      */
 261  
     public static <E extends Exchange> Expression<E> tokenizeExpression(final Expression<E> expression,
 262  
                                                                         final String token) {
 263  9
         return new Expression<E>() {
 264  
             public Object evaluate(E exchange) {
 265  6
                 String text = evaluateStringExpression(expression, exchange);
 266  6
                 if (text == null) {
 267  0
                     return null;
 268  
                 }
 269  6
                 StringTokenizer iter = new StringTokenizer(text, token);
 270  6
                 List<String> answer = new ArrayList<String>();
 271  27
                 while (iter.hasMoreTokens()) {
 272  21
                     answer.add(iter.nextToken());
 273  21
                 }
 274  6
                 return answer;
 275  
             }
 276  
 
 277  
             @Override
 278  9
             public String toString() {
 279  15
                 return "tokenize(" + expression + ", " + token + ")";
 280  
             }
 281  
         };
 282  
     }
 283  
 
 284  
     /**
 285  
      * Returns a tokenize expression which will tokenize the string with the
 286  
      * given regex
 287  
      */
 288  
     public static <E extends Exchange> Expression<E> regexTokenize(final Expression<E> expression,
 289  
                                                                    String regexTokenizer) {
 290  9
         final Pattern pattern = Pattern.compile(regexTokenizer);
 291  9
         return new Expression<E>() {
 292  
             public Object evaluate(E exchange) {
 293  12
                 String text = evaluateStringExpression(expression, exchange);
 294  12
                 if (text == null) {
 295  0
                     return null;
 296  
                 }
 297  12
                 return Arrays.asList(pattern.split(text));
 298  
             }
 299  
 
 300  
             @Override
 301  9
             public String toString() {
 302  18
                 return "regexTokenize(" + expression + ", " + pattern.pattern() + ")";
 303  
             }
 304  
         };
 305  
     }
 306  
 
 307  
     /**
 308  
      * Transforms the expression into a String then performs the regex
 309  
      * replaceAll to transform the String and return the result
 310  
      */
 311  
     public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E> expression,
 312  
                                                                      String regex, final String replacement) {
 313  3
         final Pattern pattern = Pattern.compile(regex);
 314  3
         return new Expression<E>() {
 315  
             public Object evaluate(E exchange) {
 316  3
                 String text = evaluateStringExpression(expression, exchange);
 317  3
                 if (text == null) {
 318  0
                     return null;
 319  
                 }
 320  3
                 return pattern.matcher(text).replaceAll(replacement);
 321  
             }
 322  
 
 323  
             @Override
 324  3
             public String toString() {
 325  6
                 return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
 326  
             }
 327  
         };
 328  
     }
 329  
 
 330  
     /**
 331  
      * Transforms the expression into a String then performs the regex
 332  
      * replaceAll to transform the String and return the result
 333  
      */
 334  
     public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E> expression,
 335  
                                                                      String regex,
 336  
                                                                      final Expression<E> replacementExpression) {
 337  3
         final Pattern pattern = Pattern.compile(regex);
 338  3
         return new Expression<E>() {
 339  
             public Object evaluate(E exchange) {
 340  3
                 String text = evaluateStringExpression(expression, exchange);
 341  3
                 String replacement = evaluateStringExpression(replacementExpression, exchange);
 342  3
                 if (text == null || replacement == null) {
 343  0
                     return null;
 344  
                 }
 345  3
                 return pattern.matcher(text).replaceAll(replacement);
 346  
             }
 347  
 
 348  
             @Override
 349  3
             public String toString() {
 350  6
                 return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
 351  
             }
 352  
         };
 353  
     }
 354  
 
 355  
     /**
 356  
      * Appends the String evaluations of the two expressions together
 357  
      */
 358  
     public static <E extends Exchange> Expression<E> append(final Expression<E> left,
 359  
                                                             final Expression<E> right) {
 360  3
         return new Expression<E>() {
 361  
             public Object evaluate(E exchange) {
 362  3
                 return evaluateStringExpression(left, exchange) + evaluateStringExpression(right, exchange);
 363  
             }
 364  
 
 365  
             @Override
 366  3
             public String toString() {
 367  3
                 return "append(" + left + ", " + right + ")";
 368  
             }
 369  
         };
 370  
     }
 371  
 
 372  
     /**
 373  
      * Evaluates the expression on the given exchange and returns the String
 374  
      * representation
 375  
      * 
 376  
      * @param expression the expression to evaluate
 377  
      * @param exchange the exchange to use to evaluate the expression
 378  
      * @return the String representation of the expression or null if it could
 379  
      *         not be evaluated
 380  
      */
 381  
     public static <E extends Exchange> String evaluateStringExpression(Expression<E> expression, E exchange) {
 382  33
         Object value = expression.evaluate(exchange);
 383  33
         return exchange.getContext().getTypeConverter().convertTo(String.class, value);
 384  
     }
 385  
 
 386  
     /**
 387  
      * Returns an expression for the given system property
 388  
      */
 389  
     public static <E extends Exchange> Expression<E> systemProperty(final String name) {
 390  0
         return systemProperty(name, null);
 391  
     }
 392  
 
 393  
     /**
 394  
      * Returns an expression for the given system property
 395  
      */
 396  
     public static <E extends Exchange> Expression<E> systemProperty(final String name,
 397  
                                                                     final String defaultValue) {
 398  0
         return new Expression<E>() {
 399  0
             public Object evaluate(E exchange) {
 400  0
                 return System.getProperty(name, defaultValue);
 401  
             }
 402  
         };
 403  
     }
 404  
 }