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 java.util.Collections; 020import java.util.List; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlElementRef; 025import javax.xml.bind.annotation.XmlTransient; 026 027import org.apache.camel.Expression; 028import org.apache.camel.ExpressionFactory; 029import org.apache.camel.Predicate; 030import org.apache.camel.builder.ExpressionClause; 031import org.apache.camel.model.language.ExpressionDefinition; 032 033/** 034 * A base {@link ExpressionNode} which does <b>not</b> support any outputs. 035 * <p/> 036 * This node is to be extended by definitions which need to support an 037 * expression but the definition should not contain any outputs, such as 038 * {@link org.apache.camel.model.TransformDefinition}. 039 */ 040@XmlAccessorType(XmlAccessType.FIELD) 041@XmlTransient 042public abstract class ExpressionNode extends ProcessorDefinition<ExpressionNode> { 043 044 @XmlElementRef 045 private ExpressionDefinition expression; 046 047 public ExpressionNode() { 048 } 049 050 public ExpressionNode(ExpressionDefinition expression) { 051 this.expression = expression; 052 } 053 054 public ExpressionNode(Expression expression) { 055 if (expression != null) { 056 setExpression(ExpressionNodeHelper.toExpressionDefinition(expression)); 057 } 058 } 059 060 public ExpressionNode(Predicate predicate) { 061 if (predicate != null) { 062 setExpression(ExpressionNodeHelper.toExpressionDefinition(predicate)); 063 } 064 } 065 066 public ExpressionDefinition getExpression() { 067 return expression; 068 } 069 070 public void setExpression(Expression expression) { 071 setExpression(new ExpressionDefinition(expression)); 072 } 073 074 public void setExpression(ExpressionDefinition expression) { 075 // favour using the helper to set the expression as it can unwrap some 076 // unwanted builders when using Java DSL 077 this.expression = expression; 078 } 079 080 @Override 081 public String getLabel() { 082 if (getExpression() == null) { 083 return ""; 084 } 085 return getExpression().getLabel(); 086 } 087 088 @Override 089 public void configureChild(ProcessorDefinition<?> output) { 090 // reuse the logic from pre create processor 091 preCreateProcessor(); 092 } 093 094 @Override 095 public void preCreateProcessor() { 096 Expression exp = getExpression(); 097 if (getExpression() != null && getExpression().getExpressionValue() != null) { 098 exp = getExpression().getExpressionValue(); 099 } 100 101 if (exp instanceof ExpressionClause) { 102 ExpressionClause<?> clause = (ExpressionClause<?>)exp; 103 if (clause.getExpressionType() != null) { 104 // if using the Java DSL then the expression may have been set 105 // using the 106 // ExpressionClause which is a fancy builder to define 107 // expressions and predicates 108 // using fluent builders in the DSL. However we need afterwards 109 // a callback to 110 // reset the expression to the expression type the 111 // ExpressionClause did build for us 112 ExpressionFactory model = clause.getExpressionType(); 113 if (model instanceof ExpressionDefinition) { 114 setExpression((ExpressionDefinition)model); 115 } 116 } 117 } 118 119 if (getExpression() != null && getExpression().getExpression() == null) { 120 // use toString from predicate or expression so we have some 121 // information to show in the route model 122 if (getExpression().getPredicate() != null) { 123 getExpression().setExpression(getExpression().getPredicate().toString()); 124 } else if (getExpression().getExpressionValue() != null) { 125 getExpression().setExpression(getExpression().getExpressionValue().toString()); 126 } 127 } 128 } 129 130 @Override 131 public List<ProcessorDefinition<?>> getOutputs() { 132 return Collections.emptyList(); 133 } 134 135 @Override 136 public ExpressionNode id(String id) { 137 if (!(this instanceof OutputNode)) { 138 // let parent handle assigning the id, as we do not support outputs 139 getParent().id(id); 140 return this; 141 } else { 142 return super.id(id); 143 } 144 } 145 146}