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;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStreamWriter;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.xml.sax.SAXException;
25 import org.scilab.modules.localization.LocaleToLCID;
28 * Class to convert DocBook to CHM
29 * @author Calixte DENIZET
31 public class CHMDocbookTagConverter extends HTMLDocbookTagConverter {
33 private StringBuilder buffer = new StringBuilder(8192);
34 private String outName;
35 private List<String> filesList = new ArrayList<String>();
36 private String docWebsite;
40 * @param inName the name of the input stream
41 * @param outName the output directory
42 * @param primConf the file containing the primitives of Scilab
43 * @param macroConf the file containing the macros of Scilab
44 * @param template the template to use
45 * @param version the version
46 * @param imageDir the image directory (relative to outName)
47 * @param isToolbox is true when compile a toolbox' help
48 * @param urlBase the base url for external link
49 * @param language the language to use ('en_US', 'fr_FR', ...)
51 public CHMDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, String docWebsite, boolean isToolbox, String urlBase, String language) throws IOException, SAXException {
52 super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.CHM);
53 this.outName = new File(outName).getCanonicalPath() + File.separator;
54 this.docWebsite = docWebsite;
60 public void createHTMLFile(String id, String fileName, String subtitle, String contents) {
61 super.createHTMLFile(id, fileName, subtitle, contents);
62 filesList.add(fileName);
68 public String makePrevious(String id) {
70 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
72 return "<link rel=\"prev\" href=\"\" title=\"\">";
74 HTMLDocbookLinkResolver.TreeId prev = leaf.getPrevious();
75 if (prev.parent != null) {
76 buffer.append("<link rel=\"prev\" href=\"");
77 buffer.append(mapId.get(prev.id));
78 buffer.append("\" title=\"");
79 buffer.append(tocitem.get(prev.id));
82 return buffer.toString();
85 return "<link rel=\"prev\" href=\"\" title=\"\">";
91 public String makeTop(String id) {
93 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
95 return "<link rel=\"up\" href=\"\" title=\"\">";
100 buffer.append("<link rel=\"up\" href=\"");
101 if (!leaf.isRoot()) {
102 buffer.append(mapId.get(leaf.id));
103 buffer.append("\" title=\"");
104 buffer.append(tocitem.get(leaf.id));
106 buffer.append(indexFilename);
107 buffer.append("\" title=\"");
108 buffer.append(bookTitle);
110 buffer.append("\">");
112 return buffer.toString();
115 return "<link rel=\"up\" href=\"\" title=\"\">";
121 public String makeNext(String id) {
123 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
125 return "<link rel=\"next\" href=\"\" title=\"\">";
127 HTMLDocbookLinkResolver.TreeId next = leaf.getNext();
129 buffer.append("<link rel=\"next\" href=\"");
130 buffer.append(mapId.get(next.id));
131 buffer.append("\" title=\"");
132 buffer.append(tocitem.get(next.id));
133 buffer.append("\">");
135 return buffer.toString();
138 return "<link rel=\"next\" href=\"\" title=\"\">";
144 public String makeStart(String id) {
146 buffer.append("<link rel=\"start\" href=\"");
147 buffer.append(indexFilename);
148 buffer.append("\" title=\"");
149 buffer.append(bookTitle);
150 buffer.append("\">");
152 return buffer.toString();
158 public void endDocument() throws SAXException {
160 FileOutputStream outToc = new FileOutputStream("toc.hhc");
161 FileOutputStream outFiles = new FileOutputStream("htmlhelp.hhp");
162 OutputStreamWriter writerFiles = new OutputStreamWriter(outFiles, Charset.forName("UTF-8"));
163 OutputStreamWriter writerToc = new OutputStreamWriter(outToc, Charset.forName("UTF-8"));
164 convertTree(writerToc);
170 convertFileList(writerFiles);
175 } catch (IOException e) {
176 fatalExceptionOccured(e);
180 private void convertFileList(Appendable buffer) throws IOException {
181 buffer.append("[OPTIONS]\n");
182 buffer.append("Binary TOC=Yes\n");
183 buffer.append("Compatibility=1.1 or later\n");
184 buffer.append("Compiled file=htmlhelp.chm\n");
185 buffer.append("Contents file=");
186 buffer.append(outName);
187 buffer.append("toc.hhc\n");
188 buffer.append("Default Window=Main\n");
189 buffer.append("Default topic=");
190 buffer.append(outName);
191 buffer.append("index.html\n");
192 buffer.append("Display compile progress=Yes\n");
193 buffer.append("Full-text search=Yes\n");
194 buffer.append("Language=");
195 buffer.append(LocaleToLCID.convert(language));
196 buffer.append("\nTitle=");
197 buffer.append(bookTitle);
198 buffer.append("\nEnhanced decompilation=No\n\n");
199 buffer.append("[WINDOWS]\n");
200 buffer.append("Main=\"");
201 buffer.append(bookTitle);
202 buffer.append("\",\",");
203 buffer.append(outName);
204 buffer.append("toc.hhc\",,\"");
205 buffer.append(outName);
206 buffer.append("index.html\",\"");
207 buffer.append(outName);
208 buffer.append("index.html\",,,,,0x2520,,0x603006,,,,,,,,0\n\n[FILES]\n");
209 for (String s : filesList) {
210 buffer.append(outName);
219 protected String makeRemoteLink(String link) {
220 return docWebsite + link;
223 private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf, Appendable buffer) throws IOException {
224 if (leaf.children != null) {
225 for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
226 buffer.append("<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
227 buffer.append(tocitem.get(c.id));
228 buffer.append("\">\n<param name=\"Local\" value=\"");
229 buffer.append(outName);
230 buffer.append(mapId.get(c.id));
231 buffer.append("\">\n</OBJECT>\n</LI>\n");
232 if (c.children != null) {
233 buffer.append("<UL>\n");
234 convertTreeId(c, buffer);
235 buffer.append("</UL>\n");
241 private void convertTree(Appendable buffer) throws IOException {
242 buffer.append("<HTML>\n<HEAD>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n</HEAD>\n<BODY>\n<OBJECT type=\"text/site properties\">\n<param name=\"ImageType\" value=\"Folder\">\n</OBJECT>\n");
243 buffer.append("<UL>\n<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
244 buffer.append(bookTitle);
245 buffer.append("\">\n<param name=\"Local\" value=\"");
246 buffer.append(outName);
247 buffer.append("index.html\">\n</OBJECT>\n</LI>\n<UL>\n");
248 convertTreeId(tree, buffer);
249 buffer.append("</UL>\n</UL>\n</BODY>\n</HTML>");