1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.graphviz; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.OutputStream; |
25 |
|
import java.lang.reflect.Type; |
26 |
|
import java.util.regex.Matcher; |
27 |
|
import java.util.regex.Pattern; |
28 |
|
|
29 |
|
import org.apache.commons.io.FileUtils; |
30 |
|
import org.apache.commons.io.IOUtils; |
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.slf4j.LoggerFactory; |
34 |
|
import org.xwiki.environment.Environment; |
35 |
|
|
36 |
|
import com.xpn.xwiki.XWiki; |
37 |
|
import com.xpn.xwiki.XWikiContext; |
38 |
|
import com.xpn.xwiki.api.Api; |
39 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
40 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
41 |
|
import com.xpn.xwiki.web.Utils; |
42 |
|
import com.xpn.xwiki.web.XWikiResponse; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@deprecated |
53 |
|
@version |
54 |
|
|
55 |
|
@Deprecated |
|
|
| 0% |
Uncovered Elements: 138 (138) |
Complexity: 38 |
Complexity Density: 0.41 |
|
56 |
|
public class GraphVizPlugin extends XWikiDefaultPlugin |
57 |
|
{ |
58 |
|
@link |
59 |
|
private static final Pattern HTML_ESCAPE_PATTERN = Pattern.compile("&#([0-9]++);"); |
60 |
|
|
61 |
|
|
62 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(com.xpn.xwiki.plugin.graphviz.GraphVizPlugin.class); |
63 |
|
|
64 |
|
|
65 |
|
private static final String DEFAULT_FORMAT = "png"; |
66 |
|
|
67 |
|
|
68 |
|
private static final String DOT_ENGINE = "dot"; |
69 |
|
|
70 |
|
|
71 |
|
private static final String NEATO_ENGINE = "neato"; |
72 |
|
|
73 |
|
|
74 |
|
private File tempDir; |
75 |
|
|
76 |
|
|
77 |
|
private String dotPath; |
78 |
|
|
79 |
|
|
80 |
|
private String neatoPath; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private Environment environment = Utils.getComponent((Type) Environment.class); |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@param |
91 |
|
@param |
92 |
|
@param |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
94 |
0 |
public GraphVizPlugin(String name, String className, XWikiContext context)... |
95 |
|
{ |
96 |
0 |
super(name, className, context); |
97 |
0 |
init(context); |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
0 |
@Override... |
101 |
|
public String getName() |
102 |
|
{ |
103 |
0 |
return "graphviz"; |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
108 |
|
{ |
109 |
0 |
return new GraphVizPluginApi((GraphVizPlugin) plugin, context); |
110 |
|
} |
111 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 2 |
Complexity Density: 1 |
|
112 |
0 |
@Override... |
113 |
|
public void flushCache() |
114 |
|
{ |
115 |
0 |
try { |
116 |
0 |
FileUtils.cleanDirectory(this.tempDir); |
117 |
|
} catch (Exception e) { |
118 |
|
|
119 |
|
} |
120 |
|
} |
121 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 8 |
Complexity Density: 0.44 |
|
122 |
0 |
@Override... |
123 |
|
public void init(XWikiContext context) |
124 |
|
{ |
125 |
0 |
super.init(context); |
126 |
|
|
127 |
0 |
File dir = this.environment.getTemporaryDirectory(); |
128 |
0 |
this.tempDir = new File(dir, this.getName()); |
129 |
0 |
try { |
130 |
0 |
this.tempDir.mkdirs(); |
131 |
|
} catch (Exception ex) { |
132 |
0 |
LOGGER.warn("Failed to create temporary file", ex); |
133 |
|
} |
134 |
|
|
135 |
0 |
this.dotPath = context.getWiki().Param("xwiki.plugin.graphviz.dotpath", DOT_ENGINE); |
136 |
0 |
if (!this.dotPath.equals(DOT_ENGINE)) { |
137 |
0 |
try { |
138 |
0 |
File dfile = new File(this.dotPath); |
139 |
0 |
if (!dfile.exists()) { |
140 |
0 |
LOGGER.error("Cannot find graphiz dot program at " + this.dotPath); |
141 |
|
} |
142 |
|
} catch (Exception e) { |
143 |
|
|
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
0 |
this.neatoPath = context.getWiki().Param("xwiki.plugin.graphviz.neatopath", NEATO_ENGINE); |
148 |
0 |
if (!this.neatoPath.equals(NEATO_ENGINE)) { |
149 |
0 |
try { |
150 |
0 |
File dfile = new File(this.neatoPath); |
151 |
0 |
if (!dfile.exists()) { |
152 |
0 |
LOGGER.error("Cannot find graphiz neato program at " + this.neatoPath); |
153 |
|
} |
154 |
|
} catch (Exception e) { |
155 |
|
|
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
@param |
165 |
|
@param |
166 |
|
@param |
167 |
|
@return |
168 |
|
@throws |
169 |
|
@see |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0 |
public String getDotImageURL(String content, boolean dot, XWikiContext context) throws IOException... |
172 |
|
{ |
173 |
0 |
return getDotResultURL(content, dot, DEFAULT_FORMAT, context); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@param |
181 |
|
@param |
182 |
|
@param |
183 |
|
@return |
184 |
|
@throws |
185 |
|
@see |
186 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
187 |
0 |
public String getDotResultURL(String content, boolean dot, String outputFormat, XWikiContext context)... |
188 |
|
throws IOException |
189 |
|
{ |
190 |
0 |
String filename = writeDotImage(content, outputFormat, dot); |
191 |
0 |
return context.getDoc().getAttachmentURL(filename, DOT_ENGINE, context); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param |
199 |
|
@return |
200 |
|
@throws |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
0 |
public byte[] getDotImage(String content, boolean dot) throws IOException... |
203 |
|
{ |
204 |
0 |
return getDotImage(content, DEFAULT_FORMAT, dot); |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
@param |
211 |
|
@param |
212 |
|
@param |
213 |
|
@return |
214 |
|
@throws |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
216 |
0 |
public byte[] getDotImage(String content, String extension, boolean dot) throws IOException... |
217 |
|
{ |
218 |
0 |
int hashCode = Math.abs(content.hashCode()); |
219 |
0 |
return getDotImage(hashCode, content, extension, dot); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@link |
225 |
|
|
226 |
|
@param |
227 |
|
@param |
228 |
|
@return |
229 |
|
@throws |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
231 |
0 |
public String writeDotImage(String content, boolean dot) throws IOException... |
232 |
|
{ |
233 |
0 |
return writeDotImage(content, DEFAULT_FORMAT, dot); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
@link |
239 |
|
|
240 |
|
@param |
241 |
|
@param |
242 |
|
@param |
243 |
|
@return |
244 |
|
@throws |
245 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
246 |
0 |
public String writeDotImage(String content, String extension, boolean dot) throws IOException... |
247 |
|
{ |
248 |
0 |
int hashCode = Math.abs(content.hashCode()); |
249 |
0 |
getDotImage(hashCode, content, extension, dot); |
250 |
0 |
String name = (dot ? DOT_ENGINE : NEATO_ENGINE) + '-'; |
251 |
0 |
return name + hashCode + "." + extension; |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
@param |
258 |
|
@param |
259 |
|
@param |
260 |
|
@throws |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
0 |
public void outputDotImage(String content, boolean dot, XWikiContext context) throws IOException... |
263 |
|
{ |
264 |
0 |
outputDotImage(content, DEFAULT_FORMAT, dot, context); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
@param |
271 |
|
@param |
272 |
|
@param |
273 |
|
@param |
274 |
|
@throws |
275 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
276 |
0 |
public void outputDotImage(String content, String extension, boolean dot, XWikiContext context) throws IOException... |
277 |
|
{ |
278 |
0 |
byte[] dotbytes = getDotImage(content, extension, dot); |
279 |
0 |
XWikiResponse response = context.getResponse(); |
280 |
0 |
context.setFinished(true); |
281 |
0 |
response.setContentLength(dotbytes.length); |
282 |
0 |
response.setContentType(context.getEngineContext().getMimeType("toto." + extension)); |
283 |
0 |
OutputStream os = response.getOutputStream(); |
284 |
0 |
os.write(dotbytes); |
285 |
0 |
os.flush(); |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
@param |
292 |
|
@link |
293 |
|
@param |
294 |
|
@throws |
295 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
296 |
0 |
public void outputDotImageFromFile(String filename, XWikiContext context) throws IOException... |
297 |
|
{ |
298 |
0 |
File ofile = getTempFile(filename); |
299 |
0 |
byte[] dotbytes = readDotImage(ofile); |
300 |
0 |
XWikiResponse response = context.getResponse(); |
301 |
0 |
context.setFinished(true); |
302 |
0 |
response.setDateHeader("Last-Modified", ofile.lastModified()); |
303 |
0 |
response.setContentLength(dotbytes.length); |
304 |
0 |
response.setContentType(context.getEngineContext().getMimeType(filename)); |
305 |
0 |
OutputStream os = response.getOutputStream(); |
306 |
0 |
os.write(dotbytes); |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
@param |
314 |
|
@param |
315 |
|
@param |
316 |
|
@param |
317 |
|
@return |
318 |
|
@throws |
319 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 6 |
Complexity Density: 0.24 |
|
320 |
0 |
private byte[] getDotImage(int hashCode, String content, String extension, boolean dot) throws IOException... |
321 |
|
{ |
322 |
0 |
File dfile = getTempFile(hashCode, "input.dot", dot); |
323 |
0 |
if (!dfile.exists()) { |
324 |
0 |
FileUtils.write(dfile, undoEscapeFilter(content), XWiki.DEFAULT_ENCODING); |
325 |
|
} |
326 |
|
|
327 |
0 |
File ofile = getTempFile(hashCode, extension, dot); |
328 |
0 |
if (!ofile.exists()) { |
329 |
0 |
Runtime rt = Runtime.getRuntime(); |
330 |
0 |
String[] command = new String[5]; |
331 |
0 |
command[0] = dot ? this.dotPath : this.neatoPath; |
332 |
0 |
command[1] = "-T" + extension; |
333 |
0 |
command[2] = dfile.getAbsolutePath(); |
334 |
0 |
command[3] = "-o"; |
335 |
0 |
command[4] = ofile.getAbsolutePath(); |
336 |
0 |
Process p = rt.exec(command); |
337 |
0 |
int exitValue = -1; |
338 |
0 |
final Thread thisThread = Thread.currentThread(); |
339 |
0 |
Thread t = new Thread(new Hangcheck(thisThread), "dot-hangcheck"); |
340 |
0 |
t.run(); |
341 |
0 |
try { |
342 |
0 |
exitValue = p.waitFor(); |
343 |
0 |
t.interrupt(); |
344 |
|
} catch (InterruptedException ex) { |
345 |
0 |
p.destroy(); |
346 |
0 |
LOGGER.error("Timeout while generating image from dot", ex); |
347 |
|
} |
348 |
|
|
349 |
0 |
if (exitValue != 0) { |
350 |
0 |
LOGGER.error("Error while generating image from dot: " |
351 |
|
+ IOUtils.toString(p.getErrorStream(), XWiki.DEFAULT_ENCODING)); |
352 |
|
} |
353 |
|
} |
354 |
0 |
return FileUtils.readFileToByteArray(ofile); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
@param |
361 |
|
@return |
362 |
|
@throws |
363 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
364 |
0 |
private byte[] readDotImage(File ofile) throws IOException... |
365 |
|
{ |
366 |
0 |
return FileUtils.readFileToByteArray(ofile); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@param |
373 |
|
@param |
374 |
|
@param |
375 |
|
@return |
376 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
377 |
0 |
private File getTempFile(int hashcode, String extension, boolean dot)... |
378 |
|
{ |
379 |
0 |
String name = (dot ? DOT_ENGINE : NEATO_ENGINE) + '-'; |
380 |
0 |
return getTempFile(name + hashcode + '.' + extension); |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
@param |
387 |
|
@return |
388 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
389 |
0 |
private File getTempFile(String filename)... |
390 |
|
{ |
391 |
0 |
return new File(this.tempDir, filename); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
@link |
398 |
|
|
399 |
|
@version |
400 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
401 |
|
private static class Hangcheck implements Runnable |
402 |
|
{ |
403 |
|
|
404 |
|
private Thread converterThread; |
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
@param |
410 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
411 |
0 |
Hangcheck(Thread converterThread)... |
412 |
|
{ |
413 |
0 |
this.converterThread = converterThread; |
414 |
|
} |
415 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
416 |
0 |
@Override... |
417 |
|
public void run() |
418 |
|
{ |
419 |
0 |
try { |
420 |
0 |
Thread.sleep(10000); |
421 |
0 |
this.converterThread.interrupt(); |
422 |
|
} catch (InterruptedException ex) { |
423 |
|
|
424 |
|
} |
425 |
|
} |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
|
@link |
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
@param |
435 |
|
|
436 |
|
@return |
437 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
438 |
0 |
private String undoEscapeFilter(String escapedContent)... |
439 |
|
{ |
440 |
0 |
if (StringUtils.isNotEmpty(escapedContent)) { |
441 |
0 |
Matcher matcher = HTML_ESCAPE_PATTERN.matcher(escapedContent); |
442 |
0 |
StringBuffer result = new StringBuffer(escapedContent.length()); |
443 |
0 |
while (matcher.find()) { |
444 |
0 |
int codepoint = Integer.valueOf(matcher.group(1)); |
445 |
0 |
if (codepoint >= 65 && codepoint <= 122) { |
446 |
0 |
matcher.appendReplacement(result, new String(new int[] { 92, 92, codepoint }, 0, 3)); |
447 |
|
} else { |
448 |
0 |
matcher.appendReplacement(result, "$0"); |
449 |
|
} |
450 |
|
} |
451 |
0 |
matcher.appendTail(result); |
452 |
0 |
return result.toString(); |
453 |
|
} else { |
454 |
0 |
return ""; |
455 |
|
} |
456 |
|
} |
457 |
|
} |