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     */
017    package org.apache.camel.model;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlElementRef;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.Expression;
026    import org.apache.camel.Predicate;
027    import org.apache.camel.model.language.ExpressionDefinition;
028    import org.apache.camel.spi.RouteContext;
029    
030    /**
031     * Represents an expression sub element
032     */
033    @XmlRootElement(name = "expression")
034    @XmlAccessorType(XmlAccessType.FIELD)
035    public class ExpressionSubElementDefinition {
036        @XmlElementRef
037        private ExpressionDefinition expressionType;
038        @XmlTransient
039        private Expression expression;
040        @XmlTransient
041        private Predicate predicate;
042    
043        public ExpressionSubElementDefinition() {
044        }
045    
046        public ExpressionSubElementDefinition(Expression expression) {
047            this.expression = expression;
048        }
049    
050        public ExpressionSubElementDefinition(Predicate predicate) {
051            this.predicate = predicate;
052        }   
053        
054        public ExpressionDefinition getExpressionType() {
055            return expressionType;
056        }
057    
058        public void setExpressionType(ExpressionDefinition expressionType) {
059            this.expressionType = expressionType;
060        }
061    
062        public Expression getExpression() {
063            return expression;
064        }   
065        
066        public void setExpression(Expression expression) {
067            this.expression = expression;
068        }
069    
070        public void setPredicate(Predicate predicate) {
071            this.predicate = predicate;
072        }
073    
074        public Predicate getPredicate() {
075            return predicate;
076        }    
077        
078        public Expression createExpression(RouteContext routeContext) {
079            ExpressionDefinition expressionType = getExpressionType();
080            if (expressionType != null && expression == null) {
081                expression = expressionType.createExpression(routeContext);
082            }
083            return expression;
084        }
085        
086        public Predicate createPredicate(RouteContext routeContext) {
087            ExpressionDefinition expressionType = getExpressionType();
088            if (expressionType != null && getPredicate() == null) {
089                setPredicate(expressionType.createPredicate(routeContext));
090            }
091            return getPredicate();
092        }
093    
094        @Override
095        public String toString() {
096            if (expression != null) {
097                return expression.toString();
098            } else if (expressionType != null) {
099                return expressionType.toString();
100            } else if (predicate != null) {
101                return predicate.toString();
102            }
103            return super.toString();
104        }
105    }
106