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;
018
019import org.apache.camel.Expression;
020import org.apache.camel.Predicate;
021import org.apache.camel.builder.SimpleBuilder;
022import org.apache.camel.builder.ValueBuilder;
023import org.apache.camel.builder.xml.XPathBuilder;
024import org.apache.camel.model.language.ExpressionDefinition;
025import org.apache.camel.model.language.SimpleExpression;
026import org.apache.camel.model.language.XPathExpression;
027
028/**
029 * Helper for {@link ExpressionNode}
030 */
031public final class ExpressionNodeHelper {
032
033    private ExpressionNodeHelper() {
034    }
035
036    /**
037     * Determines which {@link ExpressionDefinition} describes the given expression best possible.
038     * <p/>
039     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
040     * if the given expression is detect as such a type.
041     *
042     * @param expression the expression
043     * @return a definition which describes the expression
044     */
045    public static ExpressionDefinition toExpressionDefinition(Expression expression) {
046        if (expression instanceof SimpleBuilder) {
047            SimpleBuilder builder = (SimpleBuilder) expression;
048            // we keep the original expression by using the constructor that accepts an expression
049            SimpleExpression answer = new SimpleExpression(builder);
050            answer.setExpression(builder.getText());
051            answer.setResultType(builder.getResultType());
052            return answer;
053        } else if (expression instanceof XPathBuilder) {
054            XPathBuilder builder = (XPathBuilder) expression;
055            // we keep the original expression by using the constructor that accepts an expression
056            XPathExpression answer = new XPathExpression(builder);
057            answer.setExpression(builder.getText());
058            answer.setResultType(builder.getResultType());
059            return answer;
060        } else if (expression instanceof ValueBuilder) {
061            // ValueBuilder wraps the actual expression so unwrap
062            ValueBuilder builder = (ValueBuilder) expression;
063            expression = builder.getExpression();
064        }
065
066        if (expression instanceof ExpressionDefinition) {
067            return (ExpressionDefinition) expression;
068        }
069        return new ExpressionDefinition(expression);
070    }
071
072    /**
073     * Determines which {@link ExpressionDefinition} describes the given predicate best possible.
074     * <p/>
075     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
076     * if the given predicate is detect as such a type.
077     *
078     * @param predicate the predicate
079     * @return a definition which describes the predicate
080     */
081    public static ExpressionDefinition toExpressionDefinition(Predicate predicate) {
082        if (predicate instanceof SimpleBuilder) {
083            SimpleBuilder builder = (SimpleBuilder) predicate;
084            // we keep the original expression by using the constructor that accepts an expression
085            SimpleExpression answer = new SimpleExpression(builder);
086            answer.setExpression(builder.getText());
087            return answer;
088        } else if (predicate instanceof XPathBuilder) {
089            XPathBuilder builder = (XPathBuilder) predicate;
090            // we keep the original expression by using the constructor that accepts an expression
091            XPathExpression answer = new XPathExpression(builder);
092            answer.setExpression(builder.getText());
093            return answer;
094        } else if (predicate instanceof ValueBuilder) {
095            // ValueBuilder wraps the actual predicate so unwrap
096            ValueBuilder builder = (ValueBuilder) predicate;
097            Expression expression = builder.getExpression();
098            if (expression instanceof Predicate) {
099                predicate = (Predicate) expression;
100            }
101        }
102
103        if (predicate instanceof ExpressionDefinition) {
104            return (ExpressionDefinition) predicate;
105        }
106        return new ExpressionDefinition(predicate);
107    }
108}