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 */
017package org.apache.camel.view;
018
019import java.io.PrintWriter;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.camel.model.FromDefinition;
025import org.apache.camel.model.MulticastDefinition;
026import org.apache.camel.model.ProcessorDefinition;
027import org.apache.camel.model.RouteDefinition;
028import static org.apache.camel.util.ObjectHelper.isEmpty;
029import static org.apache.camel.util.StringHelper.xmlEncode;
030
031/**
032 * @version 
033 */
034@Deprecated
035public class XmlGraphGenerator extends GraphGeneratorSupport {
036    private boolean addUrl = true;
037
038    public XmlGraphGenerator(String dir) {
039        super(dir, ".xml");
040    }
041
042    protected void generateFile(PrintWriter writer, Map<String, List<RouteDefinition>> map) {
043        writer.println("<?xml version='1.0' encoding='UTF-8'?>");
044        writer.println("<Graph>");
045        writer.println();
046
047        if (map.size() > 0) {
048            writer.println("<Node id='root' name='Camel Routes' description='Collection of Camel Routes' nodeType='root'/>");
049        }
050        printRoutes(writer, map);
051
052        writer.println();
053        writer.println("</Graph>");
054    }
055
056    protected void printRoutes(PrintWriter writer, Map<String, List<RouteDefinition>> map) {
057        Set<Map.Entry<String, List<RouteDefinition>>> entries = map.entrySet();
058        for (Map.Entry<String, List<RouteDefinition>> entry : entries) {
059            String group = entry.getKey();
060            printRoutes(writer, group, entry.getValue());
061        }
062    }
063
064    protected void printRoutes(PrintWriter writer, String group, List<RouteDefinition> routes) {
065        group = xmlEncode(group);
066        if (group != null) {
067            int idx = group.lastIndexOf('.');
068            String name = group;
069            if (idx > 0 && idx < group.length() - 1) {
070                name = group.substring(idx + 1);
071            }
072            writer.println("<Node id='" + group + "' name='" + name + "' description='" + group + "' nodeType='group'/>");
073            writer.println("<Edge fromID='root' toID='" + group + "'/>");
074        }
075        for (RouteDefinition route : routes) {
076            List<FromDefinition> inputs = route.getInputs();
077            boolean first = true;
078            for (FromDefinition input : inputs) {
079                NodeData nodeData = getNodeData(input);
080                if (first) {
081                    first = false;
082                    if (group != null) {
083                        writer.println("<Edge fromID='" + group + "' toID='" + xmlEncode(nodeData.id) + "'/>");
084                    }
085                }
086                printRoute(writer, route, nodeData);
087            }
088            writer.println();
089        }
090    }
091
092    protected void printRoute(PrintWriter writer, final RouteDefinition route, NodeData nodeData) {
093        printNode(writer, nodeData);
094
095        NodeData from = nodeData;
096        for (ProcessorDefinition<?> output : route.getOutputs()) {
097            NodeData newData = printNode(writer, from, output);
098            from = newData;
099        }
100    }
101
102    protected NodeData printNode(PrintWriter writer, NodeData fromData, ProcessorDefinition<?> node) {
103        if (node instanceof MulticastDefinition) {
104            // no need for a multicast node
105            List<ProcessorDefinition<?>> outputs = node.getOutputs();
106            for (ProcessorDefinition<?> output : outputs) {
107                printNode(writer, fromData, output);
108            }
109            return fromData;
110        }
111        NodeData toData = getNodeData(node);
112
113        printNode(writer, toData);
114
115        if (fromData != null) {
116            writer.print("<Edge fromID=\"");
117            writer.print(xmlEncode(fromData.id));
118            writer.print("\" toID=\"");
119            writer.print(xmlEncode(toData.id));
120            String association = toData.edgeLabel;
121            if (isEmpty(association)) {
122                writer.print("\" association=\"");
123                writer.print(xmlEncode(association));
124            }
125            writer.println("\"/>");
126        }
127
128        // now lets write any children
129        List<ProcessorDefinition<?>> outputs = toData.outputs;
130        if (outputs != null) {
131            for (ProcessorDefinition<?> output : outputs) {
132                NodeData newData = printNode(writer, toData, output);
133                if (!isMulticastNode(node)) {
134                    toData = newData;
135                }
136            }
137        }
138        return toData;
139    }
140
141    protected void printNode(PrintWriter writer, NodeData data) {
142        if (!data.nodeWritten) {
143            data.nodeWritten = true;
144
145            writer.println();
146            writer.print("<Node id=\"");
147            writer.print(xmlEncode(data.id));
148            writer.print("\" name=\"");
149            String name = data.label;
150            if (isEmpty(name)) {
151                name = data.tooltop;
152            }
153            writer.print(xmlEncode(name));
154            writer.print("\" nodeType=\"");
155            String nodeType = data.image;
156            if (isEmpty(nodeType)) {
157                nodeType = data.shape;
158                if (isEmpty(nodeType)) {
159                    nodeType = "node";
160                }
161            }
162            writer.print(xmlEncode(nodeType));
163            writer.print("\" description=\"");
164            writer.print(xmlEncode(data.tooltop));
165            if (addUrl) {
166                writer.print("\" url=\"");
167                writer.print(xmlEncode(data.url));
168            }
169            writer.println("\"/>");
170        }
171    }
172
173}