f884133df5a4063fa699a56ee4b41f7b0566ed04
[scilab.git] / scilab / modules / helptools / sci_gateway / cpp / sci_buildDocv2.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2008 - INRIA - Sylvestre LEDRU
4  *  Copyright (C) 2011 - Scilab Enterprises - Sylvestre LEDRU
5  *
6  *  This file must be used under the terms of the CeCILL.
7  *  This source file is licensed as described in the file COPYING, which
8  *  you should have received as part of this distribution.  The terms
9  *  are also available at
10  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14 #include "SciDocMain.hxx"
15 #include "GiwsException.hxx"
16 #include "BuildDocObject.hxx"
17
18 extern "C"
19 {
20     /*--------------------------------------------------------------------------*/
21 #include <string.h>
22 #include "api_scilab.h"
23 #include "gw_helptools.h"
24 #include "Scierror.h"
25 #include "setgetSCIpath.h"
26 #include "localization.h"
27 #include "setgetlanguage.h"
28 #include "getScilabJavaVM.h"
29 #include "MALLOC.h"
30 #ifdef _MSC_VER
31 #include "ConvertSlash.h"
32 #endif
33     /*--------------------------------------------------------------------------*/
34 #define PATHTOCSS "/modules/helptools/css/javahelp.css"
35 #define PATHTOBUILDDOC "/modules/helptools/build/doc/scilab_%s_help/"
36 #define PATHTOMASTERXML "/modules/helptools/master_%s_help.xml"
37 #define DEFAULTEXPORT "JH"
38 #ifdef _MSC_VER
39     static void __slashToAntislash(std::string * in)
40     {
41         size_t found = in->rfind("/");
42
43         while (found != std::string::npos)
44         {
45             in->replace(found, 1, "\\");
46             found = in->rfind("/");
47         }
48     }
49 #endif
50     /*--------------------------------------------------------------------------*/
51     int sci_buildDocv2(char *fname, unsigned long l)
52     {
53         std::string exportFormat;
54         std::string SciPath = getSCIpath(); /* Scilab path */
55         std::string masterXML;  /* Which file contains all the doc stuff */
56         std::string masterXMLTMP;
57         std::string outputDirectory;    /* Working directory */
58         std::string outputDirectoryTMP;
59         std::string language;
60         std::string styleSheet; /* the CSS */
61         SciErr sciErr;
62         int *piAddr = NULL;
63         int iRet = 0;
64
65         CheckRhs(0, 4);
66         CheckLhs(1, 1);
67
68         styleSheet = SciPath + PATHTOCSS;
69
70         if (Rhs < 1)
71         {
72             exportFormat = DEFAULTEXPORT;
73         }
74         else
75         {
76             char *pstData = NULL;
77
78             sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
79             if (sciErr.iErr)
80             {
81                 printError(&sciErr, 0);
82                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
83                 return 0;
84             }
85
86             if (!isStringType(pvApiCtx, piAddr))
87             {
88                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 1);
89                 return 0;
90                 // Wrong type string
91             }
92
93             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
94             if (iRet)
95             {
96                 freeAllocatedSingleString(pstData);
97                 return iRet;
98             }
99             exportFormat = std::string(pstData);
100             freeAllocatedSingleString(pstData);
101
102         }
103
104         if (Rhs < 3)            /* Language not provided */
105         {
106             language = getlanguage();
107         }
108         else
109         {
110             char *pstData = NULL;
111
112             sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr);
113             if (sciErr.iErr)
114             {
115                 printError(&sciErr, 0);
116                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
117                 return 0;
118             }
119
120             if (!isStringType(pvApiCtx, piAddr))
121             {
122                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 3);
123                 return 0;
124                 // Wrong type string
125             }
126
127             if (!isScalar(pvApiCtx, piAddr))
128             {
129                 language = getlanguage();
130             }
131             else
132             {
133                 iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
134                 if (iRet)
135                 {
136                     freeAllocatedSingleString(pstData);
137                     return iRet;
138                 }
139                 language = std::string(pstData);
140                 freeAllocatedSingleString(pstData);
141             }
142
143         }
144
145         if (Rhs < 2)
146         {
147             /* Update the path with the localization */
148             masterXMLTMP = std::string("/modules/helptools/master_") + language + std::string("_help.xml");
149             masterXML = SciPath + masterXMLTMP;
150         }
151         else
152         {
153             char *pstData = NULL;
154
155             sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
156             if (sciErr.iErr)
157             {
158                 printError(&sciErr, 0);
159                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
160                 return 0;
161             }
162             if (!isStringType(pvApiCtx, piAddr))
163             {
164                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 2);
165                 return 0;
166                 // Wrong type string
167             }
168
169             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
170             if (iRet)
171             {
172                 freeAllocatedSingleString(pstData);
173                 return iRet;
174             }
175             masterXML = std::string(pstData);
176             freeAllocatedSingleString(pstData);
177         }
178
179         if (Rhs == 4)
180         {
181             char *pstData = NULL;
182
183             sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr);
184             if (sciErr.iErr)
185             {
186                 printError(&sciErr, 0);
187                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
188                 return 0;
189             }
190             if (!isStringType(pvApiCtx, piAddr))
191             {
192                 Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), fname, 4);
193                 return 0;
194                 // Wrong type string
195             }
196
197             iRet = getAllocatedSingleString(pvApiCtx, piAddr, &pstData);
198             if (iRet)
199             {
200                 freeAllocatedSingleString(pstData);
201                 return iRet;
202             }
203             outputDirectory = std::string(pstData) + std::string("/scilab_") + language + std::string("_help/");
204             freeAllocatedSingleString(pstData);
205
206         }
207         else                    /* Scilab help */
208         {
209             /* Update the path with the localization */
210             outputDirectoryTMP =
211                 std::string("/modules/helptools/") + std::string(exportFormat) + std::string("/scilab_") + language + std::string("_help/");
212
213             outputDirectory = SciPath + outputDirectoryTMP;
214         }
215
216 #ifdef _MSC_VER
217         __slashToAntislash(&outputDirectory);
218         __slashToAntislash(&styleSheet);
219         __slashToAntislash(&masterXML);
220 #endif
221
222         try
223         {
224             if (exportFormat == "javaHelp" || exportFormat == "html" || exportFormat == "chm" || exportFormat == "web")
225             {
226                 org_scilab_modules_helptools::SciDocMain * doc = new org_scilab_modules_helptools::SciDocMain(getScilabJavaVM());
227
228                 if (doc->setOutputDirectory((char *)outputDirectory.c_str()))
229                 {
230                     doc->setWorkingLanguage((char *)language.c_str());
231                     doc->setExportFormat((char *)exportFormat.c_str());
232                     doc->setIsToolbox(Rhs == 4);
233                     doc->process((char *)masterXML.c_str(), (char *)styleSheet.c_str());
234                 }
235                 else
236                 {
237                     Scierror(999, _("%s: Could find or create the working directory %s.\n"), fname, outputDirectory.c_str());
238                     return FALSE;
239                 }
240                 if (doc != NULL)
241                 {
242                     delete doc;
243                 }
244
245             }
246             else
247             {
248                 org_scilab_modules_helptools::BuildDocObject * doc = new org_scilab_modules_helptools::BuildDocObject(getScilabJavaVM());
249
250                 if (doc->setOutputDirectory((char *)outputDirectory.c_str()))
251                 {
252                     doc->setWorkingLanguage((char *)language.c_str());
253                     doc->setExportFormat((char *)exportFormat.c_str());
254                     doc->process((char *)masterXML.c_str(), (char *)styleSheet.c_str());
255                 }
256                 else
257                 {
258                     Scierror(999, _("%s: Could find or create the working directory %s.\n"), fname, outputDirectory.c_str());
259                     return FALSE;
260                 }
261                 if (doc != NULL)
262                 {
263                     delete doc;
264                 }
265
266             }
267         }
268         catch(GiwsException::JniException ex)
269         {
270             Scierror(999, _("%s: Error while building documentation: %s.\n"), fname, ex.getJavaDescription().c_str());
271             Scierror(999, _("%s: Execution Java stack: %s.\n"), fname, ex.getJavaStackTrace().c_str());
272             Scierror(999,
273                      _
274                      ("If Scilab is started in a chroot, you might want to try to set the two environment variables: SCI_DISABLE_TK=1 SCI_JAVA_ENABLE_HEADLESS=1\n"));
275             return FALSE;
276         }
277
278         LhsVar(1) = 0;
279         PutLhsVar();
280         return 0;
281     }
282     /*--------------------------------------------------------------------------*/
283 }