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.model.language.ExpressionDefinition; 024import org.apache.camel.model.language.SimpleExpression; 025import org.apache.camel.model.language.XPathExpression; 026import org.apache.camel.spi.ExpressionResultTypeAware; 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 038 * expression best possible. 039 * <p/> 040 * This implementation will use types such as {@link SimpleExpression}, 041 * {@link XPathExpression} etc. if the given expression is detect as such a 042 * type. 043 * 044 * @param expression the expression 045 * @return a definition which describes the expression 046 */ 047 public static ExpressionDefinition toExpressionDefinition(Expression expression) { 048 if (expression instanceof SimpleBuilder) { 049 SimpleBuilder builder = (SimpleBuilder)expression; 050 // we keep the original expression by using the constructor that 051 // accepts an expression 052 SimpleExpression answer = new SimpleExpression(builder); 053 answer.setExpression(builder.getText()); 054 answer.setResultType(builder.getResultType()); 055 return answer; 056 } else if (expression instanceof ExpressionResultTypeAware && expression.getClass().getName().equals("org.apache.camel.language.xpath.XPathBuilder")) { 057 ExpressionResultTypeAware aware = (ExpressionResultTypeAware)expression; 058 // we keep the original expression by using the constructor that 059 // accepts an expression 060 XPathExpression answer = new XPathExpression(expression); 061 answer.setExpression(aware.getExpressionText()); 062 answer.setResultType(answer.getResultType()); 063 return answer; 064 } else if (expression instanceof ValueBuilder) { 065 // ValueBuilder wraps the actual expression so unwrap 066 ValueBuilder builder = (ValueBuilder)expression; 067 expression = builder.getExpression(); 068 } 069 070 if (expression instanceof ExpressionDefinition) { 071 return (ExpressionDefinition)expression; 072 } 073 return new ExpressionDefinition(expression); 074 } 075 076 /** 077 * Determines which {@link ExpressionDefinition} describes the given 078 * predicate best possible. 079 * <p/> 080 * This implementation will use types such as {@link SimpleExpression}, 081 * {@link XPathExpression} etc. if the given predicate is detect as such a 082 * type. 083 * 084 * @param predicate the predicate 085 * @return a definition which describes the predicate 086 */ 087 public static ExpressionDefinition toExpressionDefinition(Predicate predicate) { 088 if (predicate instanceof SimpleBuilder) { 089 SimpleBuilder builder = (SimpleBuilder)predicate; 090 // we keep the original expression by using the constructor that 091 // accepts an expression 092 SimpleExpression answer = new SimpleExpression(builder); 093 answer.setExpression(builder.getText()); 094 return answer; 095 } else if (predicate instanceof ExpressionResultTypeAware && predicate.getClass().getName().equals("org.apache.camel.language.xpath.XPathBuilder")) { 096 ExpressionResultTypeAware aware = (ExpressionResultTypeAware)predicate; 097 Expression expression = (Expression)predicate; 098 // we keep the original expression by using the constructor that 099 // accepts an expression 100 XPathExpression answer = new XPathExpression(expression); 101 answer.setExpression(aware.getExpressionText()); 102 answer.setResultType(answer.getResultType()); 103 return answer; 104 } else if (predicate instanceof ValueBuilder) { 105 // ValueBuilder wraps the actual predicate so unwrap 106 ValueBuilder builder = (ValueBuilder)predicate; 107 Expression expression = builder.getExpression(); 108 if (expression instanceof Predicate) { 109 predicate = (Predicate)expression; 110 } 111 } 112 113 if (predicate instanceof ExpressionDefinition) { 114 return (ExpressionDefinition)predicate; 115 } 116 return new ExpressionDefinition(predicate); 117 } 118}