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.Collections;
021    import java.util.List;
022    
023    import javax.xml.bind.annotation.XmlAccessType;
024    import javax.xml.bind.annotation.XmlAccessorType;
025    import javax.xml.bind.annotation.XmlElement;
026    import javax.xml.bind.annotation.XmlElementRef;
027    import javax.xml.bind.annotation.XmlRootElement;
028    
029    import org.apache.camel.Predicate;
030    import org.apache.camel.Processor;
031    import org.apache.camel.builder.ExpressionClause;
032    import org.apache.camel.processor.ChoiceProcessor;
033    import org.apache.camel.processor.FilterProcessor;
034    import org.apache.camel.spi.RouteContext;
035    import org.apache.camel.util.CollectionStringBuffer;
036    
037    /**
038     * Represents an XML <choice/> element
039     *
040     * @version $Revision: 41908 $
041     */
042    @XmlRootElement(name = "choice")
043    @XmlAccessorType(XmlAccessType.FIELD)
044    public class ChoiceType extends ProcessorType<ChoiceType> {
045        @XmlElementRef
046        private List<WhenType> whenClauses = new ArrayList<WhenType>();
047        @XmlElement(required = false)
048        private OtherwiseType otherwise;
049    
050        @Override
051        public String toString() {
052            if (getOtherwise() != null) {
053                return "Choice[ " + getWhenClauses() + " " + getOtherwise() + "]";
054            } else {
055                return "Choice[ " + getWhenClauses() + "]";
056    
057            }
058        }
059    
060        @Override
061        public Processor createProcessor(RouteContext routeContext) throws Exception {
062            List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
063            for (WhenType whenClaus : whenClauses) {
064                filters.add(whenClaus.createProcessor(routeContext));
065            }
066            Processor otherwiseProcessor = null;
067            if (otherwise != null) {
068                otherwiseProcessor = otherwise.createProcessor(routeContext);
069            }
070            return new ChoiceProcessor(filters, otherwiseProcessor);
071        }
072    
073        // Fluent API
074        // -------------------------------------------------------------------------
075        public ChoiceType when(Predicate predicate) {
076            getWhenClauses().add(new WhenType(predicate));
077            return this;
078        }
079    
080    
081        public ExpressionClause<ChoiceType> when() {
082            WhenType when = new WhenType();
083            getWhenClauses().add(when);
084            ExpressionClause<ChoiceType> clause = new ExpressionClause<ChoiceType>(this);
085            when.setExpression(clause);
086            return clause;
087        }
088    
089    
090        public ChoiceType otherwise() {
091            OtherwiseType answer = new OtherwiseType();
092            setOtherwise(answer);
093            return this;
094        }
095    
096        // Properties
097        // -------------------------------------------------------------------------
098    
099        @Override
100        public String getLabel() {
101            CollectionStringBuffer buffer = new CollectionStringBuffer();
102            List<WhenType> list = getWhenClauses();
103            for (WhenType whenType : list) {
104                buffer.append(whenType.getLabel());
105            }
106            return buffer.toString();
107        }
108    
109        public List<WhenType> getWhenClauses() {
110            return whenClauses;
111        }
112    
113        public void setWhenClauses(List<WhenType> whenClauses) {
114            this.whenClauses = whenClauses;
115        }
116    
117        public List<ProcessorType<?>> getOutputs() {
118            if (otherwise != null) {
119                return otherwise.getOutputs();
120            } else if (whenClauses.isEmpty()) {
121                return Collections.EMPTY_LIST;
122            } else {
123                WhenType when = whenClauses.get(whenClauses.size() - 1);
124                return when.getOutputs();
125            }
126        }
127    
128        public OtherwiseType getOtherwise() {
129            return otherwise;
130        }
131    
132        public void setOtherwise(OtherwiseType otherwise) {
133            this.otherwise = otherwise;
134        }
135    }