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 java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlElementRef;
025    import javax.xml.bind.annotation.XmlTransient;
026    
027    import org.apache.camel.Expression;
028    import org.apache.camel.Predicate;
029    import org.apache.camel.Processor;
030    import org.apache.camel.impl.RouteContext;
031    import org.apache.camel.model.language.ExpressionType;
032    import org.apache.camel.processor.FilterProcessor;
033    
034    /**
035     * A base class for nodes which contain an expression and a number of outputs
036     *
037     * @version $Revision: 35332 $
038     */
039    @XmlAccessorType(XmlAccessType.FIELD)
040    public class ExpressionNode extends ProcessorType<ProcessorType> {
041        @XmlElementRef
042        private ExpressionType expression;
043        @XmlElementRef
044        private List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
045    
046        public ExpressionNode() {
047        }
048    
049        public ExpressionNode(ExpressionType expression) {
050            this.expression = expression;
051        }
052    
053        public ExpressionNode(Expression expression) {
054            if (expression != null) {
055                setExpression(new ExpressionType(expression));
056            }
057        }
058    
059        public ExpressionNode(Predicate predicate) {
060            if (predicate != null) {
061                setExpression(new ExpressionType(predicate));
062            }
063        }
064    
065        public ExpressionType getExpression() {
066            return expression;
067        }
068    
069        public void setExpression(ExpressionType expression) {
070            this.expression = expression;
071        }
072    
073        public List<ProcessorType<?>> getOutputs() {
074            return outputs;
075        }
076    
077        public void setOutputs(List<ProcessorType<?>> outputs) {
078            this.outputs = outputs;
079        }
080    
081        @Override
082        public String getLabel() {
083            if (getExpression() == null) {
084                return "";
085            }
086            return getExpression().getLabel();
087        }
088    
089        protected FilterProcessor createFilterProcessor(RouteContext routeContext) throws Exception {
090            Processor childProcessor = routeContext.createProcessor(this);
091            return new FilterProcessor(getExpression().createPredicate(routeContext), childProcessor);
092        }
093    
094        @Override
095        protected void configureChild(ProcessorType output) {
096            super.configureChild(output);
097            if (isInheritErrorHandler()) {
098                output.setErrorHandlerBuilder(getErrorHandlerBuilder());
099            }
100        }
101    }