1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.formula; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.security.MessageDigest; |
24 |
|
import java.security.NoSuchAlgorithmException; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
|
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.slf4j.LoggerFactory; |
30 |
|
|
31 |
|
|
32 |
|
@link |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
@version |
37 |
|
@since |
38 |
|
|
|
|
| 52.6% |
Uncovered Elements: 18 (38) |
Complexity: 11 |
Complexity Density: 0.46 |
|
39 |
|
public abstract class AbstractFormulaRenderer implements FormulaRenderer |
40 |
|
{ |
41 |
|
|
42 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFormulaRenderer.class); |
43 |
|
|
44 |
|
|
45 |
|
@Inject |
46 |
|
private ImageStorage storage; |
47 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
48 |
2 |
@Override... |
49 |
|
public String process(String formula, boolean inline, FontSize size, Type type) throws IllegalArgumentException, |
50 |
|
IOException |
51 |
|
{ |
52 |
|
|
53 |
2 |
String cacheKey = computeImageID(formula, inline, size, type); |
54 |
2 |
if (this.storage.get(cacheKey) == null) { |
55 |
2 |
ImageData image = renderImage(formula, inline, size, type); |
56 |
2 |
this.storage.put(cacheKey, image); |
57 |
|
} |
58 |
2 |
return cacheKey; |
59 |
|
} |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
@Override... |
62 |
|
public ImageData getImage(String imageID) |
63 |
|
{ |
64 |
0 |
return this.storage.get(imageID); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@param |
71 |
|
|
72 |
|
@param |
73 |
|
@param |
74 |
|
@param |
75 |
|
@return@link |
76 |
|
@throws |
77 |
|
@throws |
78 |
|
|
79 |
|
protected abstract ImageData renderImage(String formula, boolean inline, FontSize size, Type type) |
80 |
|
throws IllegalArgumentException, IOException; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
|
87 |
|
@param |
88 |
|
@param |
89 |
|
@param |
90 |
|
@return |
91 |
|
|
|
|
| 38.1% |
Uncovered Elements: 13 (21) |
Complexity: 5 |
Complexity Density: 0.29 |
|
92 |
2 |
protected String computeImageID(String formula, boolean inline, FontSize size, Type type)... |
93 |
|
{ |
94 |
|
|
95 |
2 |
try { |
96 |
2 |
MessageDigest hashAlgorithm = MessageDigest.getInstance("SHA-256"); |
97 |
2 |
hashAlgorithm.update(inline ? (byte) 't' : (byte) 'f'); |
98 |
2 |
hashAlgorithm.update((byte) size.ordinal()); |
99 |
2 |
hashAlgorithm.update((byte) type.ordinal()); |
100 |
2 |
hashAlgorithm.update(formula.getBytes()); |
101 |
2 |
return String.valueOf(org.apache.commons.codec.binary.Hex.encodeHex(hashAlgorithm.digest())); |
102 |
|
} catch (NoSuchAlgorithmException ex) { |
103 |
0 |
LOGGER.error("No MD5 hash algorithm implementation", ex); |
104 |
|
} catch (NullPointerException ex) { |
105 |
0 |
LOGGER.error("Error hashing image name", ex); |
106 |
|
} |
107 |
|
|
108 |
0 |
final int prime = 37; |
109 |
0 |
int result = 1; |
110 |
0 |
result = prime * result + formula.hashCode(); |
111 |
0 |
result = prime * result + (inline ? 0 : 1); |
112 |
0 |
result = prime * result + size.hashCode(); |
113 |
0 |
result = prime * result + type.hashCode(); |
114 |
0 |
result = prime * result + this.getClass().getCanonicalName().hashCode(); |
115 |
0 |
return result + ""; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@param |
122 |
|
@param |
123 |
|
|
124 |
|
@return |
125 |
|
|
126 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 3 |
Complexity Density: 3 |
|
127 |
2 |
protected String wrapFormula(String formula, boolean inline)... |
128 |
|
{ |
129 |
2 |
return (inline ? "\\begin{math}" : "\\begin{displaymath}") + "\n{ " + formula + " }\n" |
130 |
2 |
+ (inline ? "\\end{math}" : "\\end{displaymath}"); |
131 |
|
} |
132 |
|
} |