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.model.language;
018
019 import javax.xml.bind.annotation.XmlAccessType;
020 import javax.xml.bind.annotation.XmlAccessorType;
021 import javax.xml.bind.annotation.XmlAttribute;
022 import javax.xml.bind.annotation.XmlRootElement;
023 import javax.xml.bind.annotation.XmlTransient;
024
025 import org.apache.camel.CamelContext;
026 import org.apache.camel.Expression;
027 import org.apache.camel.Predicate;
028 import org.apache.camel.builder.SimpleBuilder;
029 import org.apache.camel.util.ObjectHelper;
030
031 /**
032 * For expressions and predicates using the
033 * <a href="http://camel.apache.org/simple.html">simple language</a>
034 *
035 * @version
036 */
037 @XmlRootElement(name = "simple")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class SimpleExpression extends ExpressionDefinition {
040 @XmlAttribute(name = "resultType")
041 private String resultTypeName;
042 @XmlTransient
043 private Class<?> resultType;
044
045 public SimpleExpression() {
046 }
047
048 public SimpleExpression(String expression) {
049 super(expression);
050 }
051
052 public SimpleExpression(Expression expression) {
053 super(expression);
054 }
055
056 public String getLanguage() {
057 return "simple";
058 }
059
060 public Class<?> getResultType() {
061 return resultType;
062 }
063
064 public void setResultType(Class<?> resultType) {
065 this.resultType = resultType;
066 }
067
068 public String getResultTypeName() {
069 return resultTypeName;
070 }
071
072 public void setResultTypeName(String resultTypeName) {
073 this.resultTypeName = resultTypeName;
074 }
075
076 @Override
077 public Expression createExpression(CamelContext camelContext) {
078 if (resultType == null && resultTypeName != null) {
079 try {
080 resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName);
081 } catch (ClassNotFoundException e) {
082 throw ObjectHelper.wrapRuntimeCamelException(e);
083 }
084 }
085
086 String exp = getExpression();
087 if (isTrim() && exp != null) {
088 exp = exp.trim();
089 }
090
091 SimpleBuilder answer = new SimpleBuilder(exp);
092 answer.setResultType(resultType);
093 return answer;
094 }
095
096 @Override
097 public Predicate createPredicate(CamelContext camelContext) {
098 // SimpleBuilder is also a Predicate
099 return (Predicate) createExpression(camelContext);
100 }
101 }