010c25a30d6efa591fbae605b07f2ce11ba12f0f
[scilab.git] / scilab / modules / jvm / src / c / loadClasspath.c
1 /*-----------------------------------------------------------------------------------*/\r
2 /* INRIA 2007 */\r
3 /* Allan CORNET */\r
4 /*-----------------------------------------------------------------------------------*/ \r
5 #include <libxml/xpath.h>\r
6 #include <libxml/xmlreader.h>\r
7 #include "loadClasspath.h"\r
8 #include "GetXmlFileEncoding.h"\r
9 #include "../../fileio/includes/FileExist.h"\r
10 #include "addToClasspath.h"\r
11 #include "setgetSCIpath.h"\r
12 #include "MALLOC.h"\r
13 /*-----------------------------------------------------------------------------------*/ \r
14 BOOL LoadClasspath(char *xmlfilename)\r
15 {\r
16         BOOL bOK = FALSE;\r
17         if ( FileExist(xmlfilename) )\r
18         {\r
19                 char *encoding=GetXmlFileEncoding(xmlfilename);\r
20 \r
21                 /* Don't care about line return / empty line */\r
22                 xmlKeepBlanksDefault(0);\r
23                 /* check if the XML file has been encoded with utf8 (unicode) or not */\r
24                 if ( (strcmp("utf-8", encoding)!=0) || (strcmp("UTF-8", encoding)==0) )\r
25                 {\r
26                         xmlDocPtr doc;\r
27                         xmlXPathContextPtr xpathCtxt = NULL;\r
28                         xmlXPathObjectPtr xpathObj = NULL;\r
29                         char *CLASSPATH=NULL;\r
30 \r
31                         doc = xmlParseFile (xmlfilename);\r
32 \r
33                         if (doc == NULL) \r
34                         {\r
35                                 printf("Error: could not parse file %s\n", xmlfilename);\r
36                                 if (encoding) {FREE(encoding);encoding=NULL;}\r
37                                 return bOK;\r
38                         }\r
39 \r
40                         xpathCtxt = xmlXPathNewContext(doc);\r
41                         xpathObj = xmlXPathEval((const xmlChar*)"//classpaths/classpath", xpathCtxt);\r
42 \r
43                         if(xpathObj && xpathObj->nodesetval->nodeMax) \r
44                         {\r
45                                 /* the Xpath has been understood and there are node */\r
46                                 int     i;\r
47                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)\r
48                                 {\r
49 \r
50                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;\r
51                                         /* Get the properties of <CLASS>  */\r
52                                         while (attrib != NULL)\r
53                                         {\r
54                                                 /* loop until when have read all the attributes */\r
55                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "path"))\r
56                                                 { \r
57                                                         /* we found the tag primitiveName */\r
58                                                         const char *str=(const char*)attrib->children->content;\r
59                                                         CLASSPATH=(char*)MALLOC(sizeof(char)*(strlen((const char*)str)+1));\r
60                                                         strcpy(CLASSPATH,str);\r
61                                                 }\r
62                                                 attrib = attrib->next;\r
63                                         }\r
64 \r
65                                         if ( (CLASSPATH) && (strlen(CLASSPATH) > 0) )\r
66                                         {\r
67                                                 #define KEYWORDSCILAB "$SCILAB" \r
68                                                 char firstchars[8];\r
69                                                 char *SCIPATH=NULL;\r
70                                                 SCIPATH=getSCIpath();\r
71                                                 \r
72                                                 strncpy(firstchars,CLASSPATH,strlen(KEYWORDSCILAB));\r
73                                                 firstchars[strlen(KEYWORDSCILAB)]='\0';\r
74 \r
75                                                 if (strcmp(firstchars,KEYWORDSCILAB)==0)\r
76                                                 {\r
77                                                         char *modifypath = (char*)MALLOC(sizeof(char)*(strlen(SCIPATH)+strlen(CLASSPATH)+1));\r
78                                                         strcpy(modifypath,SCIPATH);\r
79                                                         strcat(modifypath,&CLASSPATH[strlen(KEYWORDSCILAB)]);\r
80                                                         FREE(CLASSPATH);\r
81                                                         CLASSPATH = modifypath;\r
82                                                 }\r
83 \r
84                                             if (SCIPATH) {FREE(SCIPATH);SCIPATH=NULL;}\r
85                                                 addToClasspath(CLASSPATH);\r
86                                                 FREE(CLASSPATH);\r
87                                                 CLASSPATH = NULL;\r
88                                         }\r
89                                 }\r
90                                 bOK = TRUE;\r
91                         }\r
92                         else\r
93                                 {\r
94                         printf("Wrong format for %s. \n", xmlfilename);\r
95                         }\r
96                 }\r
97                 else\r
98                 {\r
99                         printf("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n", xmlfilename, encoding);\r
100                 }\r
101                 if (encoding) {FREE(encoding);encoding=NULL;}\r
102         }\r
103         return bOK;\r
104 }\r
105 /*-----------------------------------------------------------------------------------*/ \r