free ClassPathxmlDocPtr @ exit
[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 void freeClassPathxmlDocPtr(void)
41 {
42         if (ClassPathxmlDocPtr)
43         {
44                 xmlFreeDoc (ClassPathxmlDocPtr);
45                 ClassPathxmlDocPtr = NULL;
46         }
47 }
48 /*--------------------------------------------------------------------------*/ 
49 BOOL LoadClasspath(char *xmlfilename)
50 {
51         BOOL bOK = FALSE;
52         BOOL errorOnLoad = FALSE;
53         if ( FileExist(xmlfilename) )
54         {
55                 char *encoding=GetXmlFileEncoding(xmlfilename);
56
57                 /* Don't care about line return / empty line */
58                 xmlKeepBlanksDefault(0);
59                 /* check if the XML file has been encoded with utf8 (unicode) or not */
60                 if ( stricmp("utf-8", encoding)==0 )
61                 {
62                         xmlXPathContextPtr xpathCtxt = NULL;
63                         xmlXPathObjectPtr xpathObj = NULL;
64                         char *classpath=NULL;
65                         char *load="";
66                         typeOfLoad eLoad=STARTUP;
67                         char *currentMode = getScilabModeString();
68                         /* Xpath Query :
69                          * Retrieve all the path which are not disabled in our mode 
70                          */
71                         #define XPATH "//classpaths/path[not(@disableUnderMode='%s')]"
72                         char * XPath=(char*)MALLOC(sizeof(char)*(strlen(XPATH)+strlen(currentMode)-2+1)); /* -2 = strlen(%s) */
73                         sprintf(XPath,XPATH,currentMode);
74
75                         ClassPathxmlDocPtr = xmlParseFile (xmlfilename);
76
77                         if (ClassPathxmlDocPtr == NULL) 
78                         {
79                                 fprintf(stderr,_("Error: could not parse file %s\n"), xmlfilename);
80                                 if (encoding) {FREE(encoding);encoding=NULL;}
81                                 return bOK;
82                         }
83
84                         xpathCtxt = xmlXPathNewContext(ClassPathxmlDocPtr);
85                         xpathObj = xmlXPathEval((const xmlChar*)XPath, xpathCtxt);
86
87                         if(xpathObj && xpathObj->nodesetval->nodeMax) 
88                         {
89                                 /* the Xpath has been understood and there are node */
90                                 int     i;
91                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
92                                 {
93
94                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;
95                                         /* Get the properties of <path>  */
96                                         while (attrib != NULL)
97                                         {
98                                                 /* loop until when have read all the attributes */
99                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "value"))
100                                                 { 
101                                                         /* we found the tag value */
102                                                         classpath=(char*)attrib->children->content;
103                                                 }
104                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "load"))
105                                                 { 
106                                                         /* we found the tag load */
107                                                         load = (char*)attrib->children->content;
108
109                                                         /* By default, it is startup */
110                                                         if (stricmp(load,"background")==0)
111                                                         {
112                                                                 eLoad=BACKGROUND;
113                                                         } 
114                                                         else 
115                                                         {
116                                                                 if (stricmp(load,"onuse")==0) 
117                                                                 {
118                                                                         eLoad=ONUSE;
119                                                                 }
120                                                         }
121                                                 }
122                                                 else
123                                                 {
124                                                         eLoad=STARTUP;
125                                                 }
126                                                 attrib = attrib->next;
127                                         }
128
129                                         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 */
130                                         {
131                                                 #define KEYWORDSCILAB "$SCILAB" 
132                                                 char *sciPath = getSCIpath();
133                                                 char *FullClasspath = NULL;
134                                                 
135                                                 if (strncmp(classpath,KEYWORDSCILAB,strlen(KEYWORDSCILAB))==0)
136                                                 {
137                                                         FullClasspath = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(classpath)+1));
138                                                         if (FullClasspath)
139                                                         {
140                                                                 strcpy(FullClasspath,sciPath);
141                                                                 strcat(FullClasspath,&classpath[strlen(KEYWORDSCILAB)]);
142                                                         }
143                                                 }
144                                                 else
145                                                 {
146                                                         FullClasspath = strdup(classpath);
147                                                 }
148                                             
149                                                 if (FullClasspath)
150                                                 {
151                                                         if (!addToClasspath(FullClasspath, eLoad))
152                                                         {
153                                                                 errorOnLoad=TRUE;
154                                                         }
155                                                         FREE(FullClasspath);
156                                                         FullClasspath = NULL;
157                                                 }
158
159                                                 if (sciPath) {FREE(sciPath);sciPath=NULL;}
160                                                 classpath = NULL;
161                                         }
162                                 }
163                                 bOK = TRUE;
164                         }
165                         else
166                         {
167                                 fprintf(stderr,_("Wrong format for %s.\n"), xmlfilename);
168                         }
169
170                         if(xpathObj) xmlXPathFreeObject(xpathObj);
171                         if(xpathCtxt) xmlXPathFreeContext(xpathCtxt);
172                         /*
173                         * Cleanup function for the XML library.
174                         */
175                         xmlCleanupParser();
176                 }
177                 else
178                 {
179                         fprintf(stderr,_("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n"), xmlfilename, encoding);
180                 }
181                 if (encoding) {FREE(encoding);encoding=NULL;}
182         }
183         else
184         {
185                 fprintf(stderr,_("Warning: could not find classpath declaration file %s.\n"), xmlfilename);
186         }
187         if (errorOnLoad)
188         {
189                 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"));
190         }
191
192         return bOK;
193 }
194 /*--------------------------------------------------------------------------*/