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