2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.helptools.image;
15 import java.io.BufferedOutputStream;
16 import java.io.BufferedReader;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.OutputStream;
26 import org.scilab.modules.commons.ScilabCommons;
29 * Scilab code to PNG converter
31 public class ScilabImageConverter implements ExternalImageConverter {
33 private static ScilabImageConverter instance;
34 private final StringBuilder buffer;
36 private ScilabImageConverter() {
37 buffer = new StringBuilder(8192);
40 public String getMimeType() {
41 return "image/scilab";
44 public String getFileWithScilabCode() {
45 if (buffer.length() != 0) {
47 File f = File.createTempFile("help-", ".sce", new File(ScilabCommons.getTMPDIR()));
48 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
49 byte[] arr = buffer.toString().getBytes();
50 out.write(arr, 0, arr.length);
54 return f.getAbsolutePath();
55 } catch (Exception e) {
56 System.err.println("Cannot generate the file with Scilab code to execute:\n" + e);
68 * Since this a singleton class...
71 public static ScilabImageConverter getInstance() {
72 if (instance == null) {
73 instance = new ScilabImageConverter();
82 public String convertToImage(String currentFile, String code, Map<String, String> attributes, File imageFile, String imageName) {
83 return convertToPNG(currentFile, code, imageFile, imageName);
89 public String convertToImage(File code, Map<String, String> attributes, File imageFile, String imageName) {
91 BufferedReader in = new BufferedReader(new FileReader(code));
92 StringBuilder buffer = new StringBuilder(8192);
95 while ((line = in.readLine()) != null) {
96 buffer.append(line).append("\n");
101 return convertToPNG(code.getName(), buffer.toString(), imageFile, imageName);
102 } catch (Exception e) {
103 System.err.println("Problem when exporting Scilab code to " + imageFile + "!\n" + e.toString());
109 private final String convertToPNG(String currentFile, String code, File imageFile, String imageName) {
110 buffer.append("__olddrv__=driver();\n");
111 buffer.append("disp(\"Generate image " + imageName + " from Scilab code in file " + new File(currentFile).getName() + "\");\n");
112 buffer.append("driver(\"png\");\n");
113 buffer.append("xinit(\"").append(imageFile.getAbsolutePath()).append("\");\n");
114 buffer.append(code).append("\n");
115 buffer.append("xend();\n");
116 buffer.append("driver(__olddrv__);\n");
119 /* Prepare the code for the html inclusion */
120 code = code.trim().replace("&", "&").replace("<", "<").replace(">", ">").replace("\n", "<br />");
122 return " <div rel='tooltip' title='" + code + "'><img src=\'" + imageName + "\'/></div>";