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 language;
37 private String docWebsite;
41 * @param inName the name of the input stream
42 * @param outName the output directory
43 * @param primConf the file containing the primitives of Scilab
44 * @param macroConf the file containing the macros of Scilab
45 * @param template the template to use
46 * @param version the version
47 * @param imageDir the image directory (relative to outName)
48 * @param isToolbox is true when compile a toolbox' help
49 * @param urlBase the base url for external link
50 * @param language the language to use ('en_US', 'fr_FR', ...)
52 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 {
53 super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, HTMLDocbookTagConverter.GenerationType.CHM);
54 this.outName = new File(outName).getCanonicalPath() + File.separator;
55 this.language = language;
56 this.docWebsite = docWebsite;
62 public void createHTMLFile(String id, String fileName, String subtitle, String contents) {
63 super.createHTMLFile(id, fileName, subtitle, contents);
64 filesList.add(fileName);
70 public String makePrevious(String id) {
72 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
74 return "<link rel=\"prev\" href=\"\" title=\"\">";
76 HTMLDocbookLinkResolver.TreeId prev = leaf.getPrevious();
77 if (prev.parent != null) {
78 buffer.append("<link rel=\"prev\" href=\"");
79 buffer.append(mapId.get(prev.id));
80 buffer.append("\" title=\"");
81 buffer.append(tocitem.get(prev.id));
84 return buffer.toString();
87 return "<link rel=\"prev\" href=\"\" title=\"\">";
93 public String makeTop(String id) {
95 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
97 return "<link rel=\"up\" href=\"\" title=\"\">";
102 buffer.append("<link rel=\"up\" href=\"");
103 if (!leaf.isRoot()) {
104 buffer.append(mapId.get(leaf.id));
105 buffer.append("\" title=\"");
106 buffer.append(tocitem.get(leaf.id));
108 buffer.append(indexFilename);
109 buffer.append("\" title=\"");
110 buffer.append(bookTitle);
112 buffer.append("\">");
114 return buffer.toString();
117 return "<link rel=\"up\" href=\"\" title=\"\">";
123 public String makeNext(String id) {
125 HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
127 return "<link rel=\"next\" href=\"\" title=\"\">";
129 HTMLDocbookLinkResolver.TreeId next = leaf.getNext();
131 buffer.append("<link rel=\"next\" href=\"");
132 buffer.append(mapId.get(next.id));
133 buffer.append("\" title=\"");
134 buffer.append(tocitem.get(next.id));
135 buffer.append("\">");
137 return buffer.toString();
140 return "<link rel=\"next\" href=\"\" title=\"\">";
146 public String makeStart(String id) {
148 buffer.append("<link rel=\"start\" href=\"");
149 buffer.append(indexFilename);
150 buffer.append("\" title=\"");
151 buffer.append(bookTitle);
152 buffer.append("\">");
154 return buffer.toString();
160 public void endDocument() throws SAXException {
162 FileOutputStream outToc = new FileOutputStream("toc.hhc");
163 FileOutputStream outFiles = new FileOutputStream("htmlhelp.hhp");
164 OutputStreamWriter writerFiles = new OutputStreamWriter(outFiles, Charset.forName("UTF-8"));
165 OutputStreamWriter writerToc = new OutputStreamWriter(outToc, Charset.forName("UTF-8"));
166 convertTree(writerToc);
172 convertFileList(writerFiles);
177 } catch (IOException e) {
178 fatalExceptionOccured(e);
182 private void convertFileList(Appendable buffer) throws IOException {
183 buffer.append("[OPTIONS]\n");
184 buffer.append("Binary TOC=Yes\n");
185 buffer.append("Compatibility=1.1 or later\n");
186 buffer.append("Compiled file=htmlhelp.chm\n");
187 buffer.append("Contents file=");
188 buffer.append(outName);
189 buffer.append("toc.hhc\n");
190 buffer.append("Default Window=Main\n");
191 buffer.append("Default topic=");
192 buffer.append(outName);
193 buffer.append("index.html\n");
194 buffer.append("Display compile progress=Yes\n");
195 buffer.append("Full-text search=Yes\n");
196 buffer.append("Language=");
197 buffer.append(LocaleToLCID.convert(language));
198 buffer.append("\nTitle=");
199 buffer.append(bookTitle);
200 buffer.append("\nEnhanced decompilation=No\n\n");
201 buffer.append("[WINDOWS]\n");
202 buffer.append("Main=\"");
203 buffer.append(bookTitle);
204 buffer.append("\",\",");
205 buffer.append(outName);
206 buffer.append("toc.hhc\",,\"");
207 buffer.append(outName);
208 buffer.append("index.html\",\"");
209 buffer.append(outName);
210 buffer.append("index.html\",,,,,0x2520,,0x603006,,,,,,,,0\n\n[FILES]\n");
211 for (String s : filesList) {
212 buffer.append(outName);
221 protected String makeRemoteLink(String link) {
222 return docWebsite + link;
225 private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf, Appendable buffer) throws IOException {
226 if (leaf.children != null) {
227 for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
228 buffer.append("<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
229 buffer.append(tocitem.get(c.id));
230 buffer.append("\">\n<param name=\"Local\" value=\"");
231 buffer.append(outName);
232 buffer.append(mapId.get(c.id));
233 buffer.append("\">\n</OBJECT>\n</LI>\n");
234 if (c.children != null) {
235 buffer.append("<UL>\n");
236 convertTreeId(c, buffer);
237 buffer.append("</UL>\n");
243 private void convertTree(Appendable buffer) throws IOException {
244 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");
245 buffer.append("<UL>\n<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
246 buffer.append(bookTitle);
247 buffer.append("\">\n<param name=\"Local\" value=\"");
248 buffer.append(outName);
249 buffer.append("index.html\">\n</OBJECT>\n</LI>\n<UL>\n");
250 convertTreeId(tree, buffer);
251 buffer.append("</UL>\n</UL>\n</BODY>\n</HTML>");