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 */ 017package org.apache.camel.model.language; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.Expression; 027import org.apache.camel.Predicate; 028import org.apache.camel.builder.SimpleBuilder; 029import org.apache.camel.spi.Metadata; 030import org.apache.camel.util.ObjectHelper; 031 032/** 033 * To use Camels built-in Simple language in Camel expressions or predicates. 034 * 035 * @version 036 */ 037@Metadata(firstVersion = "1.1.0", label = "language,core,java", title = "Simple") 038@XmlRootElement(name = "simple") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class SimpleExpression extends ExpressionDefinition { 041 @XmlAttribute(name = "resultType") 042 private String resultTypeName; 043 @XmlTransient 044 private Class<?> resultType; 045 046 public SimpleExpression() { 047 } 048 049 public SimpleExpression(String expression) { 050 super(expression); 051 } 052 053 public SimpleExpression(Expression expression) { 054 super(expression); 055 } 056 057 public String getLanguage() { 058 return "simple"; 059 } 060 061 public Class<?> getResultType() { 062 return resultType; 063 } 064 065 /** 066 * Sets the class of the result type (type from output) 067 */ 068 public void setResultType(Class<?> resultType) { 069 this.resultType = resultType; 070 } 071 072 public String getResultTypeName() { 073 return resultTypeName; 074 } 075 076 /** 077 * Sets the class name of the result type (type from output) 078 */ 079 public void setResultTypeName(String resultTypeName) { 080 this.resultTypeName = resultTypeName; 081 } 082 083 @Override 084 public Expression createExpression(CamelContext camelContext) { 085 if (resultType == null && resultTypeName != null) { 086 try { 087 resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName); 088 } catch (ClassNotFoundException e) { 089 throw ObjectHelper.wrapRuntimeCamelException(e); 090 } 091 } 092 093 String exp = getExpression(); 094 // should be true by default 095 boolean isTrim = getTrim() == null || getTrim(); 096 if (exp != null && isTrim) { 097 exp = exp.trim(); 098 } 099 100 SimpleBuilder answer = new SimpleBuilder(exp); 101 answer.setResultType(resultType); 102 return answer; 103 } 104 105 @Override 106 public Predicate createPredicate(CamelContext camelContext) { 107 // SimpleBuilder is also a Predicate 108 return (Predicate) createExpression(camelContext); 109 } 110}