d216c50daa2bc1f67d6d7a3bd762c9867d14c8c3
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / CHMDocbookTagConverter.java
1 /*9
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - 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;
14
15 import java.io.File;
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;
22
23 import org.xml.sax.SAXException;
24
25 import org.scilab.modules.localization.LocaleToLCID;
26
27 /**
28  * Class to convert DocBook to CHM
29  * @author Calixte DENIZET
30  */
31 public class CHMDocbookTagConverter extends HTMLDocbookTagConverter {
32
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;
38
39     /**
40      * Constructor
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', ...)
51      */
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;
57     }
58
59     /**
60      * {@inheritDoc}
61      */
62     public void createHTMLFile(String id, String fileName, String subtitle, String contents) {
63         super.createHTMLFile(id, fileName, subtitle, contents);
64         filesList.add(fileName);
65     }
66
67     /**
68      * {@inheritDoc}
69      */
70     public String makePrevious(String id) {
71         buffer.setLength(0);
72         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
73         if (leaf == null) {
74             return "<link rel=\"prev\" href=\"\" title=\"\">";
75         }
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));
82             buffer.append("\">");
83
84             return buffer.toString();
85         }
86
87         return "<link rel=\"prev\" href=\"\" title=\"\">";
88     }
89
90     /**
91      * {@inheritDoc}
92      */
93     public String makeTop(String id) {
94         buffer.setLength(0);
95         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
96         if (leaf == null) {
97             return "<link rel=\"up\" href=\"\" title=\"\">";
98         }
99
100         leaf = leaf.parent;
101         if (leaf != null) {
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));
107             } else {
108                 buffer.append(indexFilename);
109                 buffer.append("\" title=\"");
110                 buffer.append(bookTitle);
111             }
112             buffer.append("\">");
113
114             return buffer.toString();
115         }
116
117         return "<link rel=\"up\" href=\"\" title=\"\">";
118     }
119
120     /**
121      * {@inheritDoc}
122      */
123     public String makeNext(String id) {
124         buffer.setLength(0);
125         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
126         if (leaf == null) {
127             return "<link rel=\"next\" href=\"\" title=\"\">";
128         }
129         HTMLDocbookLinkResolver.TreeId next = leaf.getNext();
130         if (next != null) {
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("\">");
136
137             return buffer.toString();
138         }
139
140         return "<link rel=\"next\" href=\"\" title=\"\">";
141     }
142
143     /**
144      * {@inheritDoc}
145      */
146     public String makeStart(String id) {
147         buffer.setLength(0);
148         buffer.append("<link rel=\"start\" href=\"");
149         buffer.append(indexFilename);
150         buffer.append("\" title=\"");
151         buffer.append(bookTitle);
152         buffer.append("\">");
153
154         return buffer.toString();
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     public void endDocument() throws SAXException {
161         try {
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);
167             writerToc.flush();
168             writerToc.close();
169             outToc.flush();
170             outToc.close();
171
172             convertFileList(writerFiles);
173             writerFiles.flush();
174             writerFiles.close();
175             outFiles.flush();
176             outFiles.close();
177         } catch (IOException e) {
178             fatalExceptionOccured(e);
179         }
180     }
181
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);
213             buffer.append(s);
214             buffer.append("\n");
215         }
216     }
217
218     /**
219      * {@inheritDoc}
220      */
221     protected String makeRemoteLink(String link) {
222         return docWebsite + link;
223     }
224
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");
238                 }
239             }
240         }
241     }
242
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>");
252     }
253 }