ea6ef00574c7ca035149b6c8499804870e3098b1
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / SciDocMain.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
17 import org.xml.sax.SAXException;
18
19 import org.scilab.modules.commons.ScilabCommonsUtils;
20 import org.scilab.modules.commons.ScilabConstants;
21 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
22 import org.scilab.modules.helptools.external.HTMLMathMLHandler;
23 import org.scilab.modules.helptools.external.HTMLScilabHandler;
24 import org.scilab.modules.helptools.external.HTMLSVGHandler;
25 import org.scilab.modules.helptools.image.ScilabImageConverter;
26
27 /**
28  * Class to convert DocBook to HTML
29  * @author Calixte DENIZET
30  */
31 public final class SciDocMain {
32
33     private static final String SCI = ScilabConstants.SCI.getPath();
34
35     private String outputDirectory;
36     private String language;
37     private String format;
38     private String template;
39     private String version;
40     private String imagedir;
41     private String[] sciprim;
42     private String[] scimacro;
43     private boolean isToolbox;
44
45     /**
46      * Set the directory where files must be exported
47      * Note that directory is created
48      *
49      * @param outputDirectoryectory The path to the directory
50      * @return If the directory exists
51      */
52     public boolean setOutputDirectory(String outputDirectory) {
53         File directory = new File(outputDirectory);
54         if (!directory.isDirectory()) {
55             if (!directory.mkdirs()) {
56                 return false;
57             }
58         }
59         this.outputDirectory = new File(outputDirectory).getAbsolutePath();
60         return true;
61     }
62
63     /**
64      * Defines the language
65      *
66      * @param language the language (xx_XX ex: en_US, fr_FR)
67      */
68     public void setWorkingLanguage(String language) {
69         this.language = language;
70     }
71
72     /**
73      * @param isToolbox must be true if we compile a toolbox doc
74      */
75     public void setIsToolbox(boolean isToolbox) {
76         this.isToolbox = isToolbox;
77     }
78
79     /**
80      * Defines the export format
81      * @param format the format (among the list CHM, HTML, PDF, JH, PS)
82      */
83     public void setExportFormat(String format) {
84         this.format = format;
85     }
86
87     /* Stylesheet is useless and just kept to keep the consistency with
88      * builddoc V1 */
89     public String process(String sourceDoc, String styleSheet)  {
90         SciDocConfiguration conf = new SciDocConfiguration();
91         template = conf.getTemplate(format.toLowerCase());
92         /* TODO: make this file generated at build time of Scilab */
93         sciprim = conf.getBuiltins();
94         scimacro = conf.getMacros();
95         version = conf.getVersion();
96         imagedir = ".";//the path must be relative to outputDirectory
97         String imageOut = outputDirectory;
98         String fileToExec = null;
99
100         if (!new File(sourceDoc).isFile()) {
101             System.err.println("Could not find master document: " + sourceDoc);
102             return null;
103         }
104
105         if (!new File(template).isFile()) {
106             System.err.println("Could not find template document: " + template);
107             return null;
108         }
109
110         try {
111             DocbookTagConverter converter = null;
112             String urlBase = null;
113
114             if (format.equalsIgnoreCase("javahelp")) {
115                 converter = new JavaHelpDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, "scilab://", language);
116                 if (!isToolbox) {
117                     imageOut = ((JavaHelpDocbookTagConverter) converter).outImages;
118                 }
119             } else {
120                 if (isToolbox) {
121                     urlBase = conf.getWebSiteURL() + language + "/";
122                 }
123                 if (format.equalsIgnoreCase("html")) {
124                     converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.HTML);
125                 } else if (format.equalsIgnoreCase("web")) {
126                     converter = new HTMLDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, isToolbox, urlBase, language, HTMLDocbookTagConverter.GenerationType.WEB);
127                 } else if (format.equalsIgnoreCase("chm")) {
128                     converter = new CHMDocbookTagConverter(sourceDoc, outputDirectory, sciprim, scimacro, template, version, imagedir, conf.getWebSiteURL(), isToolbox, urlBase, language);
129                 }
130             }
131
132             converter.registerExternalXMLHandler(HTMLMathMLHandler.getInstance(imageOut, imagedir));
133             converter.registerExternalXMLHandler(HTMLSVGHandler.getInstance(imageOut, imagedir));
134             converter.registerExternalXMLHandler(HTMLScilabHandler.getInstance(imageOut, imagedir));
135             converter.convert();
136
137             HTMLMathMLHandler.clean();
138             HTMLSVGHandler.clean();
139             HTMLScilabHandler.clean();
140
141             fileToExec = ScilabImageConverter.getFileWithScilabCode();
142
143             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/scilab_code.css"), new File(outputDirectory + "/scilab_code.css"));
144             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/xml_code.css"), new File(outputDirectory + "/xml_code.css"));
145             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/c_code.css"), new File(outputDirectory + "/c_code.css"));
146             ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/css/style.css"), new File(outputDirectory + "/style.css"));
147             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("media-playback-start")), new File(imageOut + "/ScilabExecute.png"));
148             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("accessories-text-editor")), new File(imageOut + "/ScilabEdit.png"));
149             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-information")), new File(imageOut + "/ScilabNote.png"));
150             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-warning")), new File(imageOut + "/ScilabWarning.png"));
151             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-warning")), new File(imageOut + "/ScilabCaution.png"));
152             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("dialog-information")), new File(imageOut + "/ScilabTip.png"));
153             ScilabCommonsUtils.copyFile(new File(ScilabSwingUtilities.findIcon("emblem-important")), new File(imageOut + "/ScilabImportant.png"));
154             if (format.equalsIgnoreCase("javahelp")) {
155                 if (!isToolbox) {
156                     ScilabCommonsUtils.copyFile(new File(SCI + "/modules/helptools/data/pages/error.html"), new File(outputDirectory + "/ScilabErrorPage.html"));
157                     File homepage = new File(SCI + "/modules/helptools/data/pages/homepage-" + language + ".html");
158                     if (!homepage.isFile()) {
159                         /* could not find the localized homepage. Switch to english */
160                         homepage = new File(SCI + "/modules/helptools/data/pages/homepage-en_US.html");
161                     }
162                     ScilabCommonsUtils.copyFile(homepage, new File(outputDirectory + "/ScilabHomePage.html"));
163
164                     File homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-" + language + ".png");
165                     if (!homepageImage.isFile()) {
166                         homepageImage = new File(SCI + "/modules/helptools/data/pages/ban-en_US.png");
167                     }
168                     ScilabCommonsUtils.copyFile(homepageImage, new File(imageOut + "/ban_en_US.png"));
169                 }
170
171                 if (fileToExec == null) {
172                     generateJavahelp(outputDirectory, language, isToolbox);
173                 }
174             }
175
176         } catch (SAXException e) {
177             System.err.println("An error occurred during the conversion:");
178             System.err.println(e.toString());
179         } catch (Exception e) {
180             System.err.println("An error occurred during the conversion:\n");
181             e.printStackTrace();
182         }
183
184         return fileToExec;
185     }
186
187     public static void generateJavahelp(String outputDirectory, String language, boolean isToolbox) {
188         BuildJavaHelp.buildJavaHelp(outputDirectory, language);
189         if (!isToolbox) {
190             BuildJavaHelp.buildJarImages(SCI + "/modules/helptools/images", SCI + "/modules/helptools/jar");
191         }
192     }
193 }