a3d861780680f09a0d1b17449143b8d291203fb0
[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  * 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.Iterator;
21
22 import org.xml.sax.SAXException;
23
24 import org.scilab.modules.commons.ScilabConstants;
25 import org.scilab.modules.helptools.image.ImageConverter;
26
27 /**
28  * Class to convert DocBook to JavaHelp
29  * @author Calixte DENIZET
30  */
31 public class JavaHelpDocbookTagConverter extends HTMLDocbookTagConverter {
32
33     private static final String XMLSTRING = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n";
34
35     private StringBuilder buffer = new StringBuilder(8192);
36
37     /**
38      * Constructor
39      * @param inName the name of the input stream
40      * @param outName the output directory
41      * @param primConf the file containing the primitives of Scilab
42      * @param macroConf the file containing the macros of Scilab
43      * @param template the template to use
44      * @param version the version
45      * @param imageDir the image directory (relative to outName)
46      * @param isToolbox is true when compile a toolbox' help
47      * @param urlBase the base url for external link
48      */
49     public JavaHelpDocbookTagConverter(String inName, String outName, String[] primConf, String[] macroConf, String template, String version, String imageDir, boolean isToolbox, String urlBase, String language) throws IOException, SAXException {
50         super(inName, outName, primConf, macroConf, template, version, imageDir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.JAVAHELP);
51         if (!isToolbox) {
52             this.outImages = ScilabConstants.SCI.getPath() + "/modules/helptools/images";
53             File dir = new File(this.outImages);
54             if (!dir.exists()) {
55                 dir.mkdir();
56             }
57             ImageConverter.loadMD5s(ScilabConstants.SCI.getPath() + "/modules/helptools/etc");
58         }
59         prependToProgramListing = "<table border=\"0\" width=\"100%\"><tr><td width=\"98%\">";
60         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>";
61         appendForExecToProgramListing = "</td><td valign=\"top\"><a href=\"scilab://scilab.execexample/\"><img src=\"" + getBaseImagePath() + "ScilabExecute.png\" border=\"0\"/></a></td><td></td></tr></table>";
62     }
63
64     /**
65      * {@inheritDoc}
66      */
67     public void endDocument() throws SAXException {
68         try {
69             FileOutputStream outToc = new FileOutputStream("jhelptoc.xml");
70             FileOutputStream outMap = new FileOutputStream("jhelpmap.jhm");
71             FileOutputStream outSet = new FileOutputStream("jhelpset.hs");
72             FileOutputStream outIndex = new FileOutputStream("jhelpidx.xml");
73             OutputStreamWriter writerIndex = new OutputStreamWriter(outIndex, Charset.forName("UTF-8"));
74             OutputStreamWriter writerSet = new OutputStreamWriter(outSet, Charset.forName("UTF-8"));
75             OutputStreamWriter writerMap = new OutputStreamWriter(outMap, Charset.forName("UTF-8"));
76             OutputStreamWriter writerToc = new OutputStreamWriter(outToc, Charset.forName("UTF-8"));
77             writerMap.append(XMLSTRING);
78             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");
79             writerMap.append(convertMapId());
80             writerMap.flush();
81             writerMap.close();
82             outMap.flush();
83             outMap.close();
84
85             writerToc.append(XMLSTRING);
86             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");
87             writerToc.append(convertTocItem());
88             writerToc.flush();
89             writerToc.close();
90             outToc.flush();
91             outToc.close();
92
93             writerSet.append(XMLSTRING);
94             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);
95             writerSet.append(str);
96             writerSet.flush();
97             writerSet.close();
98             outSet.flush();
99             outSet.close();
100
101             writerIndex.append(XMLSTRING);
102             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\"/>");
103             writerIndex.flush();
104             writerIndex.close();
105             outIndex.flush();
106             outIndex.close();
107
108             if (!isToolbox) {
109                 ImageConverter.saveMD5s(ScilabConstants.SCI.getPath() + "/modules/helptools/etc");
110             }
111         } catch (IOException e) {
112             fatalExceptionOccured(e);
113         }
114     }
115
116     /**
117      * {@inheritDoc}
118      */
119     protected String makeRemoteLink(String link) {
120         return "file://SCI/modules/" + link;
121     }
122
123     public String getBaseImagePath() {
124         if (isToolbox) {
125             return super.getBaseImagePath();
126         }
127
128         return "jarsci:";
129     }
130
131     private String convertMapId() {
132         buffer.setLength(0);
133         buffer.append("<map version=\"1.0\">\n<mapID target=\"index\" url=\"index.html\"/>\n");
134         if (!isToolbox) {
135             buffer.append("<mapID target=\"whatsnew\" url=\"ScilabHomePage.html\"/>\n");
136         }
137         Iterator<String> iter = mapId.keySet().iterator();
138         while (iter.hasNext()) {
139             String id = iter.next();
140             buffer.append("<mapID target=\"");
141             buffer.append(id);
142             buffer.append("\" url=\"");
143             buffer.append(mapId.get(id));
144             buffer.append("\"/>\n");
145         }
146         buffer.append("</map>");
147
148         return buffer.toString();
149     }
150
151     private void convertTreeId(HTMLDocbookLinkResolver.TreeId leaf) {
152         if (leaf.children != null) {
153             for (HTMLDocbookLinkResolver.TreeId c : leaf.children) {
154                 buffer.append("<tocitem target=\"");
155                 buffer.append(c.id);
156                 buffer.append("\" text=\"");
157                 buffer.append(replaceEntity(tocitem.get(c.id)));
158                 if (c.children == null) {
159                     buffer.append("\"/>\n");
160                 } else {
161                     buffer.append("\">\n");
162                     convertTreeId(c);
163                     buffer.append("</tocitem>\n");
164                 }
165             }
166         }
167     }
168
169     private String convertTocItem() {
170         buffer.setLength(0);
171         buffer.append("<toc version=\"1.0\">\n<tocitem target=\"index\" text=\"" + bookTitle + "\">\n");
172         if (!isToolbox) {
173             buffer.append("<tocitem target=\"whatsnew\" text=\"Scilab Home\"/>\n");
174         }
175         convertTreeId(tree);
176         buffer.append("</tocitem>\n</toc>");
177
178         return buffer.toString();
179     }
180 }