001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.language.simple; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Expression; 021 import org.apache.camel.builder.ExpressionBuilder; 022 import org.apache.camel.language.IllegalSyntaxException; 023 import org.apache.camel.util.ObjectHelper; 024 025 /** 026 * A <a href="http://activemq.apache.org/camel/simple.html">simple language</a> 027 * which maps simple property style notations to access headers and bodies. 028 * Examples of supported expressions are: 029 * <ul> 030 * <li>id to access the inbound message Id</li> 031 * <li>in.body or body to access the inbound body</li> 032 * <li>out.body to access the inbound body</li> 033 * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li> 034 * <li>out.header.foo to access an outbound header called 'foo'</li> 035 * <li>property.foo to access the exchange property called 'foo'</li> 036 * <li>sys.foo to access the system property called 'foo'</li> 037 * <li>date:<command>:<pattern> for date formatting using the {@link java.text.SimpleDateFormat} patterns. 038 * Supported commands are: <tt>now</tt> for current timestamp, 039 * <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header. 040 * <tt>out.header.xxx</tt> to use the Date object in the out header. 041 * </li> 042 * <li>bean:<bean expression> to invoke a bean using the 043 * {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li> 044 * </ul> 045 * 046 * @version $Revision: 1293 $ 047 */ 048 public class SimpleLanguage extends AbstractSimpleLanguage { 049 050 public static Expression simple(String expression) { 051 SimpleLanguage language = new SimpleLanguage(); 052 return language.createExpression(expression); 053 } 054 055 protected Expression<Exchange> createSimpleExpression(String expression) { 056 if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) { 057 return ExpressionBuilder.bodyExpression(); 058 } else if (ObjectHelper.equal(expression, "out.body")) { 059 return ExpressionBuilder.outBodyExpression(); 060 } else if (ObjectHelper.equal(expression, "id")) { 061 return ExpressionBuilder.messageIdExpression(); 062 } 063 064 // in header expression 065 String remainder = ifStartsWithReturnRemainder("in.header.", expression); 066 if (remainder == null) { 067 remainder = ifStartsWithReturnRemainder("header.", expression); 068 } 069 if (remainder == null) { 070 remainder = ifStartsWithReturnRemainder("headers.", expression); 071 } 072 if (remainder == null) { 073 remainder = ifStartsWithReturnRemainder("in.headers.", expression); 074 } 075 if (remainder != null) { 076 return ExpressionBuilder.headerExpression(remainder); 077 } 078 079 // out header expression 080 remainder = ifStartsWithReturnRemainder("out.header.", expression); 081 if (remainder == null) { 082 remainder = ifStartsWithReturnRemainder("out.headers.", expression); 083 } 084 if (remainder != null) { 085 return ExpressionBuilder.outHeaderExpression(remainder); 086 } 087 088 // property 089 remainder = ifStartsWithReturnRemainder("property.", expression); 090 if (remainder != null) { 091 return ExpressionBuilder.propertyExpression(remainder); 092 } 093 094 // system property 095 remainder = ifStartsWithReturnRemainder("sys.", expression); 096 if (remainder != null) { 097 return ExpressionBuilder.systemProperty(remainder); 098 } 099 100 // date: prefix 101 remainder = ifStartsWithReturnRemainder("date:", expression); 102 if (remainder != null) { 103 String[] parts = remainder.split(":"); 104 if (parts.length != 2) { 105 throw new IllegalSyntaxException(this, expression + " ${date:command:pattern} is the correct syntax."); 106 } 107 String command = parts[0]; 108 String pattern = parts[1]; 109 return ExpressionBuilder.dateExpression(command, pattern); 110 } 111 112 // bean: prefix 113 remainder = ifStartsWithReturnRemainder("bean:", expression); 114 if (remainder != null) { 115 return ExpressionBuilder.beanExpression(remainder); 116 } 117 118 throw new IllegalSyntaxException(this, expression); 119 } 120 121 }