View Javadoc

1   /*
2    * $Id: MuleParser.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tools.visualizer.components;
12  
13  import org.mule.tools.visualizer.config.GraphEnvironment;
14  import org.mule.tools.visualizer.postprocessors.ExternalSystemPostProcessor;
15  import org.mule.tools.visualizer.postprocessors.NodeHiderPostProcessor;
16  import org.mule.tools.visualizer.postprocessors.UrlAssignerPostProcessor;
17  import org.mule.tools.visualizer.processor.AgentProcessor;
18  import org.mule.tools.visualizer.processor.ConnectorProcessor;
19  import org.mule.tools.visualizer.processor.EndpointIdentifiersProcessor;
20  import org.mule.tools.visualizer.processor.EndpointsProcessor;
21  import org.mule.tools.visualizer.processor.MuleConfigProcessor;
22  import org.mule.tools.visualizer.processor.MuleModelProcessor;
23  import org.mule.tools.visualizer.processor.TagProcessor;
24  import org.mule.tools.visualizer.processor.TransformerProcessor;
25  
26  import com.oy.shared.lm.graph.Graph;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import org.jdom.Document;
36  import org.jdom.Element;
37  import org.jdom.JDOMException;
38  import org.jdom.input.SAXBuilder;
39  
40  public class MuleParser
41  {
42  
43      private final SAXBuilder builder;
44      private final List processors = new ArrayList();
45      private final List postProcessors = new ArrayList();
46      private GraphEnvironment env;
47  
48      public MuleParser(GraphEnvironment env, SAXBuilder builder)
49      {
50          this.env = env;
51          this.builder = builder;
52          processors.add(new MuleConfigProcessor(env));
53          processors.add(new EndpointIdentifiersProcessor(env));
54          processors.add(new EndpointsProcessor(env));
55          processors.add(new ConnectorProcessor(env));
56          processors.add(new MuleModelProcessor(env));
57          processors.add(new TransformerProcessor(env));
58          processors.add(new AgentProcessor(env));
59  
60          postProcessors.add(new UrlAssignerPostProcessor());
61          postProcessors.add(new ExternalSystemPostProcessor());
62          // Always set this to last as deleting nodes earlier seems to have adverse
63          // affects
64          // when adding nodes
65          postProcessors.add(new NodeHiderPostProcessor());
66  
67      }
68  
69      public void parseMuleConfig(InputStream in, Graph graph) throws JDOMException, IOException
70      {
71          Document doc = builder.build(in);
72          parseMuleConfig(doc, graph);
73      }
74  
75      public void parseMuleConfig(File myFile, Graph graph) throws JDOMException, IOException
76      {
77          Document doc = builder.build(myFile);
78          parseMuleConfig(doc, graph);
79      }
80  
81      public void parseMuleConfig(Document doc, Graph graph) throws JDOMException, IOException
82      {
83          Element root = doc.getRootElement();
84          String caption = root.getAttribute("id").getValue();
85          if (caption != null)
86          {
87              caption = caption.replaceAll("_", " ");
88          }
89          else
90          {
91              caption = "Mule Configuration";
92          }
93  
94          StringBuffer captionBuffer = new StringBuffer();
95          captionBuffer.append(caption);
96          TagProcessor.appendDescription(root, captionBuffer);
97          graph.getInfo().setCaption(captionBuffer.toString());
98  
99          for (Iterator iterator = processors.iterator(); iterator.hasNext();)
100         {
101             TagProcessor tagProcessor = (TagProcessor) iterator.next();
102             tagProcessor.process(graph, root, null);
103         }
104     }
105 
106     public void finalise(Graph graph)
107     {
108         for (Iterator iter = postProcessors.iterator(); iter.hasNext();)
109         {
110             PostProcessor postProcessor = (PostProcessor) iter.next();
111             postProcessor.postProcess(graph, env);
112         }
113     }
114 
115 }