~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to timeline/exporting-server/java/highcharts-export/highcharts-export-convert/src/main/java/com/highcharts/export/converter/SVGConverter.java

  • Committer: Jeff Stys
  • Date: 2014-04-21 12:46:26 UTC
  • Revision ID: jstys@sesda3.com-20140421124626-2332pb2dyjc33jxi
Proof-of-concept version of Data Coverage Timeline using Highchart/Highstock javascript library.  Changes to getDataCoverage API in order to feed the necessary data to the Timeline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
12
import java.io.File;
 
13
import java.io.IOException;
 
14
import java.net.SocketTimeoutException;
 
15
import java.util.HashMap;
 
16
import java.util.Map;
 
17
import java.util.NoSuchElementException;
 
18
import java.util.concurrent.TimeoutException;
 
19
 
 
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;
 
24
 
 
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;
 
30
 
 
31
@Service("svgConverter")
 
32
public class SVGConverter {
 
33
 
 
34
        @Autowired
 
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\">";
 
38
 
 
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);
 
42
        }
 
43
 
 
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 {
 
46
 
 
47
                        Map<String, String> params = new HashMap<String, String>();
 
48
                        Gson gson = new Gson();
 
49
 
 
50
                        if (filename != null) {
 
51
                                params.put("outfile", filename);
 
52
                        } else {
 
53
                                params.put("type", mime.name().toLowerCase());
 
54
                        }
 
55
 
 
56
                        params.put("infile", input);
 
57
 
 
58
                        if (constructor != null && !constructor.isEmpty()) {
 
59
                                params.put("constr", constructor);
 
60
                        }
 
61
 
 
62
                        if (callback != null && !callback.isEmpty()) {
 
63
                                params.put("callback", callback);
 
64
                        }
 
65
 
 
66
                        if (globalOptions != null && !globalOptions.isEmpty()) {
 
67
                                params.put("globaloptions", globalOptions);
 
68
                        }
 
69
 
 
70
                        if (dataOptions != null && !dataOptions.isEmpty()) {
 
71
                                params.put("dataoptions", dataOptions);
 
72
                        }
 
73
 
 
74
                        if (customCode != null && !customCode.isEmpty()) {
 
75
                                params.put("customcode", customCode);
 
76
                        }
 
77
 
 
78
                        if (width != null) {
 
79
                                params.put("width", String.valueOf(width));
 
80
                        }
 
81
 
 
82
                        if (scale != null) {
 
83
                                params.put("scale", String.valueOf(scale));
 
84
                        }
 
85
 
 
86
                        // parameters to JSON
 
87
                        String json = gson.toJson(params);
 
88
 
 
89
                        // send to phantomJs
 
90
                        String output = "";
 
91
                        output = requestServer(json);
 
92
 
 
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);
 
97
                        }
 
98
 
 
99
                        return output;
 
100
        }
 
101
 
 
102
        /*
 
103
         * Redirect the SVG string directly
 
104
         */
 
105
        public String redirectSVG(String svg, String filename) throws SVGConverterException {
 
106
                // add XML Doctype for svg
 
107
                String output = SVG_DOCTYPE + svg;
 
108
 
 
109
                if (filename != null) {
 
110
                        // Create file and return filename instead.
 
111
                        //String filename = createUniqueFileName(".svg");
 
112
                        File file = new File(filename);
 
113
                        try {
 
114
                                FileUtils.writeStringToFile(file, output);
 
115
                        } catch (IOException ioex) {
 
116
                                logger.error(ioex.getMessage());
 
117
                                throw new SVGConverterException("Error while converting, " + ioex.getMessage());
 
118
                        }
 
119
                        return filename;
 
120
                }
 
121
                return output;
 
122
        }
 
123
 
 
124
        public String requestServer(String params) throws SVGConverterException, TimeoutException, NoSuchElementException, PoolException {
 
125
                Server server = null;
 
126
 
 
127
                try {
 
128
                        server = (Server) serverPool.borrowObject();
 
129
                        String response = server.request(params);
 
130
 
 
131
                        return response;
 
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());
 
142
                } finally {
 
143
                        try {
 
144
                                serverPool.returnObject(server, true);
 
145
                        } catch (Exception e) {
 
146
                                logger.error("Exception while returning server to pool: " + e.getMessage());
 
147
                        }
 
148
                }
 
149
        }
 
150
}
 
 
b'\\ No newline at end of file'