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;
27 import org.scilab.modules.helptools.HTMLDocbookTagConverter;
30 * Scilab code to PNG converter
32 public class ScilabImageConverter implements ExternalImageConverter {
34 private static ScilabImageConverter instance;
35 private final StringBuilder buffer;
36 private final HTMLDocbookTagConverter.GenerationType type;
38 private ScilabImageConverter(HTMLDocbookTagConverter.GenerationType type) {
39 buffer = new StringBuilder(8192);
43 public String getMimeType() {
44 return "image/scilab";
47 public static String getFileWithScilabCode() {
48 if (instance.buffer.length() != 0) {
50 File f = File.createTempFile("help-", ".sce", new File(ScilabCommons.getTMPDIR()));
51 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
52 byte[] arr = instance.buffer.toString().getBytes();
53 out.write(arr, 0, arr.length);
57 return f.getAbsolutePath();
58 } catch (Exception e) {
59 System.err.println("Cannot generate the file with Scilab code to execute:\n" + e);
71 * Since this a singleton class...
74 public static ScilabImageConverter getInstance(HTMLDocbookTagConverter.GenerationType type) {
75 if (instance == null) {
76 instance = new ScilabImageConverter(type);
85 public String convertToImage(String currentFile, String code, Map<String, String> attributes, File imageFile, String imageName) {
86 return convertToPNG(currentFile, code, imageFile, imageName);
92 public String convertToImage(File code, Map<String, String> attributes, File imageFile, String imageName) {
94 BufferedReader in = new BufferedReader(new FileReader(code));
95 StringBuilder buffer = new StringBuilder(8192);
98 while ((line = in.readLine()) != null) {
99 buffer.append(line).append("\n");
104 return convertToPNG(code.getName(), buffer.toString(), imageFile, imageName);
105 } catch (Exception e) {
106 System.err.println("Problem when exporting Scilab code to " + imageFile + "!\n" + e.toString());
112 private final String convertToPNG(String currentFile, String code, File imageFile, String imageName) {
113 buffer.append("__olddrv__=driver();\n");
114 buffer.append("disp(\"Generate image " + imageName + " from Scilab code in file " + new File(currentFile).getName() + "\");\n");
115 buffer.append("driver(\"png\");\n");
116 buffer.append("xinit(\"").append(imageFile.getAbsolutePath()).append("\");\n");
117 buffer.append(code).append("\n");
118 buffer.append("xend();\n");
119 buffer.append("driver(__olddrv__);\n");
122 /* Prepare the code for the html inclusion */
123 code = code.trim().replace("&", "&").replace("<", "<").replace(">", ">").replace("\n", "<br />");
125 return " <div rel='tooltip' title='" + code + "'><img src=\'" + imageName + "\'/></div>";