~ubuntu-branches/ubuntu/trusty/scilab/trusty

« back to all changes in this revision

Viewing changes to modules/helptools/src/java/org/scilab/modules/helptools/image/ScilabImageConverter.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-02 11:02:49 UTC
  • mfrom: (1.4.6)
  • Revision ID: package-import@ubuntu.com-20120802110249-0v5953emkp25geuz
Tags: 5.4.0-beta-2-1~exp1
* New upstream release
* Remove libscilab-java (remove upstream). Use libscilab2-java instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 
3
 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
 
4
 *
 
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
 
10
 *
 
11
 */
 
12
 
 
13
package org.scilab.modules.helptools.image;
 
14
 
 
15
import java.io.BufferedOutputStream;
 
16
import java.io.BufferedReader;
 
17
import java.io.File;
 
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;
 
24
import java.util.Map;
 
25
 
 
26
import org.scilab.modules.commons.ScilabCommons;
 
27
 
 
28
/**
 
29
 * Scilab code to PNG converter
 
30
 */
 
31
public class ScilabImageConverter implements ExternalImageConverter {
 
32
 
 
33
    private static ScilabImageConverter instance;
 
34
    private final StringBuilder buffer;
 
35
 
 
36
    private ScilabImageConverter() {
 
37
        buffer = new StringBuilder(8192);
 
38
    }
 
39
 
 
40
    public String getMimeType() {
 
41
        return "image/scilab";
 
42
    }
 
43
 
 
44
    public String getFileWithScilabCode() {
 
45
        if (buffer.length() != 0) {
 
46
            try {
 
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);
 
51
                out.flush();
 
52
                out.close();
 
53
 
 
54
                return f.getAbsolutePath();
 
55
            } catch (Exception e) {
 
56
                System.err.println("Cannot generate the file with Scilab code to execute:\n" + e);
 
57
            } finally {
 
58
                instance = null;
 
59
            }
 
60
        }
 
61
 
 
62
        instance = null;
 
63
 
 
64
        return null;
 
65
    }
 
66
 
 
67
    /**
 
68
     * Since this a singleton class...
 
69
     * @return this
 
70
     */
 
71
    public static ScilabImageConverter getInstance() {
 
72
        if (instance == null) {
 
73
            instance = new ScilabImageConverter();
 
74
        }
 
75
 
 
76
        return instance;
 
77
    }
 
78
 
 
79
    /**
 
80
     * {@inheritDoc}
 
81
     */
 
82
    public String convertToImage(String currentFile, String code, Map<String, String> attributes, File imageFile, String imageName) {
 
83
        return convertToPNG(currentFile, code, imageFile, imageName);
 
84
    }
 
85
 
 
86
    /**
 
87
     * {@inheritDoc}
 
88
     */
 
89
    public String convertToImage(File code, Map<String, String> attributes, File imageFile, String imageName) {
 
90
        try {
 
91
            BufferedReader in = new BufferedReader(new FileReader(code));
 
92
            StringBuilder buffer = new StringBuilder(8192);
 
93
            String line;
 
94
 
 
95
            while ((line = in.readLine()) != null) {
 
96
                buffer.append(line).append("\n");
 
97
            }
 
98
 
 
99
            in.close();
 
100
 
 
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());
 
104
        }
 
105
 
 
106
        return null;
 
107
    }
 
108
 
 
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");
 
117
 
 
118
        return "<img src=\'" + imageName + "\'/>";
 
119
    }
 
120
}
 
 
b'\\ No newline at end of file'