Coverage Report - org.mule.tools.visualizer.processor.TagProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
TagProcessor
54%
49/90
47%
30/64
3.143
 
 1  
 /*
 2  
  * $Id: TagProcessor.java 11728 2008-05-13 07:31:11Z 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.processor;
 12  
 
 13  
 import org.mule.tools.visualizer.config.GraphEnvironment;
 14  
 import org.mule.tools.visualizer.util.MuleTag;
 15  
 import org.mule.util.StringUtils;
 16  
 
 17  
 import com.oy.shared.lm.graph.Graph;
 18  
 import com.oy.shared.lm.graph.GraphEdge;
 19  
 import com.oy.shared.lm.graph.GraphNode;
 20  
 
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.jdom.Attribute;
 25  
 import org.jdom.Element;
 26  
 
 27  
 public abstract class TagProcessor
 28  
 {
 29  
 
 30  
     // TODO - serious weirdness here, with a  static value set in a constructor...
 31  
     private static GraphEnvironment environment;
 32  
 
 33  
     public TagProcessor(GraphEnvironment env)
 34  76
     {
 35  76
         setEnvironment(env);
 36  76
     }
 37  
 
 38  
     public abstract void process(Graph graph, Element currentElement, GraphNode parent);
 39  
 
 40  
     public static void appendProperties(Element element, StringBuffer caption)
 41  
     {
 42  18
         Element properties = element.getChild(MuleTag.ELEMENT_PROPERTIES);
 43  18
         if (properties != null)
 44  
         {
 45  0
             for (Iterator iterator = properties.getChildren(MuleTag.ELEMENT_PROPERTY).iterator(); iterator
 46  0
                 .hasNext();)
 47  
             {
 48  0
                 Element property = (Element) iterator.next();
 49  0
                 caption.append(property.getAttributeValue(MuleTag.ATTRIBUTE_NAME) + " :"
 50  
                                 + lookupPropertyTemplate(property.getAttributeValue(MuleTag.ATTRIBUTE_VALUE))
 51  
                                 + "\n");
 52  0
             }
 53  
         }
 54  18
         for (Iterator iterator = element.getAttributes().iterator(); iterator.hasNext();)
 55  
         {
 56  50
             Attribute a = (Attribute) iterator.next();
 57  50
             if (!ignoreAttribute(a.getName()))
 58  
             {
 59  26
                 caption.append(a.getName() + " :" + a.getValue() + "\n");
 60  
             }
 61  50
         }
 62  18
     }
 63  
 
 64  
     protected static boolean ignoreAttribute(String name)
 65  
     {
 66  50
         if (name == null || "".equals(name))
 67  
         {
 68  0
             return true;
 69  
         }
 70  50
         for (Iterator iterator = getEnvironment().getConfig().getIgnoredAttributes().iterator(); iterator
 71  478
             .hasNext();)
 72  
         {
 73  452
             String s = (String) iterator.next();
 74  452
             if (name.equals(s))
 75  
             {
 76  24
                 return true;
 77  
             }
 78  
 
 79  428
         }
 80  26
         return false;
 81  
     }
 82  
 
 83  
     public static void appendDescription(Element e, StringBuffer caption)
 84  
     {
 85  22
         Element description = e.getChild(MuleTag.ELEMENT_DESCRIPTION);
 86  22
         if (description != null)
 87  
         {
 88  2
             caption.append("\n-------------------\n").append(description.getText()).append("\n");
 89  
         }
 90  22
     }
 91  
 
 92  
     protected void appendProfiles(Element descriptor, StringBuffer caption)
 93  
     {
 94  6
         List elements = descriptor.getChildren(MuleTag.ELEMENT_THREADING_PROFILE);
 95  6
         for (Iterator iterator = elements.iterator(); iterator.hasNext();)
 96  
         {
 97  0
             Element threadingProfile = (Element) iterator.next();
 98  0
             if (threadingProfile != null)
 99  
             {
 100  0
                 appendAttribute(threadingProfile, "maxBufferSize", caption);
 101  0
                 appendAttribute(threadingProfile, "threadTTL", caption);
 102  0
                 appendAttribute(threadingProfile, "maxThreadsActive", caption);
 103  0
                 appendAttribute(threadingProfile, "maxThreadsIdle", caption);
 104  0
                 appendAttribute(threadingProfile, "id", caption);
 105  
             }
 106  0
         }
 107  6
         Element poolingProfile = descriptor.getChild(MuleTag.ELEMENT_POOLING_PROFILE);
 108  6
         if (poolingProfile != null)
 109  
         {
 110  0
             appendAttribute(poolingProfile, "exhaustedAction", caption);
 111  0
             appendAttribute(poolingProfile, "maxActive", caption);
 112  0
             appendAttribute(poolingProfile, "maxIdle", caption);
 113  0
             appendAttribute(poolingProfile, "maxWait", caption);
 114  
         }
 115  
 
 116  6
         Element queueProfile = descriptor.getChild(MuleTag.ELEMENT_QUEUE_PROFILE);
 117  6
         if (queueProfile != null)
 118  
         {
 119  0
             appendAttribute(queueProfile, "maxOutstandingMessages", caption);
 120  0
             appendAttribute(queueProfile, "persistent", caption);
 121  
         }
 122  6
     }
 123  
 
 124  
     protected void appendAttribute(Element e, String name, StringBuffer caption)
 125  
     {
 126  0
         if (e.getAttribute(name) == null)
 127  
         {
 128  0
             return;
 129  
         }
 130  0
         String value = e.getAttributeValue(name);
 131  0
         if (value != null)
 132  
         {
 133  0
             caption.append(name + " = "
 134  
                             + (StringUtils.EMPTY.equals(value) ? "\"\"" : lookupPropertyTemplate(value))
 135  
                             + "\n");
 136  
         }
 137  0
     }
 138  
 
 139  
     public static void addEdge(Graph graph, GraphNode src, GraphNode dest, String caption, boolean twoway)
 140  
     {
 141  14
         GraphEdge ge = graph.addEdge(src, dest);
 142  14
         if (twoway)
 143  
         {
 144  0
             ge.getInfo().setArrowTailNormal();
 145  
         }
 146  
 
 147  14
         if (caption != null)
 148  
         {
 149  14
             if ("in".equalsIgnoreCase(caption) && twoway)
 150  
             {
 151  0
                 caption += " / out";
 152  
             }
 153  
             else
 154  
             {
 155  14
                 if ("out".equalsIgnoreCase(caption) && twoway)
 156  
                 {
 157  0
                     caption += " / in";
 158  
                 }
 159  
             }
 160  14
             ge.getInfo().setCaption(caption);
 161  
         }
 162  14
     }
 163  
 
 164  
     public static void addRelation(Graph graph, GraphNode src, GraphNode dest, String caption)
 165  
     {
 166  8
         if (null != src && null != dest)
 167  
         {
 168  8
             GraphEdge ge = graph.addEdge(src, dest);
 169  8
             ge.getInfo().setArrowHeadNone();
 170  8
             if (caption != null)
 171  
             {
 172  8
                 ge.getInfo().setCaption(caption);
 173  
             }
 174  8
         }
 175  
         else
 176  
         {
 177  0
             environment.log("Null relation: " + describeNode(src) + " / " + describeNode(dest)
 178  
                     + " (" + caption + ")");
 179  
         }
 180  8
     }
 181  
 
 182  
     private static String describeNode(GraphNode node)
 183  
     {
 184  0
         if (null == node)
 185  
         {
 186  0
             return "-";
 187  
         }
 188  
         else
 189  
         {
 190  0
             return node.getId() + ":" + node.getInfo().getCaption();
 191  
         }
 192  
     }
 193  
 
 194  
     public boolean isTwoWay(Element e)
 195  
     {
 196  14
         if (e == null)
 197  
         {
 198  0
             return getEnvironment().isDefaultTwoWay();
 199  
         }
 200  14
         return ("true".equalsIgnoreCase(e.getAttributeValue(MuleTag.ATTRIBUTE_SYNCHRONOUS)) || getEnvironment()
 201  
             .isDefaultTwoWay());
 202  
     }
 203  
 
 204  
     protected static String lookupPropertyTemplate(String template)
 205  
     {
 206  0
         if (template == null)
 207  
         {
 208  0
             return null;
 209  
         }
 210  0
         String value = getEnvironment().getProperties().getProperty(template, null);
 211  0
         if (value == null && template.startsWith("${"))
 212  
         {
 213  0
             value = getEnvironment().getProperties().getProperty(template.substring(2, template.length() - 1),
 214  
                 template);
 215  
         }
 216  
         else
 217  
         {
 218  0
             value = template;
 219  
         }
 220  0
         return value;
 221  
     }
 222  
 
 223  
     protected static void setEnvironment(GraphEnvironment environment)
 224  
     {
 225  76
         TagProcessor.environment = environment;
 226  76
     }
 227  
 
 228  
     public static GraphEnvironment getEnvironment()
 229  
     {
 230  158
         return environment;
 231  
     }
 232  
 }