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: 784038 $
041 */
042 @XmlRootElement(name = "choice")
043 @XmlAccessorType(XmlAccessType.FIELD)
044 public class ChoiceDefinition extends ProcessorDefinition<ChoiceDefinition> {
045
046 @XmlElementRef
047 private List<WhenDefinition> whenClauses = new ArrayList<WhenDefinition>();
048 @XmlElement(required = false)
049 private OtherwiseDefinition otherwise;
050
051 @Override
052 public String toString() {
053 if (getOtherwise() != null) {
054 return "Choice[" + getWhenClauses() + " " + getOtherwise() + "]";
055 } else {
056 return "Choice[" + getWhenClauses() + "]";
057
058 }
059 }
060 @Override
061 public String getShortName() {
062 return "choice";
063 }
064
065 @Override
066 public Processor createProcessor(RouteContext routeContext) throws Exception {
067 List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
068 for (WhenDefinition whenClaus : whenClauses) {
069 filters.add(whenClaus.createProcessor(routeContext));
070 }
071 Processor otherwiseProcessor = null;
072 if (otherwise != null) {
073 otherwiseProcessor = otherwise.createProcessor(routeContext);
074 }
075 return new ChoiceProcessor(filters, otherwiseProcessor);
076 }
077
078 // Fluent API
079 // -------------------------------------------------------------------------
080 /**
081 * Sets the predicate for the when node
082 *
083 * @param predicate the predicate
084 * @return the builder
085 */
086 public ChoiceDefinition when(Predicate predicate) {
087 getWhenClauses().add(new WhenDefinition(predicate));
088 return this;
089 }
090
091 /**
092 * Creates an expression for the when node
093 *
094 * @return expression to be used as builder to configure the when node
095 */
096 public ExpressionClause<ChoiceDefinition> when() {
097 WhenDefinition when = new WhenDefinition();
098 getWhenClauses().add(when);
099 ExpressionClause<ChoiceDefinition> clause = new ExpressionClause<ChoiceDefinition>(this);
100 when.setExpression(clause);
101 return clause;
102 }
103
104 /**
105 * Sets the otherwise node
106 *
107 * @return the builder
108 */
109 public ChoiceDefinition otherwise() {
110 OtherwiseDefinition answer = new OtherwiseDefinition();
111 setOtherwise(answer);
112 return this;
113 }
114
115 // Properties
116 // -------------------------------------------------------------------------
117
118 @Override
119 public String getLabel() {
120 CollectionStringBuffer buffer = new CollectionStringBuffer();
121 List<WhenDefinition> list = getWhenClauses();
122 for (WhenDefinition whenType : list) {
123 buffer.append(whenType.getLabel());
124 }
125 return buffer.toString();
126 }
127
128 public List<WhenDefinition> getWhenClauses() {
129 return whenClauses;
130 }
131
132 public void setWhenClauses(List<WhenDefinition> whenClauses) {
133 this.whenClauses = whenClauses;
134 }
135
136 @SuppressWarnings("unchecked")
137 public List<ProcessorDefinition> getOutputs() {
138 if (otherwise != null) {
139 return otherwise.getOutputs();
140 } else if (whenClauses.isEmpty()) {
141 return Collections.EMPTY_LIST;
142 } else {
143 WhenDefinition when = whenClauses.get(whenClauses.size() - 1);
144 return when.getOutputs();
145 }
146 }
147
148 public OtherwiseDefinition getOtherwise() {
149 return otherwise;
150 }
151
152 public void setOtherwise(OtherwiseDefinition otherwise) {
153 this.otherwise = otherwise;
154 }
155
156 }