2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - 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.external;
16 import java.io.FileFilter;
17 import java.io.IOException;
18 import java.util.HashMap;
21 import org.xml.sax.Attributes;
23 import org.scilab.modules.helptools.image.ImageConverter;
24 import org.scilab.modules.helptools.image.ScilabImageConverter;
27 * Handle the included SCILAB code
28 * @author Calixte DENIZET
30 public class HTMLScilabHandler extends ExternalXMLHandler {
32 private static final String IMAGE = "image";
33 private static final String BASENAME = "_";
35 private static HTMLScilabHandler instance;
37 private int compt = 1;
38 private StringBuilder buffer = new StringBuilder(8192);
39 private String baseDir;
40 private String outputDir;
41 private boolean isLocalized;
45 * @param baseDir the base directory where to put the generated images
47 private HTMLScilabHandler(String outputDir, String baseDir) {
48 this.outputDir = outputDir + File.separator + baseDir;
49 this.baseDir = baseDir + "/";
52 public static HTMLScilabHandler getInstance(String outputDir, String baseDir) {
53 if (instance == null) {
54 instance = new HTMLScilabHandler(outputDir, baseDir);
60 public static HTMLScilabHandler getInstance() {
64 public void resetCompt() {
71 public String getURI() {
72 return "http://www.scilab.org";
78 public StringBuilder startExternalXML(String localName, Attributes attributes) {
79 if (localName.equals("image")) {
80 String v = attributes.getValue("localized");
81 isLocalized = "true".equalsIgnoreCase(v);
84 if (IMAGE.equals(localName)) {
87 recreateTag(buffer, localName, attributes);
96 public String endExternalXML(String localName) {
97 if (IMAGE.equals(localName)) {
98 String currentFileName = getConverter().getCurrentFileName();
99 String baseName = new File(currentFileName).getName();
100 int dotpos = baseName.lastIndexOf('.');
102 baseName = baseName.substring(0, dotpos);
104 String fileName = baseName + BASENAME + (compt++) + ".png";
105 File f = new File(outputDir, fileName);
106 Map<String, String> attributes = new HashMap<String, String>();
110 if (isLocalized || (existing = getExistingFile(outputDir, fileName)) == null) {
111 ret = ImageConverter.getImageByCode(currentFileName, buffer.toString(), attributes, "image/scilab", f, baseDir + f.getName());
113 ret = ImageConverter.getImageByFile(attributes, null, existing.getAbsolutePath(), outputDir, ".");
114 ret = ScilabImageConverter.getInstance().getHTMLCodeToReturn(buffer.toString(), ret);
122 recreateTag(buffer, localName, null);
127 private static File getExistingFile(String outputDir, String filename) {
129 final File outDir = new File(outputDir).getCanonicalFile();
130 FileFilter filter = new FileFilter() {
131 public boolean accept(File f) {
132 return f.isDirectory() && !f.equals(outDir);
135 File[] dirs = outDir.getParentFile().listFiles(filter);
136 File im = new File(filename);
137 for (File dir : dirs) {
138 File f = new File(dir, im.getName());
139 if (f.exists() && f.canRead()) {
143 } catch (IOException e) { }