~ubuntu-branches/ubuntu/raring/scilab/raring-proposed

« 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-30 14:42:38 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20120830144238-c1y2og7dbm7m9nig
Tags: 5.4.0-beta-3-1~exp1
* New upstream release
* Update the scirenderer dep
* Get ride of libjhdf5-java dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import java.util.Map;
25
25
 
26
26
import org.scilab.modules.commons.ScilabCommons;
 
27
import org.scilab.modules.helptools.HTMLDocbookTagConverter;
27
28
 
28
29
/**
29
30
 * Scilab code to PNG converter
32
33
 
33
34
    private static ScilabImageConverter instance;
34
35
    private final StringBuilder buffer;
 
36
    private final HTMLDocbookTagConverter.GenerationType type;
35
37
 
36
 
    private ScilabImageConverter() {
 
38
    private ScilabImageConverter(HTMLDocbookTagConverter.GenerationType type) {
37
39
        buffer = new StringBuilder(8192);
 
40
        this.type = type;
38
41
    }
39
42
 
40
43
    public String getMimeType() {
41
44
        return "image/scilab";
42
45
    }
43
46
 
44
 
    public String getFileWithScilabCode() {
45
 
        if (buffer.length() != 0) {
 
47
    /**
 
48
     * {@inheritDoc}
 
49
     */
 
50
    public boolean mustRegenerate() {
 
51
        return false;
 
52
    }
 
53
 
 
54
    public static String getFileWithScilabCode() {
 
55
        if (instance.buffer.length() != 0) {
46
56
            try {
47
57
                File f = File.createTempFile("help-", ".sce", new File(ScilabCommons.getTMPDIR()));
48
58
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
49
 
                byte[] arr = buffer.toString().getBytes();
 
59
                byte[] arr = instance.buffer.toString().getBytes();
50
60
                out.write(arr, 0, arr.length);
51
61
                out.flush();
52
62
                out.close();
68
78
     * Since this a singleton class...
69
79
     * @return this
70
80
     */
 
81
    public static ScilabImageConverter getInstance(HTMLDocbookTagConverter.GenerationType type) {
 
82
        if (instance == null) {
 
83
            instance = new ScilabImageConverter(type);
 
84
        }
 
85
 
 
86
        return instance;
 
87
    }
 
88
 
71
89
    public static ScilabImageConverter getInstance() {
72
 
        if (instance == null) {
73
 
            instance = new ScilabImageConverter();
74
 
        }
75
 
 
76
90
        return instance;
77
91
    }
78
92
 
115
129
        buffer.append("xend();\n");
116
130
        buffer.append("driver(__olddrv__);\n");
117
131
 
118
 
        return "<img src=\'" + imageName + "\'/>";
119
 
    }
120
 
}
 
 
b'\\ No newline at end of file'
 
132
        return getHTMLCodeToReturn(code, "<img src=\'" + imageName + "\'/>");
 
133
    }
 
134
 
 
135
    public String getHTMLCodeToReturn(String code, String imageTag) {
 
136
        if (type == HTMLDocbookTagConverter.GenerationType.WEB) {
 
137
            /* Prepare the code for the html inclusion */
 
138
            code = convertCode(code);
 
139
            /* Provide a tooltip */
 
140
            return "<div rel='tooltip' title='" + code + "'>" + imageTag + "</div>";
 
141
        } else {
 
142
            /* No tooltip in the javahelp browser ...
 
143
             * too limited html capabilities */
 
144
            return imageTag;
 
145
        }
 
146
    }
 
147
 
 
148
    private static final String convertCode(String code) {
 
149
        if (code == null || code.length() == 0) {
 
150
            return "";
 
151
        }
 
152
 
 
153
        StringBuffer buffer = new StringBuffer(2 * code.length());
 
154
        int start = 0;
 
155
        int end = code.length() - 1;
 
156
        char c = code.charAt(0);
 
157
 
 
158
        // first we trim
 
159
        while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
 
160
            if (start < end) {
 
161
                c = code.charAt(++start);
 
162
            } else {
 
163
                break;
 
164
            }
 
165
        }
 
166
        c = code.charAt(end);
 
167
        while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
 
168
            if (end > 0) {
 
169
                c = code.charAt(--end);
 
170
            } else {
 
171
                break;
 
172
            }
 
173
        }
 
174
 
 
175
        // replace chars by their html entities equivalent
 
176
        for (int j = start; j <= end; j++) {
 
177
            c = code.charAt(j);
 
178
            switch (c) {
 
179
            case '&':
 
180
                buffer.append("&amp;");
 
181
                break;
 
182
            case '<':
 
183
                buffer.append("&lt;");
 
184
                break;
 
185
            case '>':
 
186
                buffer.append("&gt;");
 
187
                break;
 
188
            case '\n':
 
189
                buffer.append("<br />");
 
190
                break;
 
191
            case '\'':
 
192
                buffer.append("&#039;");
 
193
                break;
 
194
            case '\"':
 
195
                buffer.append("&quot;");
 
196
                break;
 
197
            default:
 
198
                buffer.append(c);
 
199
            }
 
200
        }
 
201
 
 
202
        return buffer.toString();
 
203
    }
 
204
}