1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web.sx; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.io.StringWriter; |
25 |
|
import java.text.MessageFormat; |
26 |
|
|
27 |
|
import org.slf4j.Logger; |
28 |
|
import org.slf4j.LoggerFactory; |
29 |
|
|
30 |
|
import com.yahoo.platform.yui.compressor.JavaScriptCompressor; |
31 |
|
import com.yahoo.platform.yui.compressor.javascript.ErrorReporter; |
32 |
|
import com.yahoo.platform.yui.compressor.javascript.EvaluatorException; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
40 |
|
public class JsExtension implements Extension |
41 |
|
{ |
42 |
|
|
43 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(JsExtension.class); |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
45 |
759 |
@Override... |
46 |
|
public String getClassName() |
47 |
|
{ |
48 |
759 |
return "XWiki.JavaScriptExtension"; |
49 |
|
} |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
51 |
252 |
@Override... |
52 |
|
public String getContentType() |
53 |
|
{ |
54 |
252 |
return "text/javascript; charset=UTF-8"; |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
252 |
@Override... |
58 |
|
public SxCompressor getCompressor() |
59 |
|
{ |
60 |
252 |
return new JsCompressor(); |
61 |
|
} |
62 |
|
|
63 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 4 |
Complexity Density: 0.4 |
|
64 |
|
private static class JsCompressor implements SxCompressor |
65 |
|
{ |
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 4 |
Complexity Density: 0.4 |
|
66 |
252 |
@Override... |
67 |
|
public String compress(String source) |
68 |
|
{ |
69 |
252 |
try { |
70 |
252 |
ErrorReporter reporter = new CustomErrorReporter(); |
71 |
252 |
JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(source), reporter); |
72 |
252 |
StringWriter out = new StringWriter(); |
73 |
252 |
compressor.compress(out, -1, true, false, false, false); |
74 |
252 |
return out.toString(); |
75 |
|
} catch (IOException ex) { |
76 |
0 |
LOGGER.info("Failed to write the compressed output: " + ex.getMessage()); |
77 |
|
} catch (EvaluatorException ex) { |
78 |
0 |
LOGGER.info("Failed to parse the JS extension: " + ex.getMessage()); |
79 |
|
} catch (Exception ex) { |
80 |
0 |
LOGGER.warn("Failed to compress JS extension: " + ex.getMessage()); |
81 |
|
} |
82 |
0 |
return source; |
83 |
|
} |
84 |
|
|
85 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
86 |
|
private static class CustomErrorReporter implements ErrorReporter |
87 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
0 |
@Override... |
89 |
|
public void error(String message, String filename, int lineNumber, String context, int column) |
90 |
|
{ |
91 |
0 |
LOGGER.warn(MessageFormat.format("Error at line {2}, column {3}: {0}. Caused by: [{1}]", |
92 |
|
message, context, lineNumber, column)); |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
95 |
0 |
@Override... |
96 |
|
public EvaluatorException runtimeError(String message, String filename, int lineNumber, |
97 |
|
String context, int column) |
98 |
|
{ |
99 |
0 |
LOGGER.error(MessageFormat.format("Runtime error minimizing JSX object: {0}", message)); |
100 |
0 |
return null; |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0 |
@Override... |
104 |
|
public void warning(String message, String filename, int lineNumber, String context, int column) |
105 |
|
{ |
106 |
0 |
LOGGER.info(MessageFormat.format("Warning at line {2}, column {3}: {0}. Caused by: [{1}]", |
107 |
|
message, context, lineNumber, column)); |
108 |
|
} |
109 |
|
} |
110 |
|
} |
111 |
|
} |