1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts

File ChartingPlugin.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
71
11
1
234
170
19
0.27
6.45
11
1.73

Classes

Class Line # Actions
ChartingPlugin 63 71 0% 19 84
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * TODO: Document this
60    *
61    * @version $Id: 8f42639e4f578d8189062a6974bfa30c60cf9aa9 $
62    */
 
63    public class ChartingPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface
64    {
65    /**
66    * Used to get the temporary directory.
67    */
68    private Environment environment = Utils.getComponent((Type) Environment.class);
69   
 
70  0 toggle public ChartingPlugin(String name, String className, XWikiContext context)
71    {
72  0 super(name, className, context);
73  0 init(context);
74    }
75   
 
76  0 toggle @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   
 
97  0 toggle @Override
98    public String getName()
99    {
100  0 return "charting";
101    }
102   
 
103  0 toggle public Chart generateChart(ChartParams params, XWikiContext context) throws GenerateException
104    {
105  0 try {
106    // Obtain the corresponding data source and wrap it into a data source object
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   
 
143  0 toggle private Chart generateSvgChart(JFreeChart jfchart, ChartParams params, XWikiContext context) throws IOException,
144    GenerateException
145    {
146    // Get a DOMImplementation
147  0 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
148    // Create an instance of org.w3c.dom.Document
149  0 Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
150    // Create an instance of the SVG Generator
151  0 SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
152    // Ask the chart to render into the SVG Graphics2D implementation
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   
 
180  0 toggle 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   
 
194  0 toggle 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   
 
207  0 toggle 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   
 
215  0 toggle @Override
216    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context)
217    {
218  0 return new ChartingPluginApi((ChartingPlugin) plugin, context);
219    }
220   
 
221  0 toggle private File getTempFile(int hashcode, String extension)
222    {
223  0 return getTempFile(hashcode + "." + extension);
224    }
225   
 
226  0 toggle 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    }