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.svg; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileInputStream; |
24 |
|
import java.io.FileWriter; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.OutputStream; |
27 |
|
import java.lang.reflect.Type; |
28 |
|
import java.util.Iterator; |
29 |
|
import java.util.Vector; |
30 |
|
|
31 |
|
import org.apache.batik.apps.rasterizer.DestinationType; |
32 |
|
import org.apache.batik.apps.rasterizer.SVGConverter; |
33 |
|
import org.apache.batik.apps.rasterizer.SVGConverterException; |
34 |
|
import org.apache.commons.io.FileUtils; |
35 |
|
import org.apache.commons.io.IOUtils; |
36 |
|
import org.slf4j.Logger; |
37 |
|
import org.slf4j.LoggerFactory; |
38 |
|
import org.xwiki.environment.Environment; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.api.Api; |
42 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
43 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
44 |
|
import com.xpn.xwiki.web.Utils; |
45 |
|
import com.xpn.xwiki.web.XWikiResponse; |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 103 (103) |
Complexity: 25 |
Complexity Density: 0.32 |
|
47 |
|
public class SVGPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface |
48 |
|
{ |
49 |
|
private static Logger LOGGER = LoggerFactory.getLogger(com.xpn.xwiki.plugin.svg.SVGPlugin.class); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
private Environment environment = Utils.getComponent((Type) Environment.class); |
55 |
|
|
56 |
|
private File tempDir; |
57 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
58 |
0 |
public SVGPlugin(String name, String className, XWikiContext context)... |
59 |
|
{ |
60 |
0 |
super(name, className, context); |
61 |
0 |
init(context); |
62 |
|
} |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
@Override... |
65 |
|
public String getName() |
66 |
|
{ |
67 |
0 |
return "svg"; |
68 |
|
} |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
0 |
@Override... |
71 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
72 |
|
{ |
73 |
0 |
return new SVGPluginApi((SVGPlugin) plugin, context); |
74 |
|
} |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 0.6 |
|
76 |
0 |
@Override... |
77 |
|
public void flushCache() |
78 |
|
{ |
79 |
0 |
try { |
80 |
0 |
File[] filelist = this.tempDir.listFiles(); |
81 |
0 |
for (File element : filelist) { |
82 |
0 |
try { |
83 |
0 |
element.delete(); |
84 |
|
} catch (Exception e) { |
85 |
|
} |
86 |
|
} |
87 |
|
} catch (Exception e) { |
88 |
|
} |
89 |
|
} |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
91 |
0 |
@Override... |
92 |
|
public void init(XWikiContext context) |
93 |
|
{ |
94 |
0 |
super.init(context); |
95 |
|
|
96 |
0 |
File dir = this.environment.getTemporaryDirectory(); |
97 |
0 |
this.tempDir = new File(dir, "svg"); |
98 |
0 |
try { |
99 |
0 |
this.tempDir.mkdirs(); |
100 |
|
} catch (Exception ex) { |
101 |
0 |
LOGGER.warn("Cannot create temporary files", ex); |
102 |
|
} |
103 |
|
} |
104 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
0 |
public byte[] getSVGImage(String content, int height, int width) throws IOException, SVGConverterException... |
106 |
|
{ |
107 |
0 |
return getSVGImage(content, "png", height, width); |
108 |
|
} |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
110 |
0 |
public byte[] getSVGImage(String content, String extension, int height, int width) throws IOException,... |
111 |
|
SVGConverterException |
112 |
|
{ |
113 |
0 |
int hashCode = Math.abs(content.hashCode()); |
114 |
0 |
return getSVGImage(hashCode, content, extension, height, width); |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 2 |
Complexity Density: 0.1 |
|
117 |
0 |
public byte[] getSVGImage(int hashCode, String content, String extension, int height, int width)... |
118 |
|
throws IOException, SVGConverterException |
119 |
|
{ |
120 |
0 |
File dfile = getTempFile(hashCode, "svg"); |
121 |
0 |
if (!dfile.exists()) { |
122 |
0 |
FileWriter fwriter = new FileWriter(dfile); |
123 |
0 |
fwriter.write(content); |
124 |
0 |
fwriter.flush(); |
125 |
0 |
fwriter.close(); |
126 |
|
} |
127 |
|
|
128 |
0 |
File ofile = getTempFile(hashCode, extension); |
129 |
|
|
130 |
|
|
131 |
0 |
SVGConverter conv = new SVGConverter(); |
132 |
|
|
133 |
0 |
conv.setDestinationType(DestinationType.PNG); |
134 |
0 |
conv.setDst(ofile); |
135 |
0 |
conv.setHeight(height); |
136 |
0 |
conv.setWidth(width); |
137 |
0 |
String[] sources = { dfile.getAbsolutePath() }; |
138 |
0 |
conv.setSources(sources); |
139 |
0 |
conv.execute(); |
140 |
|
|
141 |
0 |
FileInputStream fis = new FileInputStream(ofile); |
142 |
0 |
byte[] result = new byte[(int) ofile.length()]; |
143 |
0 |
try { |
144 |
0 |
fis.read(result); |
145 |
|
} finally { |
146 |
0 |
IOUtils.closeQuietly(fis); |
147 |
|
} |
148 |
|
|
149 |
0 |
return result; |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
152 |
0 |
protected String[] expandSources(Vector sources)... |
153 |
|
{ |
154 |
0 |
Vector expandedSources = new Vector(); |
155 |
0 |
Iterator iter = sources.iterator(); |
156 |
0 |
while (iter.hasNext()) { |
157 |
0 |
String v = (String) iter.next(); |
158 |
0 |
File f = new File(v); |
159 |
0 |
if (f.exists() && f.isDirectory()) { |
160 |
0 |
File[] fl = f.listFiles(new SVGConverter.SVGFileFilter()); |
161 |
0 |
for (File element : fl) { |
162 |
0 |
expandedSources.addElement(element.getPath()); |
163 |
|
} |
164 |
|
} else { |
165 |
0 |
expandedSources.addElement(v); |
166 |
|
} |
167 |
|
} |
168 |
|
|
169 |
0 |
String[] s = new String[expandedSources.size()]; |
170 |
0 |
expandedSources.copyInto(s); |
171 |
0 |
return s; |
172 |
|
} |
173 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
0 |
public byte[] readSVGImage(File ofile) throws IOException... |
175 |
|
{ |
176 |
0 |
return FileUtils.readFileToByteArray(ofile); |
177 |
|
} |
178 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
179 |
0 |
public String writeSVGImage(String content, int height, int width) throws IOException, SVGConverterException... |
180 |
|
{ |
181 |
0 |
return writeSVGImage(content, "png", height, width); |
182 |
|
} |
183 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
184 |
0 |
public String writeSVGImage(String content, String extension, int height, int width) throws IOException,... |
185 |
|
SVGConverterException |
186 |
|
{ |
187 |
0 |
int hashCode = Math.abs(content.hashCode()); |
188 |
0 |
getSVGImage(hashCode, content, extension, height, width); |
189 |
0 |
return hashCode + "." + extension; |
190 |
|
} |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
0 |
public void outputSVGImage(String content, int height, int width, XWikiContext context) throws IOException,... |
193 |
|
SVGConverterException |
194 |
|
{ |
195 |
0 |
outputSVGImage(content, "png", height, width, context); |
196 |
|
} |
197 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
198 |
0 |
public void outputSVGImage(String content, String extension, int height, int width, XWikiContext context)... |
199 |
|
throws IOException, SVGConverterException |
200 |
|
{ |
201 |
0 |
byte[] svgbytes = getSVGImage(content, extension, height, width); |
202 |
0 |
XWikiResponse response = context.getResponse(); |
203 |
0 |
context.setFinished(true); |
204 |
0 |
response.setContentLength(svgbytes.length); |
205 |
0 |
response.setContentType(context.getEngineContext().getMimeType("toto." + extension)); |
206 |
0 |
OutputStream os = response.getOutputStream(); |
207 |
0 |
os.write(svgbytes); |
208 |
0 |
os.flush(); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
211 |
0 |
public void outputSVGImageFromFile(String filename, XWikiContext context) throws IOException... |
212 |
|
{ |
213 |
0 |
File ofile = getTempFile(filename); |
214 |
0 |
byte[] svgbytes = readSVGImage(ofile); |
215 |
0 |
XWikiResponse response = context.getResponse(); |
216 |
0 |
context.setFinished(true); |
217 |
0 |
response.setDateHeader("Last-Modified", ofile.lastModified()); |
218 |
0 |
response.setContentLength(svgbytes.length); |
219 |
0 |
response.setContentType(context.getEngineContext().getMimeType(filename)); |
220 |
0 |
OutputStream os = response.getOutputStream(); |
221 |
0 |
os.write(svgbytes); |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
0 |
public File getTempFile(String filename)... |
225 |
|
{ |
226 |
0 |
return new File(this.tempDir, filename); |
227 |
|
} |
228 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
0 |
public File getTempFile(int hashcode, String extension)... |
230 |
|
{ |
231 |
0 |
return getTempFile(hashcode + "." + extension); |
232 |
|
} |
233 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
234 |
0 |
public String getSVGImageURL(String content, int height, int width, XWikiContext context) throws IOException,... |
235 |
|
SVGConverterException |
236 |
|
{ |
237 |
0 |
String filename = writeSVGImage(content, "png", height, width); |
238 |
0 |
return context.getDoc().getAttachmentURL(filename, "svg", context); |
239 |
|
} |
240 |
|
} |