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.view;
018
019 import java.util.ArrayList;
020 import java.util.List;
021 import java.util.Locale;
022
023 import org.apache.camel.model.AggregateDefinition;
024 import org.apache.camel.model.BeanDefinition;
025 import org.apache.camel.model.ChoiceDefinition;
026 import org.apache.camel.model.FilterDefinition;
027 import org.apache.camel.model.FromDefinition;
028 import org.apache.camel.model.OtherwiseDefinition;
029 import org.apache.camel.model.ProcessorDefinition;
030 import org.apache.camel.model.RecipientListDefinition;
031 import org.apache.camel.model.ResequenceDefinition;
032 import org.apache.camel.model.RoutingSlipDefinition;
033 import org.apache.camel.model.SplitDefinition;
034 import org.apache.camel.model.ToDefinition;
035 import org.apache.camel.model.WhenDefinition;
036
037 import static org.apache.camel.util.ObjectHelper.isEmpty;
038 import static org.apache.camel.util.ObjectHelper.isNotEmpty;
039
040 /**
041 * Represents a node in the EIP diagram tree
042 *
043 * @version
044 */
045 public class NodeData {
046 public String id;
047 public String image;
048 public String label;
049 public String shape;
050 public String edgeLabel;
051 public String tooltop;
052 public String nodeType;
053 public boolean nodeWritten;
054 public String url;
055 public List<ProcessorDefinition<?>> outputs;
056 public String association = "property";
057
058 public NodeData(String id, Object node, String imagePrefix) {
059 this.id = id;
060
061 if (node instanceof ProcessorDefinition) {
062 ProcessorDefinition<?> processorType = (ProcessorDefinition<?>)node;
063 this.edgeLabel = processorType.getLabel();
064 }
065 if (node instanceof FromDefinition) {
066 FromDefinition fromType = (FromDefinition)node;
067 this.tooltop = fromType.getLabel();
068 this.label = removeQueryString(this.tooltop);
069 this.url = "http://camel.apache.org/message-endpoint.html";
070 } else if (node instanceof ToDefinition) {
071 ToDefinition toType = (ToDefinition)node;
072 this.tooltop = toType.getLabel();
073 this.label = removeQueryString(this.tooltop);
074 this.edgeLabel = "";
075 this.url = "http://camel.apache.org/message-endpoint.html";
076 } else if (node instanceof FilterDefinition) {
077 this.image = imagePrefix + "MessageFilterIcon.png";
078 this.label = "Filter";
079 this.nodeType = "Message Filter";
080 } else if (node instanceof WhenDefinition) {
081 this.image = imagePrefix + "MessageFilterIcon.png";
082 this.nodeType = "When Filter";
083 this.label = "When";
084 this.url = "http://camel.apache.org/content-based-router.html";
085 } else if (node instanceof OtherwiseDefinition) {
086 this.nodeType = "Otherwise";
087 this.edgeLabel = "";
088 this.url = "http://camel.apache.org/content-based-router.html";
089 this.tooltop = "Otherwise";
090 } else if (node instanceof ChoiceDefinition) {
091 this.image = imagePrefix + "ContentBasedRouterIcon.png";
092 this.nodeType = "Content Based Router";
093 this.label = "Choice";
094 this.edgeLabel = "";
095
096 ChoiceDefinition choice = (ChoiceDefinition)node;
097 List<ProcessorDefinition<?>> outputs = new ArrayList<ProcessorDefinition<?>>(choice.getWhenClauses());
098 if (choice.getOtherwise() != null) {
099 outputs.add(choice.getOtherwise());
100 }
101 this.outputs = outputs;
102 } else if (node instanceof RecipientListDefinition) {
103 this.image = imagePrefix + "RecipientListIcon.png";
104 this.nodeType = "Recipient List";
105 } else if (node instanceof RoutingSlipDefinition) {
106 this.image = imagePrefix + "RoutingTableIcon.png";
107 this.nodeType = "Routing Slip";
108 this.url = "http://camel.apache.org/routing-slip.html";
109 } else if (node instanceof SplitDefinition) {
110 this.image = imagePrefix + "SplitterIcon.png";
111 this.nodeType = "Splitter";
112 } else if (node instanceof AggregateDefinition) {
113 this.image = imagePrefix + "AggregatorIcon.png";
114 this.nodeType = "Aggregator";
115 } else if (node instanceof ResequenceDefinition) {
116 this.image = imagePrefix + "ResequencerIcon.png";
117 this.nodeType = "Resequencer";
118 } else if (node instanceof BeanDefinition) {
119 BeanDefinition beanRef = (BeanDefinition) node;
120 this.nodeType = "Bean Ref";
121 this.label = beanRef.getLabel() + " Bean";
122 this.shape = "box";
123 }
124
125 // lets auto-default as many values as we can
126 if (isEmpty(this.nodeType) && node != null) {
127 String name = node.getClass().getName();
128 int idx = name.lastIndexOf('.');
129 if (idx > 0) {
130 name = name.substring(idx + 1);
131 }
132 if (name.endsWith("Type")) {
133 name = name.substring(0, name.length() - 4);
134 }
135 this.nodeType = insertSpacesBetweenCamelCase(name);
136 }
137 if (this.label == null) {
138 if (isEmpty(this.image)) {
139 this.label = this.nodeType;
140 this.shape = "box";
141 } else if (isNotEmpty(this.edgeLabel)) {
142 this.label = "";
143 } else {
144 this.label = node.toString();
145 }
146 }
147 if (isEmpty(this.tooltop)) {
148 if (isNotEmpty(this.nodeType)) {
149 String description = isNotEmpty(this.edgeLabel) ? this.edgeLabel : this.label;
150 this.tooltop = this.nodeType + ": " + description;
151 } else {
152 this.tooltop = this.label;
153 }
154 }
155 if (isEmpty(this.url) && isNotEmpty(this.nodeType)) {
156 this.url = "http://camel.apache.org/" + this.nodeType.toLowerCase(Locale.ENGLISH).replace(' ', '-') + ".html";
157 }
158 if (node instanceof ProcessorDefinition && this.outputs == null) {
159 ProcessorDefinition<?> processorType = (ProcessorDefinition<?>)node;
160 this.outputs = processorType.getOutputs();
161 }
162 }
163
164 protected String removeQueryString(String text) {
165 int idx = text.indexOf('?');
166 if (idx <= 0) {
167 return text;
168 } else {
169 return text.substring(0, idx);
170 }
171 }
172
173 /**
174 * Inserts a space before each upper case letter after a lowercase
175 */
176 public static String insertSpacesBetweenCamelCase(String name) {
177 boolean lastCharacterLowerCase = false;
178 StringBuilder buffer = new StringBuilder();
179 int i = 0;
180 for (int size = name.length(); i < size; i++) {
181 char ch = name.charAt(i);
182 if (Character.isUpperCase(ch)) {
183 if (lastCharacterLowerCase) {
184 buffer.append(' ');
185 }
186 lastCharacterLowerCase = false;
187 } else {
188 lastCharacterLowerCase = true;
189 }
190 buffer.append(ch);
191 }
192 return buffer.toString();
193 }
194 }