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

File SVGPlugin.java

 

Coverage histogram

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

Code metrics

6
79
18
1
240
186
25
0.32
4.39
18
1.39

Classes

Class Line # Actions
SVGPlugin 47 79 0% 25 103
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.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   
 
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    * Used to get the temporary directory.
53    */
54    private Environment environment = Utils.getComponent((Type) Environment.class);
55   
56    private File tempDir;
57   
 
58  0 toggle public SVGPlugin(String name, String className, XWikiContext context)
59    {
60  0 super(name, className, context);
61  0 init(context);
62    }
63   
 
64  0 toggle @Override
65    public String getName()
66    {
67  0 return "svg";
68    }
69   
 
70  0 toggle @Override
71    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context)
72    {
73  0 return new SVGPluginApi((SVGPlugin) plugin, context);
74    }
75   
 
76  0 toggle @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   
 
91  0 toggle @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   
 
105  0 toggle public byte[] getSVGImage(String content, int height, int width) throws IOException, SVGConverterException
106    {
107  0 return getSVGImage(content, "png", height, width);
108    }
109   
 
110  0 toggle 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   
 
117  0 toggle 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    // TODO implement conversion HERE
130   
131  0 SVGConverter conv = new SVGConverter();
132    // TODO PNG ONLY
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   
 
152  0 toggle 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   
 
174  0 toggle public byte[] readSVGImage(File ofile) throws IOException
175    {
176  0 return FileUtils.readFileToByteArray(ofile);
177    }
178   
 
179  0 toggle public String writeSVGImage(String content, int height, int width) throws IOException, SVGConverterException
180    {
181  0 return writeSVGImage(content, "png", height, width);
182    }
183   
 
184  0 toggle 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   
 
192  0 toggle 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   
 
198  0 toggle 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   
 
211  0 toggle 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   
 
224  0 toggle public File getTempFile(String filename)
225    {
226  0 return new File(this.tempDir, filename);
227    }
228   
 
229  0 toggle public File getTempFile(int hashcode, String extension)
230    {
231  0 return getTempFile(hashcode + "." + extension);
232    }
233   
 
234  0 toggle 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    }