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.charts; |
21 |
|
|
22 |
|
import java.awt.geom.Rectangle2D; |
23 |
|
import java.io.File; |
24 |
|
import java.io.FileInputStream; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.OutputStream; |
27 |
|
import java.io.StringWriter; |
28 |
|
import java.lang.reflect.InvocationTargetException; |
29 |
|
import java.lang.reflect.Method; |
30 |
|
import java.lang.reflect.Type; |
31 |
|
|
32 |
|
import org.apache.batik.apps.rasterizer.SVGConverterException; |
33 |
|
import org.apache.batik.dom.GenericDOMImplementation; |
34 |
|
import org.apache.batik.svggen.SVGGraphics2D; |
35 |
|
import org.jfree.chart.ChartUtilities; |
36 |
|
import org.jfree.chart.JFreeChart; |
37 |
|
import org.jfree.chart.plot.Plot; |
38 |
|
import org.slf4j.Logger; |
39 |
|
import org.slf4j.LoggerFactory; |
40 |
|
import org.w3c.dom.DOMImplementation; |
41 |
|
import org.w3c.dom.Document; |
42 |
|
import org.xwiki.environment.Environment; |
43 |
|
|
44 |
|
import com.xpn.xwiki.XWikiContext; |
45 |
|
import com.xpn.xwiki.api.Api; |
46 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
47 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
48 |
|
import com.xpn.xwiki.plugin.charts.exceptions.DataSourceException; |
49 |
|
import com.xpn.xwiki.plugin.charts.exceptions.GenerateException; |
50 |
|
import com.xpn.xwiki.plugin.charts.params.ChartParams; |
51 |
|
import com.xpn.xwiki.plugin.charts.plots.PlotFactory; |
52 |
|
import com.xpn.xwiki.plugin.charts.source.DataSource; |
53 |
|
import com.xpn.xwiki.plugin.charts.source.MainDataSourceFactory; |
54 |
|
import com.xpn.xwiki.plugin.svg.SVGPlugin; |
55 |
|
import com.xpn.xwiki.web.Utils; |
56 |
|
import com.xpn.xwiki.web.XWikiResponse; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@version |
62 |
|
|
|
|
| 0% |
Uncovered Elements: 84 (84) |
Complexity: 19 |
Complexity Density: 0.27 |
|
63 |
|
public class ChartingPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface |
64 |
|
{ |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private Environment environment = Utils.getComponent((Type) Environment.class); |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
70 |
0 |
public ChartingPlugin(String name, String className, XWikiContext context)... |
71 |
|
{ |
72 |
0 |
super(name, className, context); |
73 |
0 |
init(context); |
74 |
|
} |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.27 |
|
76 |
0 |
@Override... |
77 |
|
public void init(XWikiContext context) |
78 |
|
{ |
79 |
0 |
super.init(context); |
80 |
0 |
LOGGER.info("Charting Plugin - init"); |
81 |
|
|
82 |
0 |
File dir = this.environment.getTemporaryDirectory(); |
83 |
0 |
tempDir = new File(dir, "charts"); |
84 |
0 |
try { |
85 |
0 |
tempDir.mkdirs(); |
86 |
|
} catch (Exception e1) { |
87 |
0 |
LOGGER.warn("Could not create charts temporary directory: " + tempDir, e1); |
88 |
0 |
dir = new File(context.getWiki().Param("xwiki.upload.tempdir")); |
89 |
0 |
try { |
90 |
0 |
tempDir = new File(dir, "charts"); |
91 |
|
} catch (Exception e2) { |
92 |
0 |
LOGGER.error("Could not create charts temporary directory: " + tempDir, e2); |
93 |
|
} |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
0 |
@Override... |
98 |
|
public String getName() |
99 |
|
{ |
100 |
0 |
return "charting"; |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0.28 |
|
103 |
0 |
public Chart generateChart(ChartParams params, XWikiContext context) throws GenerateException... |
104 |
|
{ |
105 |
0 |
try { |
106 |
|
|
107 |
0 |
DataSource dataSource = |
108 |
|
MainDataSourceFactory.getInstance().create(params.getMap(ChartParams.SOURCE), context); |
109 |
|
|
110 |
0 |
String type = params.getString(ChartParams.TYPE); |
111 |
|
|
112 |
0 |
Plot plot; |
113 |
0 |
try { |
114 |
0 |
String factoryClassName = |
115 |
|
ChartingPlugin.class.getPackage().getName() + ".plots." + Character.toUpperCase(type.charAt(0)) |
116 |
|
+ type.toLowerCase().substring(1) + "PlotFactory"; |
117 |
|
|
118 |
0 |
Class factoryClass = Class.forName(factoryClassName); |
119 |
0 |
Method method = factoryClass.getMethod("getInstance", new Class[] {}); |
120 |
0 |
PlotFactory factory = (PlotFactory) method.invoke(null, new Object[] {}); |
121 |
|
|
122 |
0 |
plot = factory.create(dataSource, params); |
123 |
|
} catch (InvocationTargetException e) { |
124 |
0 |
throw new GenerateException(e.getTargetException()); |
125 |
|
} catch (Throwable e) { |
126 |
0 |
throw new GenerateException(e); |
127 |
|
} |
128 |
|
|
129 |
0 |
ChartCustomizer.customizePlot(plot, params); |
130 |
|
|
131 |
0 |
JFreeChart jfchart = new JFreeChart(plot); |
132 |
|
|
133 |
0 |
ChartCustomizer.customizeChart(jfchart, params); |
134 |
|
|
135 |
0 |
return generatePngChart(jfchart, params, context); |
136 |
|
} catch (IOException ioe) { |
137 |
0 |
throw new GenerateException(ioe); |
138 |
|
} catch (DataSourceException dse) { |
139 |
0 |
throw new GenerateException(dse); |
140 |
|
} |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 3 |
Complexity Density: 0.17 |
|
143 |
0 |
private Chart generateSvgChart(JFreeChart jfchart, ChartParams params, XWikiContext context) throws IOException,... |
144 |
|
GenerateException |
145 |
|
{ |
146 |
|
|
147 |
0 |
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); |
148 |
|
|
149 |
0 |
Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null); |
150 |
|
|
151 |
0 |
SVGGraphics2D svgGenerator = new SVGGraphics2D(document); |
152 |
|
|
153 |
0 |
Rectangle2D.Double rect = |
154 |
|
new Rectangle2D.Double(0, 0, params.getInteger(ChartParams.WIDTH).intValue(), params.getInteger( |
155 |
|
ChartParams.HEIGHT).intValue()); |
156 |
0 |
jfchart.draw(svgGenerator, rect); |
157 |
0 |
boolean useCSS = false; |
158 |
0 |
StringWriter swriter = new StringWriter(); |
159 |
0 |
svgGenerator.stream(swriter, useCSS); |
160 |
0 |
String svgText = swriter.toString(); |
161 |
|
|
162 |
0 |
String pageURL = null; |
163 |
0 |
SVGPlugin svgPlugin = (SVGPlugin) context.getWiki().getPlugin("svg", context); |
164 |
0 |
if (svgPlugin == null) { |
165 |
0 |
throw new GenerateException("SVGPlugin not loaded"); |
166 |
|
} |
167 |
|
|
168 |
0 |
String imageURL; |
169 |
0 |
try { |
170 |
0 |
imageURL = |
171 |
|
svgPlugin.getSVGImageURL(svgText, params.getInteger(ChartParams.HEIGHT).intValue(), |
172 |
|
params.getInteger(ChartParams.WIDTH).intValue(), context); |
173 |
|
} catch (SVGConverterException sce) { |
174 |
0 |
throw new GenerateException(sce); |
175 |
|
} |
176 |
|
|
177 |
0 |
return new ChartImpl(params, imageURL, pageURL); |
178 |
|
} |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
180 |
0 |
private Chart generatePngChart(JFreeChart jfchart, ChartParams params, XWikiContext context) throws IOException,... |
181 |
|
GenerateException |
182 |
|
{ |
183 |
|
|
184 |
0 |
File file = getTempFile(params.hashCode(), "png"); |
185 |
|
|
186 |
0 |
ChartUtilities.saveChartAsPNG(file, jfchart, params.getInteger(ChartParams.WIDTH).intValue(), params |
187 |
|
.getInteger(ChartParams.HEIGHT).intValue()); |
188 |
|
|
189 |
0 |
String imageURL = context.getDoc().getAttachmentURL(file.getName(), "charting", context); |
190 |
0 |
String pageURL = imageURL; |
191 |
0 |
return new ChartImpl(params, imageURL, pageURL); |
192 |
|
} |
193 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
194 |
0 |
public void outputFile(String filename, XWikiContext context) throws IOException... |
195 |
|
{ |
196 |
0 |
File ofile = getTempFile(filename); |
197 |
0 |
byte[] bytes = readFile(ofile); |
198 |
0 |
XWikiResponse response = context.getResponse(); |
199 |
0 |
context.setFinished(true); |
200 |
0 |
response.setDateHeader("Last-Modified", ofile.lastModified()); |
201 |
0 |
response.setContentLength(bytes.length); |
202 |
0 |
response.setContentType(context.getEngineContext().getMimeType(filename)); |
203 |
0 |
OutputStream os = response.getOutputStream(); |
204 |
0 |
os.write(bytes); |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
207 |
0 |
public byte[] readFile(File ofile) throws IOException... |
208 |
|
{ |
209 |
0 |
FileInputStream fis = new FileInputStream(ofile); |
210 |
0 |
byte[] result = new byte[(int) ofile.length()]; |
211 |
0 |
fis.read(result); |
212 |
0 |
return result; |
213 |
|
} |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
0 |
@Override... |
216 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
217 |
|
{ |
218 |
0 |
return new ChartingPluginApi((ChartingPlugin) plugin, context); |
219 |
|
} |
220 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
0 |
private File getTempFile(int hashcode, String extension)... |
222 |
|
{ |
223 |
0 |
return getTempFile(hashcode + "." + extension); |
224 |
|
} |
225 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
0 |
private File getTempFile(String filename)... |
227 |
|
{ |
228 |
0 |
return new File(tempDir, filename); |
229 |
|
} |
230 |
|
|
231 |
|
private static Logger LOGGER = LoggerFactory.getLogger(ChartingPlugin.class); |
232 |
|
|
233 |
|
private File tempDir; |
234 |
|
} |