2
* @license Highcharts JS v2.3.3 (2012-11-02)
6
* Author: Gert Vaartjes
8
* License: www.highcharts.com/license
10
package com.highcharts.export.converter;
13
import java.io.IOException;
14
import java.net.SocketTimeoutException;
15
import java.util.HashMap;
17
import java.util.NoSuchElementException;
18
import java.util.concurrent.TimeoutException;
20
import org.apache.commons.io.FileUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.stereotype.Service;
25
import com.google.gson.Gson;
26
import com.highcharts.export.pool.PoolException;
27
import com.highcharts.export.pool.BlockingQueuePool;
28
import com.highcharts.export.server.Server;
29
import com.highcharts.export.util.MimeType;
31
@Service("svgConverter")
32
public class SVGConverter {
35
private BlockingQueuePool serverPool;
36
protected static Logger logger = Logger.getLogger("converter");
37
private static final String SVG_DOCTYPE = "<?xml version=\"1.0\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">";
39
public String convert(String input, MimeType mime,
40
String constructor, String callback, Float width, Float scale, String filename) throws SVGConverterException, PoolException, NoSuchElementException, TimeoutException {
41
return this.convert(input, null, null, null, mime, constructor, callback, width, scale, filename);
44
public String convert(String input, String globalOptions, String dataOptions, String customCode, MimeType mime,
45
String constructor, String callback, Float width, Float scale, String filename) throws SVGConverterException, PoolException, NoSuchElementException, TimeoutException {
47
Map<String, String> params = new HashMap<String, String>();
48
Gson gson = new Gson();
50
if (filename != null) {
51
params.put("outfile", filename);
53
params.put("type", mime.name().toLowerCase());
56
params.put("infile", input);
58
if (constructor != null && !constructor.isEmpty()) {
59
params.put("constr", constructor);
62
if (callback != null && !callback.isEmpty()) {
63
params.put("callback", callback);
66
if (globalOptions != null && !globalOptions.isEmpty()) {
67
params.put("globaloptions", globalOptions);
70
if (dataOptions != null && !dataOptions.isEmpty()) {
71
params.put("dataoptions", dataOptions);
74
if (customCode != null && !customCode.isEmpty()) {
75
params.put("customcode", customCode);
79
params.put("width", String.valueOf(width));
83
params.put("scale", String.valueOf(scale));
87
String json = gson.toJson(params);
91
output = requestServer(json);
93
// check first for errors
94
if (output.length() > 5 && output.substring(0,5).equalsIgnoreCase("error")) {
95
logger.debug("recveived error from phantomjs: " + output);
96
throw new SVGConverterException("recveived error from phantomjs:" + output);
103
* Redirect the SVG string directly
105
public String redirectSVG(String svg, String filename) throws SVGConverterException {
106
// add XML Doctype for svg
107
String output = SVG_DOCTYPE + svg;
109
if (filename != null) {
110
// Create file and return filename instead.
111
//String filename = createUniqueFileName(".svg");
112
File file = new File(filename);
114
FileUtils.writeStringToFile(file, output);
115
} catch (IOException ioex) {
116
logger.error(ioex.getMessage());
117
throw new SVGConverterException("Error while converting, " + ioex.getMessage());
124
public String requestServer(String params) throws SVGConverterException, TimeoutException, NoSuchElementException, PoolException {
125
Server server = null;
128
server = (Server) serverPool.borrowObject();
129
String response = server.request(params);
132
} catch (SocketTimeoutException ste) {
133
throw new TimeoutException(ste.getMessage());
134
} catch (TimeoutException te) {
135
throw new TimeoutException(te.getMessage());
136
} catch (PoolException nse) {
137
logger.error("POOL EXHAUSTED!!");
138
throw new PoolException(nse.getMessage());
139
} catch (Exception e) {
140
logger.debug(e.getMessage());
141
throw new SVGConverterException("Error converting SVG" + e.getMessage());
144
serverPool.returnObject(server, true);
145
} catch (Exception e) {
146
logger.error("Exception while returning server to pool: " + e.getMessage());
b'\\ No newline at end of file'