1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.svg.script

File SVGScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
17
6
1
165
73
9
0.53
2.83
6
1.5

Classes

Class Line # Actions
SVGScriptService 47 17 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 12 tests. .

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 org.xwiki.platform.svg.script;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.apache.commons.lang3.exception.ExceptionUtils;
27    import org.slf4j.Logger;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.platform.svg.SVGRasterizer;
31    import org.xwiki.resource.ResourceReferenceSerializer;
32    import org.xwiki.resource.temporary.TemporaryResourceReference;
33    import org.xwiki.script.service.ScriptService;
34    import org.xwiki.stability.Unstable;
35    import org.xwiki.url.ExtendedURL;
36   
37    /**
38    * Utilities for working with SVG images.
39    *
40    * @version $Id: 84b8bdea85a6f16f124122c14a021a1a24332afc $
41    * @since 8.0M1
42    */
43    @Component
44    @Named("svg")
45    @Singleton
46    @Unstable("New API introduced in 8.0")
 
47    public class SVGScriptService implements ScriptService
48    {
49    @Inject
50    private Logger logger;
51   
52    @Inject
53    private SVGRasterizer component;
54   
55    @Inject
56    @Named("standard/tmp")
57    private ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> serializer;
58   
59    /**
60    * Rasterize an image as PNG into a temporary resource belonging to the current document, accessible through the
61    * "tmp" resource URL handler.
62    *
63    * @param content the SVG image
64    * @return URL pointing to the temporary resource where the PNG is stored
65    */
 
66  2 toggle public ExtendedURL rasterizeToTemporaryResource(String content)
67    {
68  2 return rasterizeToTemporaryResource(content, 0, 0);
69    }
70   
71    /**
72    * Rasterize an image as PNG into a temporary resource belonging to the current document, accessible through the
73    * "tmp" resource URL handler.
74    *
75    * @param content the SVG image
76    * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
77    * is used
78    * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
79    * size is used
80    * @return URL pointing to the temporary resource where the PNG is stored
81    */
 
82  4 toggle public ExtendedURL rasterizeToTemporaryResource(String content, int width, int height)
83    {
84  4 try {
85  4 return this.serializer.serialize(this.component.rasterizeToTemporaryResource(content, width, height));
86    } catch (Exception ex) {
87  2 this.logger.warn("Failed to rasterize SVG image to temporary resource: {}",
88    ExceptionUtils.getRootCauseMessage(ex));
89    }
90  2 return null;
91    }
92   
93    /**
94    * Rasterize an image as PNG into a temporary resource belonging to the current document, accessible through the
95    * "tmp" resource URL handler.
96    *
97    * @param content the SVG image
98    * @param targetContext the document which will "own" the new temporary resource
99    * @return URL pointing to the temporary resource where the PNG is stored
100    */
 
101  2 toggle public ExtendedURL rasterizeToTemporaryResource(String content, DocumentReference targetContext)
102    {
103  2 return rasterizeToTemporaryResource(content, 0, 0, targetContext);
104    }
105   
106    /**
107    * Rasterize an image as PNG as into temporary resource belonging to the current document, accessible through the
108    * "tmp" resource URL handler.
109    *
110    * @param content the SVG image
111    * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
112    * is used
113    * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
114    * size is used
115    * @param targetContext the document which will "own" the new temporary resource
116    * @return URL pointing to the temporary resource where the PNG is stored
117    */
 
118  4 toggle public ExtendedURL rasterizeToTemporaryResource(String content, int width, int height,
119    DocumentReference targetContext)
120    {
121  4 try {
122  4 ExtendedURL result = this.serializer
123    .serialize(this.component.rasterizeToTemporaryResource(content, width, height, targetContext));
124  2 return result;
125    } catch (Exception ex) {
126  2 this.logger.warn("Failed to rasterize SVG image to temporary resource in context [{}]: {}", targetContext,
127    ExceptionUtils.getRootCauseMessage(ex));
128    }
129  2 return null;
130    }
131   
132    /**
133    * Rasterize an image as PNG into the current response.
134    *
135    * @param content the SVG image
136    * @return {@code true} if the image was successfully rasterized and written to the response, {@code false} in case
137    * of exceptions
138    */
 
139  2 toggle public boolean rasterizeToResponse(String content)
140    {
141  2 return rasterizeToResponse(content, 0, 0);
142    }
143   
144    /**
145    * Rasterize an image as PNG into the current response.
146    *
147    * @param content the SVG image
148    * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
149    * is used
150    * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
151    * size is used
152    * @return {@code true} if the image was successfully rasterized and written to the response, {@code false} in case
153    * of exceptions
154    */
 
155  4 toggle public boolean rasterizeToResponse(String content, int width, int height)
156    {
157  4 try {
158  4 this.component.rasterizeToResponse(content, width, height);
159  2 return true;
160    } catch (Exception ex) {
161  2 this.logger.warn("Failed to rasterize SVG image to response: {}", ExceptionUtils.getRootCauseMessage(ex));
162    }
163  2 return false;
164    }
165    }