You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.5 KiB

8 years ago
  1. /**
  2. * @license Highcharts JS v2.3.3 (2012-11-02)
  3. *
  4. * (c) 20012-2014
  5. *
  6. * Author: Gert Vaartjes
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. package com.highcharts.export.converter;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.net.SocketTimeoutException;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.NoSuchElementException;
  17. import java.util.concurrent.TimeoutException;
  18. import org.apache.commons.io.FileUtils;
  19. import org.apache.log4j.Logger;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import com.google.gson.Gson;
  23. import com.highcharts.export.pool.PoolException;
  24. import com.highcharts.export.pool.BlockingQueuePool;
  25. import com.highcharts.export.server.Server;
  26. import com.highcharts.export.util.MimeType;
  27. @Service("svgConverter")
  28. public class SVGConverter {
  29. @Autowired
  30. private BlockingQueuePool serverPool;
  31. protected static Logger logger = Logger.getLogger("converter");
  32. 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\">";
  33. public String convert(String input, MimeType mime,
  34. String constructor, String callback, String globalOptions, Float width, Float scale, String filename) throws SVGConverterException, PoolException, NoSuchElementException, TimeoutException {
  35. return this.convert(input, globalOptions, null, null, mime, constructor, callback, width, scale, filename);
  36. }
  37. public String convert(String input, String globalOptions, String dataOptions, String customCode, MimeType mime,
  38. String constructor, String callback, Float width, Float scale, String filename) throws SVGConverterException, PoolException, NoSuchElementException, TimeoutException {
  39. Map<String, String> params = new HashMap<String, String>();
  40. Gson gson = new Gson();
  41. if (filename != null) {
  42. params.put("outfile", filename);
  43. } else {
  44. params.put("type", mime.name().toLowerCase());
  45. }
  46. params.put("infile", input);
  47. if (constructor != null && !constructor.isEmpty()) {
  48. params.put("constr", constructor);
  49. }
  50. if (callback != null && !callback.isEmpty()) {
  51. params.put("callback", callback);
  52. }
  53. if (globalOptions != null && !globalOptions.isEmpty()) {
  54. params.put("globaloptions", globalOptions);
  55. }
  56. if (dataOptions != null && !dataOptions.isEmpty()) {
  57. params.put("dataoptions", dataOptions);
  58. }
  59. if (customCode != null && !customCode.isEmpty()) {
  60. params.put("customcode", customCode);
  61. }
  62. if (width != null) {
  63. params.put("width", String.valueOf(width));
  64. }
  65. if (scale != null) {
  66. params.put("scale", String.valueOf(scale));
  67. }
  68. // parameters to JSON
  69. String json = gson.toJson(params);
  70. // send to phantomJs
  71. String output = "";
  72. output = requestServer(json);
  73. // check first for errors
  74. if (output.length() > 5 && output.substring(0,5).equalsIgnoreCase("error")) {
  75. throw new SVGConverterException("recveived error from phantomjs:" + output);
  76. }
  77. return output;
  78. }
  79. /*
  80. * Redirect the SVG string directly
  81. */
  82. public String redirectSVG(String svg, String filename) throws SVGConverterException {
  83. // add XML Doctype for svg
  84. String output = SVG_DOCTYPE + svg;
  85. if (filename != null) {
  86. // Create file and return filename instead.
  87. //String filename = createUniqueFileName(".svg");
  88. File file = new File(filename);
  89. try {
  90. FileUtils.writeStringToFile(file, output);
  91. } catch (IOException ioex) {
  92. logger.error(ioex.getMessage());
  93. throw new SVGConverterException("Error while converting, " + ioex.getMessage());
  94. }
  95. return filename;
  96. }
  97. return output;
  98. }
  99. public String requestServer(String params) throws SVGConverterException, TimeoutException, NoSuchElementException, PoolException {
  100. Server server = null;
  101. try {
  102. server = (Server) serverPool.borrowObject();
  103. String response = server.request(params);
  104. return response;
  105. } catch (SocketTimeoutException ste) {
  106. logger.error(ste);
  107. throw new TimeoutException(ste.getMessage());
  108. } catch (TimeoutException te) {
  109. logger.error(te);
  110. throw new TimeoutException(te.getMessage());
  111. } catch (PoolException nse) {
  112. logger.error("POOL EXHAUSTED!!");
  113. throw new PoolException(nse.getMessage());
  114. } catch (Exception e) {
  115. logger.error(e);
  116. throw new SVGConverterException("Error converting SVG: " + e.getMessage());
  117. } finally {
  118. try {
  119. serverPool.returnObject(server, true);
  120. } catch (Exception e) {
  121. logger.error("Exception while returning server to pool: " + e.getMessage());
  122. }
  123. }
  124. }
  125. }