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 builder = (ValueBuilder) expression; 062 expression = builder.getExpression(); 063 } 064 065 if (expression instanceof ExpressionDefinition) { 066 return (ExpressionDefinition) expression; 067 } 068 return new ExpressionDefinition(expression); 069 } 070 071 /** 072 * Determines which {@link ExpressionDefinition} describes the given predicate best possible. 073 * <p/> 074 * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc. 075 * if the given predicate is detect as such a type. 076 * 077 * @param predicate the predicate 078 * @return a definition which describes the predicate 079 */ 080 public static ExpressionDefinition toExpressionDefinition(Predicate predicate) { 081 if (predicate instanceof SimpleBuilder) { 082 SimpleBuilder builder = (SimpleBuilder) predicate; 083 // we keep the original expression by using the constructor that accepts an expression 084 SimpleExpression answer = new SimpleExpression(builder); 085 answer.setExpression(builder.getText()); 086 return answer; 087 } else if (predicate instanceof XPathBuilder) { 088 XPathBuilder builder = (XPathBuilder) predicate; 089 // we keep the original expression by using the constructor that accepts an expression 090 XPathExpression answer = new XPathExpression(builder); 091 answer.setExpression(builder.getText()); 092 return answer; 093 } 094 095 if (predicate instanceof ExpressionDefinition) { 096 return (ExpressionDefinition) predicate; 097 } 098 return new ExpressionDefinition(predicate); 099 } 100}