Update jflex to 1.8.2
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / JavaHelpDocbookTagConverter.java
1 /*
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.Iterator;
24
25 import org.scilab.modules.commons.ScilabCommonsUtils;
26 import org.scilab.modules.commons.ScilabConstants;
27 import org.scilab.modules.commons.gui.FindIconHelper;
28 import org.scilab.modules.helptools.image.ImageConverter;
29 import org.xml.sax.SAXException;
30
31 /**
32  * Class to convert DocBook to JavaHelp
33  * @author Calixte DENIZET
34  */
35 public class JavaHelpDocbookTagConverter extends HTMLDocbookTagConverter {
36
37     private static final String XMLSTRING = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n";
38
39     private StringBuilder buffer = new StringBuilder(8192);
40
41     /**
42      * Constructor
43      * @param inName the name of the input stream
44      * @param sciDocMain provide useful doc generation properties
45      * @param imgConvert the shared image converter for all generation
46      */
47     public JavaHelpDocbookTagConverter(String inName, SciDocMain sciDocMain, ImageConverter imgConvert) throws IOException, SAXException {
48         super(inName, sciDocMain, imgConvert);
49
50         urlBase = "scilab://";
51         linkToTheWeb = false;
52
53         if (!isToolbox) {
54             this.outImages = ScilabConstants.SCI.getPath() + "/modules/helptools/images";
55             File dir = new File(this.outImages);
56             if (!dir.exists()) {
57                 dir.mkdir();
58             }
59         }
60
61         prependToProgramListing = "<table border=\"0\" width=\"100%\"><tr><td width=\"98%\">";
62         appendToProgramListing = "</td><td valign=\"top\"><a href=\"scilab://scilab.execexample/\"><img src=\"" + getBaseImagePath() + "ScilabExecute.png\" border=\"0\"/></a></td><td valign=\"top\"><a href=\"scilab://scilab.editexample/\"><img src=\"" + getBaseImagePath() + "ScilabEdit.png\" border=\"0\"/></a></td><td></td></tr></table>";
63         appendForExecToProgramListing = "</td><td valign=\"top\"><a href=\"scilab://scilab.execexample/\"><img src=\"" + getBaseImagePath() + "ScilabExecute.png\" border=\"0\"/></a></td><td></td></tr></table>";
64     }
65
66     @Override
67     public void install() {
68         super.install();
69
70         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("media-playback-start")), new File(outImages + "/ScilabExecute.png"));
71         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("accessories-text-editor")), new File(outImages + "/ScilabEdit.png"));
72         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("dialog-information")), new File(outImages + "/ScilabNote.png"));
73         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("dialog-warning")), new File(outImages + "/ScilabWarning.png"));
74         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("dialog-warning")), new File(outImages + "/ScilabCaution.png"));
75         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("dialog-information")), new File(outImages + "/ScilabTip.png"));
76         ScilabCommonsUtils.copyFile(new File(FindIconHelper.findIcon("emblem-important")), new File(outImages + "/ScilabImportant.png"));
77         if (!isToolbox) {
78             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/pages/error.html"), new File(outName + "/ScilabErrorPage.html"));
79             File homepage = new File(SCI + "/modules/helptools/data/pages/homepage-" + language + ".html");
80             if (!homepage.isFile()) {
81                 /* could not find the localized homepage. Switch to english */
82                 homepage = new File(SCI + "/modules/helptools/data/pages/homepage-en_US.html");
83             }
84             ScilabCommonsUtils.copyFile(homepage, new File(outName + "/ScilabHomePage.html"));
85
86             File changes = new File(SCI + "/modules/helptools/data/pages/CHANGES.html");
87             if (changes.isFile()) {
88                 ScilabCommonsUtils.copyFile(changes, new File(outName + "/CHANGES.html"));
89             }
90
91             File homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-" + language + ".png");
92             if (!homepageImage.isFile()) {
93                 homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-en_US.png");
94             }
95             ScilabCommonsUtils.copyFile(homepageImage, new File(outImages + "/ban_en_US.png"));
96         }
97     }
98
99     /**
100      * {@inheritDoc}
101      */
102     public void endDocument() throws SAXException {
103                 
104         try ( FileOutputStream outToc = new FileOutputStream("jhelptoc.xml");
105               FileOutputStream outMap = new FileOutputStream("jhelpmap.jhm");
106               FileOutputStream outSet = new FileOutputStream("jhelpset.hs");
107               FileOutputStream outIndex = new FileOutputStream("jhelpidx.xml");
108               OutputStreamWriter writerIndex = new OutputStreamWriter(outIndex, Charset.forName("UTF-8"));
109               OutputStreamWriter writerSet = new OutputStreamWriter(outSet, Charset.forName("UTF-8"));
110               OutputStreamWriter writerMap = new OutputStreamWriter(outMap, Charset.forName("UTF-8"));
111               OutputStreamWriter writerToc = new OutputStreamWriter(outToc, Charset.forName("UTF-8")))
112             {
113                                   
114             writerMap.append(XMLSTRING);
115             writerMap.append("<!DOCTYPE map PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN\" \"http://java.sun.com/products/javahelp/map_1_0.dtd\">\n");
116             writerMap.append(convertMapId());
117             writerMap.flush();
118             
119             outMap.flush();
120             
121
122             writerToc.append(XMLSTRING);
123             writerToc.append("<!DOCTYPE toc PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp TOC Version 1.0//EN\" \"http://java.sun.com/products/javahelp/toc_1_0.dtd\">\n");
124             writerToc.append(convertTocItem());
125             writerToc.flush();
126             
127             outToc.flush();
128             
129
130             writerSet.append(XMLSTRING);
131             String str = "<!DOCTYPE helpset\n  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp HelpSet Version 1.0//EN\" \"http://java.sun.com/products/javahelp/helpset_1_0.dtd\">\n<helpset version=\"1.0\">\n<title>TITLE</title>\n<maps>\n<homeID>top</homeID>\n<mapref location=\"jhelpmap.jhm\"/>\n</maps>\n<view>\n<name>TOC</name>\n<label>Table Of Contents</label>\n<type>javax.help.TOCView</type>\n<data>jhelptoc.xml</data>\n</view>\n<view>\n<name>Index</name>\n<label>Index</label>\n<type>javax.help.IndexView</type>\n<data>jhelpidx.xml</data>\n</view>\n<view>\n<name>Search</name>\n<label>Search</label>\n<type>javax.help.SearchView</type>\n<data engine=\"com.sun.java.help.search.DefaultSearchEngine\">JavaHelpSearch</data>\n</view>\n</helpset>".replaceFirst("TITLE", bookTitle);
132             writerSet.append(str);
133             writerSet.flush();
134             
135             outSet.flush();
136             
137
138             writerIndex.append(XMLSTRING);
139             writerIndex.append("<!DOCTYPE index PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Index Version 1.0//EN\" \"http://java.sun.com/products/javahelp/index_1_0.dtd\">\n<index version=\"1.0\"/>");
140             writerIndex.flush();
141             
142             outIndex.flush();
143             
144
145             if (!isToolbox) {
146                 getImageConverter().saveMD5s(ScilabConstants.SCI.getPath() + "/modules/helptools/etc");
147             }
148         } catch (IOException e) {
149             fatalExceptionOccurred(e);
150         }       
151     }
152
153     /**
154      * {@inheritDoc}
155      */
156     protected String makeRemoteLink(String link) {
157         return "file://SCI/modules/" + link;
158     }
159
160     public String getBaseImagePath() {
161         if (isToolbox) {
162             return super.getBaseImagePath();
163         }
164
165         return "jarsci:";
166     }
167
168     private String convertMapId() {
169         buffer.setLength(0);
170         buffer.append("<map version=\"1.0\">\n<mapID target=\"index\" url=\"index.html\"/>\n");
171         if (!isToolbox) {
172             buffer.append("<mapID target=\"whatsnew\" url=\"ScilabHomePage.html\"/>\n");
173         }
174         Iterator<String> iter = mapId.keySet().iterator();
175         while (iter.hasNext()) {
176             String id = iter.next();
177             buffer.append("<mapID target=\"");
178             buffer.append(id);
179             buffer.append("\" url=\"");
180             buffer.append(mapId.get(id));
181             buffer.append("\"/>\n");
182         }
183         buffer.append("</map>");
184
185         return buffer.toString();
186     }
187
188     private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf) {
189         if (leaf.children != null) {
190             for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
191                 buffer.append("<tocitem target=\"");
192                 buffer.append(c.id);
193                 buffer.append("\" text=\"");
194                 buffer.append(tocitem.get(c.id));
195                 if (c.children == null) {
196                     buffer.append("\"/>\n");
197                 } else {
198                     buffer.append("\">\n");
199                     convertTreeId(c);
200                     buffer.append("</tocitem>\n");
201                 }
202             }
203         }
204     }
205
206     private String convertTocItem() {
207         buffer.setLength(0);
208         buffer.append("<toc version=\"1.0\">\n<tocitem target=\"index\" text=\"" + bookTitle + "\">\n");
209         if (!isToolbox) {
210             buffer.append("<tocitem target=\"whatsnew\" text=\"Scilab Home\"/>\n");
211         }
212         convertTreeId(tree);
213         buffer.append("</tocitem>\n</toc>");
214
215         return buffer.toString();
216     }
217 }