Update files for Scilab 6.0.0 beta 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         try {
104             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             writerMap.append(XMLSTRING);
113             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");
114             writerMap.append(convertMapId());
115             writerMap.flush();
116             writerMap.close();
117             outMap.flush();
118             outMap.close();
119
120             writerToc.append(XMLSTRING);
121             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");
122             writerToc.append(convertTocItem());
123             writerToc.flush();
124             writerToc.close();
125             outToc.flush();
126             outToc.close();
127
128             writerSet.append(XMLSTRING);
129             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);
130             writerSet.append(str);
131             writerSet.flush();
132             writerSet.close();
133             outSet.flush();
134             outSet.close();
135
136             writerIndex.append(XMLSTRING);
137             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\"/>");
138             writerIndex.flush();
139             writerIndex.close();
140             outIndex.flush();
141             outIndex.close();
142
143             if (!isToolbox) {
144                 getImageConverter().saveMD5s(ScilabConstants.SCI.getPath() + "/modules/helptools/etc");
145             }
146         } catch (IOException e) {
147             fatalExceptionOccurred(e);
148         }
149     }
150
151     /**
152      * {@inheritDoc}
153      */
154     protected String makeRemoteLink(String link) {
155         return "file://SCI/modules/" + link;
156     }
157
158     public String getBaseImagePath() {
159         if (isToolbox) {
160             return super.getBaseImagePath();
161         }
162
163         return "jarsci:";
164     }
165
166     private String convertMapId() {
167         buffer.setLength(0);
168         buffer.append("<map version=\"1.0\">\n<mapID target=\"index\" url=\"index.html\"/>\n");
169         if (!isToolbox) {
170             buffer.append("<mapID target=\"whatsnew\" url=\"ScilabHomePage.html\"/>\n");
171         }
172         Iterator<String> iter = mapId.keySet().iterator();
173         while (iter.hasNext()) {
174             String id = iter.next();
175             buffer.append("<mapID target=\"");
176             buffer.append(id);
177             buffer.append("\" url=\"");
178             buffer.append(mapId.get(id));
179             buffer.append("\"/>\n");
180         }
181         buffer.append("</map>");
182
183         return buffer.toString();
184     }
185
186     private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf) {
187         if (leaf.children != null) {
188             for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
189                 buffer.append("<tocitem target=\"");
190                 buffer.append(c.id);
191                 buffer.append("\" text=\"");
192                 buffer.append(tocitem.get(c.id));
193                 if (c.children == null) {
194                     buffer.append("\"/>\n");
195                 } else {
196                     buffer.append("\">\n");
197                     convertTreeId(c);
198                     buffer.append("</tocitem>\n");
199                 }
200             }
201         }
202     }
203
204     private String convertTocItem() {
205         buffer.setLength(0);
206         buffer.append("<toc version=\"1.0\">\n<tocitem target=\"index\" text=\"" + bookTitle + "\">\n");
207         if (!isToolbox) {
208             buffer.append("<tocitem target=\"whatsnew\" text=\"Scilab Home\"/>\n");
209         }
210         convertTreeId(tree);
211         buffer.append("</tocitem>\n</toc>");
212
213         return buffer.toString();
214     }
215 }