efba2462f17c6170ba1fb9d5fb4e8e33bdf079a7
[scilab.git] / scilab / modules / jvm / src / c / loadClasspath.c
1
2 /*
3  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4  * Copyright (C) INRIA - Allan CORNET
5  * Copyright (C) 2008-2008 - INRIA - Sylvestre LEDRU
6  * 
7  * This file must be used under the terms of the CeCILL.
8  * This source file is licensed as described in the file COPYING, which
9  * you should have received as part of this distribution.  The terms
10  * are also available at    
11  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12  *
13  */
14
15 /*--------------------------------------------------------------------------*/ 
16 #include <libxml/xpath.h>
17 #include <libxml/xmlreader.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "loadClasspath.h"
21 #include "GetXmlFileEncoding.h"
22 #include "../../fileio/includes/FileExist.h"
23 #include "addToClasspath.h"
24 #include "setgetSCIpath.h"
25 #include "MALLOC.h"
26 #include "localization.h"
27 #include "scilabmode.h"
28 #include "stricmp.h"
29 #ifdef _MSC_VER
30         #include "strdup_windows.h"
31 #endif
32 /*--------------------------------------------------------------------------*/ 
33 static xmlDocPtr ClassPathxmlDocPtr = NULL;
34 /*--------------------------------------------------------------------------*/ 
35 xmlDocPtr getClassPathxmlDocPtr(void)
36 {
37         return ClassPathxmlDocPtr;
38 }
39 /*--------------------------------------------------------------------------*/ 
40 BOOL LoadClasspath(char *xmlfilename)
41 {
42         BOOL bOK = FALSE;
43         BOOL errorOnLoad = FALSE;
44         if ( FileExist(xmlfilename) )
45         {
46                 char *encoding=GetXmlFileEncoding(xmlfilename);
47
48                 /* Don't care about line return / empty line */
49                 xmlKeepBlanksDefault(0);
50                 /* check if the XML file has been encoded with utf8 (unicode) or not */
51                 if ( stricmp("utf-8", encoding)==0 )
52                 {
53                         xmlXPathContextPtr xpathCtxt = NULL;
54                         xmlXPathObjectPtr xpathObj = NULL;
55                         char *classpath=NULL;
56                         char *load="";
57                         typeOfLoad eLoad=STARTUP;
58                         char *currentMode = getScilabModeString();
59                         /* Xpath Query :
60                          * Retrieve all the path which are not disabled in our mode 
61                          */
62                         #define XPATH "//classpaths/path[not(@disableUnderMode='%s')]"
63                         char * XPath=(char*)MALLOC(sizeof(char)*(strlen(XPATH)+strlen(currentMode)-2+1)); /* -2 = strlen(%s) */
64                         sprintf(XPath,XPATH,currentMode);
65
66                         ClassPathxmlDocPtr = xmlParseFile (xmlfilename);
67
68                         if (ClassPathxmlDocPtr == NULL) 
69                         {
70                                 fprintf(stderr,_("Error: could not parse file %s\n"), xmlfilename);
71                                 if (encoding) {FREE(encoding);encoding=NULL;}
72                                 return bOK;
73                         }
74
75                         xpathCtxt = xmlXPathNewContext(ClassPathxmlDocPtr);
76                         xpathObj = xmlXPathEval((const xmlChar*)XPath, xpathCtxt);
77
78                         if(xpathObj && xpathObj->nodesetval->nodeMax) 
79                         {
80                                 /* the Xpath has been understood and there are node */
81                                 int     i;
82                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
83                                 {
84
85                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;
86                                         /* Get the properties of <path>  */
87                                         while (attrib != NULL)
88                                         {
89                                                 /* loop until when have read all the attributes */
90                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "value"))
91                                                 { 
92                                                         /* we found the tag value */
93                                                         classpath=(char*)attrib->children->content;
94                                                 }
95                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "load"))
96                                                 { 
97                                                         /* we found the tag load */
98                                                         load = (char*)attrib->children->content;
99
100                                                         /* By default, it is startup */
101                                                         if (stricmp(load,"background")==0)
102                                                         {
103                                                                 eLoad=BACKGROUND;
104                                                         } 
105                                                         else 
106                                                         {
107                                                                 if (stricmp(load,"onuse")==0) 
108                                                                 {
109                                                                         eLoad=ONUSE;
110                                                                 }
111                                                         }
112                                                 }
113                                                 else
114                                                 {
115                                                         eLoad=STARTUP;
116                                                 }
117                                                 attrib = attrib->next;
118                                         }
119
120                                         if ( (classpath) && (strlen(classpath) > 0) && (strncmp(classpath,"@",1) != 0) ) /* If it starts by a @ that means it hasn't been able to find it... which is normal... for example with the documentation */
121                                         {
122                                                 #define KEYWORDSCILAB "$SCILAB" 
123                                                 char *sciPath = getSCIpath();
124                                                 char *FullClasspath = NULL;
125                                                 
126                                                 if (strncmp(classpath,KEYWORDSCILAB,strlen(KEYWORDSCILAB))==0)
127                                                 {
128                                                         FullClasspath = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(classpath)+1));
129                                                         if (FullClasspath)
130                                                         {
131                                                                 strcpy(FullClasspath,sciPath);
132                                                                 strcat(FullClasspath,&classpath[strlen(KEYWORDSCILAB)]);
133                                                         }
134                                                 }
135                                                 else
136                                                 {
137                                                         FullClasspath = strdup(classpath);
138                                                 }
139                                             
140                                                 if (FullClasspath)
141                                                 {
142                                                         if (!addToClasspath(FullClasspath, eLoad))
143                                                         {
144                                                                 errorOnLoad=TRUE;
145                                                         }
146                                                         FREE(FullClasspath);
147                                                         FullClasspath = NULL;
148                                                 }
149
150                                                 if (sciPath) {FREE(sciPath);sciPath=NULL;}
151                                                 classpath = NULL;
152                                         }
153                                 }
154                                 bOK = TRUE;
155                         }
156                         else
157                         {
158                                 fprintf(stderr,_("Wrong format for %s.\n"), xmlfilename);
159                         }
160
161                         if(xpathObj) xmlXPathFreeObject(xpathObj);
162                         if(xpathCtxt) xmlXPathFreeContext(xpathCtxt);
163                         /*
164                         * Cleanup function for the XML library.
165                         */
166                         xmlCleanupParser();
167                 }
168                 else
169                 {
170                         fprintf(stderr,_("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n"), xmlfilename, encoding);
171                 }
172                 if (encoding) {FREE(encoding);encoding=NULL;}
173         }
174         else
175         {
176                 fprintf(stderr,_("Warning: could not find classpath declaration file %s.\n"), xmlfilename);
177         }
178         if (errorOnLoad)
179         {
180                 fprintf(stderr,_("Some problems during the loading of the Java libraries occured.\nThis could lead to inconsistent behaviours.\nPlease check SCI/etc/classpath.xml.\n"));
181         }
182
183         return bOK;
184 }
185 /*--------------------------------------------------------------------------*/