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.reifier.language;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.Exchange;
024import org.apache.camel.Expression;
025import org.apache.camel.Predicate;
026import org.apache.camel.builder.SimpleBuilder;
027import org.apache.camel.model.language.ExpressionDefinition;
028import org.apache.camel.model.language.SimpleExpression;
029
030public class SimpleExpressionReifier extends ExpressionReifier<SimpleExpression> {
031
032    public SimpleExpressionReifier(CamelContext camelContext, ExpressionDefinition definition) {
033        super(camelContext, (SimpleExpression) definition);
034    }
035
036    @Override
037    public Expression createExpression() {
038        Expression expr = createBuilder().createExpression(camelContext);
039        return new Expression() {
040            @Override
041            public <T> T evaluate(Exchange exchange, Class<T> type) {
042                return expr.evaluate(exchange, type);
043            }
044            @Override
045            public String toString() {
046                return definition.getExpression();
047            }
048        };
049    }
050
051    @Override
052    public Predicate createPredicate() {
053        Predicate pred = createBuilder().createPredicate(camelContext);
054        return new Predicate() {
055            @Override
056            public boolean matches(Exchange exchange) {
057                return pred.matches(exchange);
058            }
059            @Override
060            public String toString() {
061                return definition.getExpression();
062            }
063        };
064    }
065
066    protected SimpleBuilder createBuilder() {
067        String exp = parseString(definition.getExpression());
068        // should be true by default
069        boolean isTrim = parseBoolean(definition.getTrim(), true);
070        if (exp != null && isTrim) {
071            exp = exp.trim();
072        }
073        SimpleBuilder answer = new SimpleBuilder(exp);
074        Map<String, Object> props = new HashMap<>();
075        props.put("resultType", or(definition.getResultType(), definition.getResultTypeName()));
076        setProperties(answer, props);
077        return answer;
078    }
079
080}