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
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
15 /*--------------------------------------------------------------------------*/
16 #include <libxml/xpath.h>
17 #include <libxml/xmlreader.h>
20 #include "loadClasspath.h"
21 #include "GetXmlFileEncoding.h"
22 #include "../../fileio/includes/FileExist.h"
23 #include "addToClasspath.h"
24 #include "setgetSCIpath.h"
26 #include "localization.h"
27 #include "scilabmode.h"
30 #include "strdup_windows.h"
32 /*--------------------------------------------------------------------------*/
33 static xmlDocPtr ClassPathxmlDocPtr = NULL;
34 /*--------------------------------------------------------------------------*/
35 xmlDocPtr getClassPathxmlDocPtr(void)
37 return ClassPathxmlDocPtr;
39 /*--------------------------------------------------------------------------*/
40 void freeClassPathxmlDocPtr(void)
42 if (ClassPathxmlDocPtr)
44 xmlFreeDoc (ClassPathxmlDocPtr);
45 ClassPathxmlDocPtr = NULL;
48 /*--------------------------------------------------------------------------*/
49 BOOL LoadClasspath(char *xmlfilename)
52 BOOL errorOnLoad = FALSE;
53 if ( FileExist(xmlfilename) )
55 char *encoding=GetXmlFileEncoding(xmlfilename);
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 )
62 xmlXPathContextPtr xpathCtxt = NULL;
63 xmlXPathObjectPtr xpathObj = NULL;
66 typeOfLoad eLoad=STARTUP;
67 char *currentMode = getScilabModeString();
69 * Retrieve all the path which are not disabled in our mode
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);
75 ClassPathxmlDocPtr = xmlParseFile (xmlfilename);
77 if (ClassPathxmlDocPtr == NULL)
79 fprintf(stderr,_("Error: could not parse file %s\n"), xmlfilename);
80 if (encoding) {FREE(encoding);encoding=NULL;}
84 xpathCtxt = xmlXPathNewContext(ClassPathxmlDocPtr);
85 xpathObj = xmlXPathEval((const xmlChar*)XPath, xpathCtxt);
87 if(xpathObj && xpathObj->nodesetval->nodeMax)
89 /* the Xpath has been understood and there are node */
91 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
94 xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;
95 /* Get the properties of <path> */
96 while (attrib != NULL)
98 /* loop until when have read all the attributes */
99 if (xmlStrEqual (attrib->name, (const xmlChar*) "value"))
101 /* we found the tag value */
102 classpath=(char*)attrib->children->content;
104 if (xmlStrEqual (attrib->name, (const xmlChar*) "load"))
106 /* we found the tag load */
107 load = (char*)attrib->children->content;
109 /* By default, it is startup */
110 if (stricmp(load,"background")==0)
116 if (stricmp(load,"onuse")==0)
126 attrib = attrib->next;
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 */
131 #define KEYWORDSCILAB "$SCILAB"
132 char *sciPath = getSCIpath();
133 char *FullClasspath = NULL;
135 if (strncmp(classpath,KEYWORDSCILAB,strlen(KEYWORDSCILAB))==0)
137 FullClasspath = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(classpath)+1));
140 strcpy(FullClasspath,sciPath);
141 strcat(FullClasspath,&classpath[strlen(KEYWORDSCILAB)]);
146 FullClasspath = strdup(classpath);
151 if (!addToClasspath(FullClasspath, eLoad))
156 FullClasspath = NULL;
159 if (sciPath) {FREE(sciPath);sciPath=NULL;}
167 fprintf(stderr,_("Wrong format for %s.\n"), xmlfilename);
170 if(xpathObj) xmlXPathFreeObject(xpathObj);
171 if(xpathCtxt) xmlXPathFreeContext(xpathCtxt);
173 * Cleanup function for the XML library.
179 fprintf(stderr,_("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n"), xmlfilename, encoding);
181 if (encoding) {FREE(encoding);encoding=NULL;}
185 fprintf(stderr,_("Warning: could not find classpath declaration file %s.\n"), xmlfilename);
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"));
194 /*--------------------------------------------------------------------------*/