View Javadoc

1   /*
2    * $Id: UrlAssignerPostProcessor.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.postprocessors;
12  
13  import org.mule.tools.visualizer.components.PostProcessor;
14  import org.mule.tools.visualizer.config.GraphConfig;
15  import org.mule.tools.visualizer.config.GraphEnvironment;
16  import org.mule.util.StringUtils;
17  
18  import com.oy.shared.lm.graph.Graph;
19  import com.oy.shared.lm.graph.GraphNode;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.ToStringBuilder;
28  import org.apache.log4j.Logger;
29  import org.springframework.util.AntPathMatcher;
30  
31  public class UrlAssignerPostProcessor implements PostProcessor
32  {
33  
34      private static Logger log = Logger.getLogger(UrlAssignerPostProcessor.class);
35  
36      private class Pattern implements Comparable
37      {
38          private String pattern;
39  
40          private String url;
41  
42          private AntPathMatcher matcher = new AntPathMatcher();
43  
44          Pattern(String pattern, String url)
45          {
46              this.pattern = pattern;
47              this.url = url;
48          }
49  
50          public boolean match(String className)
51          {
52              return matcher.match(this.pattern, className);
53          }
54  
55          public boolean equals(Object obj)
56          {
57              if (!(obj instanceof Pattern))
58              {
59                  return false;
60              }
61              if (this == obj)
62              {
63                  return true;
64              }
65              Pattern rhs = (Pattern) obj;
66              return new EqualsBuilder().appendSuper(super.equals(obj)).append(pattern, rhs.pattern).isEquals();
67          }
68          
69          public int hashCode()
70          {
71              return (null == pattern ? 0 : pattern.hashCode()) ^ (null == url ? 0 : url.hashCode());
72          }
73  
74          public String toString()
75          {
76              return ToStringBuilder.reflectionToString(this) + "\n";
77          }
78  
79          public int compareTo(Object arg)
80          {
81              Pattern rhs = (Pattern) arg;
82              return -this.pattern.compareTo(rhs.pattern);
83  
84          }
85      }
86  
87      private List packagePatterns = null;
88  
89      public void postProcess(Graph graph, GraphEnvironment env)
90      {
91  
92          initPatterns(env.getConfig());
93  
94          GraphNode[] nodes = graph.getNodes();
95          for (int i = 0; i < nodes.length; i++)
96          {
97              GraphNode node = nodes[i];
98              String caption = node.getInfo().getCaption();
99              String header = node.getInfo().getHeader();
100             String className;
101 
102             className = extractFullClassName(caption, header);
103             String url = getUrlPatternForClass(className);
104             String classNameUrl = className.replaceAll("\\.", "/");
105 
106             url = StringUtils.replace(url, "${classname}", classNameUrl);
107             url = StringUtils.replace(url, "${header}", header);
108 
109             node.getInfo().setAttributes(node.getInfo().getAttributes() + "\n URL=\"" + url + "\" ");
110         }
111     }
112 
113     private void initPatterns(GraphConfig config)
114     {
115 
116         if (packagePatterns == null)
117         {
118             packagePatterns = new ArrayList();
119             for (Iterator iter = config.getUrls().keySet().iterator(); iter.hasNext();)
120             {
121                 String pattern = (String) iter.next();
122                 String url = config.getUrls().getProperty(pattern);
123                 packagePatterns.add(new Pattern(pattern, url));
124             }
125             Collections.sort(packagePatterns);
126             log.info("patterns : " + packagePatterns);
127         }
128 
129     }
130 
131     private String getUrlPatternForClass(String className)
132     {
133 
134         for (Iterator iter = packagePatterns.iterator(); iter.hasNext();)
135         {
136             Pattern element = (Pattern) iter.next();
137             if (element.match(className))
138             {
139                 log.info(className + " match pattern " + element);
140                 return element.url;
141             }
142         }
143 
144         return "http://mule.codehaus.org/docs/apidocs/${classname}.html";
145     }
146 
147     private String extractFullClassName(String caption, String header)
148     {
149         String className;
150         className = getAttribute(caption, "className");
151         if (className == null)
152         {
153             className = getAttribute(caption, "implementation");
154         }
155         if (className == null)
156         {
157             className = header;
158         }
159         if (className == null)
160         {
161             className = "";
162         }
163 
164         return className;
165     }
166 
167     private String getAttribute(String caption, String attrib)
168     {
169         String result = null;
170         String toSearch = attrib + " :";
171         int index = caption.indexOf(toSearch);
172         if (index != -1)
173         {
174             String sub = caption.substring(index + toSearch.length());
175             int indexEnd = sub.indexOf("\n");
176             result = sub.substring(0, indexEnd);
177         }
178         return result;
179     }
180 
181 }