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