Update jflex to 1.8.2
[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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 package org.scilab.modules.helptools;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.nio.charset.Charset;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.xml.sax.SAXException;
27 import org.scilab.modules.helptools.image.ImageConverter;
28 import org.scilab.modules.localization.LocaleToLCID;
29
30 /**
31  * Class to convert DocBook to CHM
32  * @author Calixte DENIZET
33  */
34 public class CHMDocbookTagConverter extends HTMLDocbookTagConverter {
35
36     private StringBuilder buffer = new StringBuilder(8192);
37     private String outName;
38     private List<String> filesList = new ArrayList<String>();
39     private String docWebsite;
40
41
42     /**
43      * Constructor
44      * @param inName the name of the input stream
45      * @param outName the output directory
46      * @param imgConvert The associated image converter
47      */
48     public CHMDocbookTagConverter(String inName, SciDocMain sciDocMain, ImageConverter imgConvert) throws IOException, SAXException {
49         super(inName, sciDocMain, imgConvert);
50         this.outName = new File(outName).getCanonicalPath() + File.separator;
51         this.docWebsite = sciDocMain.getConf().getWebSiteURL();
52     }
53
54     /**
55      * {@inheritDoc}
56      */
57     public void createHTMLFile(String id, String fileName, String subtitle, String contents) {
58         super.createHTMLFile(id, fileName, subtitle, contents);
59         filesList.add(fileName);
60     }
61
62     /**
63      * {@inheritDoc}
64      */
65     public String makePrevious(String id) {
66         buffer.setLength(0);
67         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
68         if (leaf == null) {
69             return "<link rel=\"prev\" href=\"\" title=\"\">";
70         }
71         HTMLDocbookLinkResolver.TreeId prev = leaf.getPrevious();
72         if (prev.parent != null) {
73             buffer.append("<link rel=\"prev\" href=\"");
74             buffer.append(mapId.get(prev.id));
75             buffer.append("\" title=\"");
76             buffer.append(tocitem.get(prev.id));
77             buffer.append("\">");
78
79             return buffer.toString();
80         }
81
82         return "<link rel=\"prev\" href=\"\" title=\"\">";
83     }
84
85     /**
86      * {@inheritDoc}
87      */
88     public String makeTop(String id) {
89         buffer.setLength(0);
90         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
91         if (leaf == null) {
92             return "<link rel=\"up\" href=\"\" title=\"\">";
93         }
94
95         leaf = leaf.parent;
96         if (leaf != null) {
97             buffer.append("<link rel=\"up\" href=\"");
98             if (!leaf.isRoot()) {
99                 buffer.append(mapId.get(leaf.id));
100                 buffer.append("\" title=\"");
101                 buffer.append(tocitem.get(leaf.id));
102             } else {
103                 buffer.append(indexFilename);
104                 buffer.append("\" title=\"");
105                 buffer.append(bookTitle);
106             }
107             buffer.append("\">");
108
109             return buffer.toString();
110         }
111
112         return "<link rel=\"up\" href=\"\" title=\"\">";
113     }
114
115     /**
116      * {@inheritDoc}
117      */
118     public String makeNext(String id) {
119         buffer.setLength(0);
120         HTMLDocbookLinkResolver.TreeId leaf = mapTreeId.get(id);
121         if (leaf == null) {
122             return "<link rel=\"next\" href=\"\" title=\"\">";
123         }
124         HTMLDocbookLinkResolver.TreeId next = leaf.getNext();
125         if (next != null) {
126             buffer.append("<link rel=\"next\" href=\"");
127             buffer.append(mapId.get(next.id));
128             buffer.append("\" title=\"");
129             buffer.append(tocitem.get(next.id));
130             buffer.append("\">");
131
132             return buffer.toString();
133         }
134
135         return "<link rel=\"next\" href=\"\" title=\"\">";
136     }
137
138     /**
139      * {@inheritDoc}
140      */
141     public String makeStart(String id) {
142         buffer.setLength(0);
143         buffer.append("<link rel=\"start\" href=\"");
144         buffer.append(indexFilename);
145         buffer.append("\" title=\"");
146         buffer.append(bookTitle);
147         buffer.append("\">");
148
149         return buffer.toString();
150     }
151
152     /**
153      * {@inheritDoc}
154      */
155     public void endDocument() throws SAXException {
156         try ( FileOutputStream outToc = new FileOutputStream("toc.hhc");
157               FileOutputStream outFiles = new FileOutputStream("htmlhelp.hhp");
158               OutputStreamWriter writerFiles = new OutputStreamWriter(outFiles, Charset.forName("UTF-8"));
159               OutputStreamWriter writerToc = new OutputStreamWriter(outToc, Charset.forName("UTF-8")) ) {
160
161              convertTree(writerToc);
162              writerToc.flush();
163              outToc.flush();
164
165              convertFileList(writerFiles);
166              writerFiles.flush();
167              outFiles.flush();
168         } catch (IOException e) {
169             fatalExceptionOccurred(e);
170         }
171     }
172
173     private void convertFileList(Appendable buffer) throws IOException {
174         buffer.append("[OPTIONS]\n");
175         buffer.append("Binary TOC=Yes\n");
176         buffer.append("Compatibility=1.1 or later\n");
177         buffer.append("Compiled file=htmlhelp.chm\n");
178         buffer.append("Contents file=");
179         buffer.append(outName);
180         buffer.append("toc.hhc\n");
181         buffer.append("Default Window=Main\n");
182         buffer.append("Default topic=");
183         buffer.append(outName);
184         buffer.append("index.html\n");
185         buffer.append("Display compile progress=Yes\n");
186         buffer.append("Full-text search=Yes\n");
187         buffer.append("Language=");
188         buffer.append(LocaleToLCID.convert(language));
189         buffer.append("\nTitle=");
190         buffer.append(bookTitle);
191         buffer.append("\nEnhanced decompilation=No\n\n");
192         buffer.append("[WINDOWS]\n");
193         buffer.append("Main=\"");
194         buffer.append(bookTitle);
195         buffer.append("\",\",");
196         buffer.append(outName);
197         buffer.append("toc.hhc\",,\"");
198         buffer.append(outName);
199         buffer.append("index.html\",\"");
200         buffer.append(outName);
201         buffer.append("index.html\",,,,,0x2520,,0x603006,,,,,,,,0\n\n[FILES]\n");
202         for (String s : filesList) {
203             buffer.append(outName);
204             buffer.append(s);
205             buffer.append("\n");
206         }
207     }
208
209     /**
210      * {@inheritDoc}
211      */
212     protected String makeRemoteLink(String link) {
213         return docWebsite + link;
214     }
215
216     private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf, Appendable buffer) throws IOException {
217         if (leaf.children != null) {
218             for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
219                 buffer.append("<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
220                 buffer.append(tocitem.get(c.id));
221                 buffer.append("\">\n<param name=\"Local\" value=\"");
222                 buffer.append(outName);
223                 buffer.append(mapId.get(c.id));
224                 buffer.append("\">\n</OBJECT>\n</LI>\n");
225                 if (c.children != null) {
226                     buffer.append("<UL>\n");
227                     convertTreeId(c, buffer);
228                     buffer.append("</UL>\n");
229                 }
230             }
231         }
232     }
233
234     private void convertTree(Appendable buffer) throws IOException {
235         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");
236         buffer.append("<UL>\n<LI><OBJECT type=\"text/sitemap\">\n<param name=\"Name\" value=\"");
237         buffer.append(bookTitle);
238         buffer.append("\">\n<param name=\"Local\" value=\"");
239         buffer.append(outName);
240         buffer.append("index.html\">\n</OBJECT>\n</LI>\n<UL>\n");
241         convertTreeId(tree, buffer);
242         buffer.append("</UL>\n</UL>\n</BODY>\n</HTML>");
243     }
244 }