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