Coverage Report - org.apache.camel.language.simple.SimpleLanguage
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleLanguage
58% 
67% 
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.language.simple;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 import org.apache.camel.Predicate;
 22  
 import org.apache.camel.builder.ExpressionBuilder;
 23  
 import org.apache.camel.builder.PredicateBuilder;
 24  
 import org.apache.camel.language.IllegalSyntaxException;
 25  
 import org.apache.camel.spi.Language;
 26  
 import org.apache.camel.util.ObjectHelper;
 27  
 
 28  
 /**
 29  
  * A <a href="http://activemq.apache.org/camel/simple.html>simple language</a>
 30  
  * which maps simple property style notations to acces headers and bodies.
 31  
  * Examples of supported expressions are <p/>
 32  
  * <ul>
 33  
  * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
 34  
  * <li>in.body or body to access the inbound body</li>
 35  
  * <li>out.header.foo to access an outbound header called 'foo'</li>
 36  
  * <li>out.body to access the inbound body</li>
 37  
  * <li>property.foo to access the exchange property called 'foo'</li>
 38  
  * <li>sys.foo to access the system property called 'foo'</li>
 39  
  * </ul>
 40  
  * 
 41  
  * @version $Revision: $
 42  
  */
 43  21
 public class SimpleLanguage implements Language {
 44  
 
 45  
     public Predicate<Exchange> createPredicate(String expression) {
 46  9
         return PredicateBuilder.toPredicate(createExpression(expression));
 47  
     }
 48  
 
 49  
     public Expression<Exchange> createExpression(String expression) {
 50  21
         if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
 51  9
             return ExpressionBuilder.bodyExpression();
 52  12
         } else if (ObjectHelper.equals(expression, "out.body")) {
 53  0
             return ExpressionBuilder.outBodyExpression();
 54  
         }
 55  12
         String remainder = ifStartsWithReturnRemainder("in.header.", expression);
 56  12
         if (remainder == null) {
 57  9
             remainder = ifStartsWithReturnRemainder("header.", expression);
 58  
         }
 59  12
         if (remainder != null) {
 60  12
             return ExpressionBuilder.headerExpression(remainder);
 61  
         }
 62  0
         remainder = ifStartsWithReturnRemainder("out.header.", expression);
 63  0
         if (remainder != null) {
 64  0
             return ExpressionBuilder.outHeaderExpression(remainder);
 65  
         }
 66  0
         remainder = ifStartsWithReturnRemainder("property.", expression);
 67  0
         if (remainder != null) {
 68  0
             return ExpressionBuilder.propertyExpression(remainder);
 69  
         }
 70  0
         remainder = ifStartsWithReturnRemainder("sys.", expression);
 71  0
         if (remainder != null) {
 72  0
             return ExpressionBuilder.propertyExpression(remainder);
 73  
         }
 74  0
         throw new IllegalSyntaxException(this, expression);
 75  
     }
 76  
 
 77  
     protected String ifStartsWithReturnRemainder(String prefix, String text) {
 78  21
         if (text.startsWith(prefix)) {
 79  12
             String remainder = text.substring(prefix.length());
 80  12
             if (remainder.length() > 0) {
 81  
 
 82  12
                 return remainder;
 83  
             }
 84  
         }
 85  9
         return null;
 86  
     }
 87  
 }