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