Oups, revert useless changes sorry.
[scilab.git] / scilab / modules / jvm / src / c / loadOnUseClassPath.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008-2008 - INRIA - Sylvestre LEDRU
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 /*--------------------------------------------------------------------------*/ 
14 #include <jni.h>
15 #include <stdio.h>
16 #include <libxml/xpath.h>
17 #include <libxml/xmlreader.h>
18 #include "MALLOC.h"
19 #include "scilabDefaults.h"
20 #include "getScilabJNIEnv.h"
21 #include "localization.h"
22 #include "setgetSCIpath.h"
23 #include "stricmp.h"
24 #include "addToClasspath.h"
25 #include "loadOnUseClassPath.h"
26 #include "../../fileio/includes/FileExist.h"
27 #include "GetXmlFileEncoding.h"
28 #ifdef _MSC_VER
29 #include "strdup_windows.h"
30 #endif
31 /*--------------------------------------------------------------------------*/ 
32 BOOL loadOnUseClassPath(char *tag)
33 {
34         BOOL bOK = FALSE;
35         char *sciPath = getSCIpath();
36
37         char *classpathfile = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(XMLCLASSPATH)+1));
38
39         sprintf(classpathfile,XMLCLASSPATH,sciPath);
40
41         // Load the XML
42         if ( FileExist(classpathfile) )
43         {
44                 char *encoding = GetXmlFileEncoding(classpathfile);
45
46                 /* Don't care about line return / empty line */
47                 xmlKeepBlanksDefault(0);
48                 /* check if the XML file has been encoded with utf8 (unicode) or not */
49                 if ( stricmp("utf-8", encoding)==0 )
50                 {
51                         xmlDocPtr doc;
52                         xmlXPathContextPtr xpathCtxt = NULL;
53                         xmlXPathObjectPtr xpathObj = NULL;
54                         /* 
55                          * Xpath query: Retrieve all nodes what have the property
56                          * load="onUse" and the tag we are looking for 
57                          */
58                         #define XPATH "//classpaths/path[@load='onUse']/load[@on='%s']"
59                         char * XPath=(char*)MALLOC(sizeof(char)*(strlen(XPATH)+strlen(tag)-2+1)); /* -2 = strlen(%s) */
60                         sprintf(XPath,XPATH,tag);
61                         doc = xmlParseFile (classpathfile);
62                         if (doc == NULL) 
63                         {
64                                 fprintf(stderr,_("Error: could not parse file %s\n"), classpathfile);
65                                 if (encoding) {FREE(encoding);encoding=NULL;}
66                                 return bOK;
67                         }
68
69                         xpathCtxt = xmlXPathNewContext(doc);
70                         // Look for all the tag which matches
71                         xpathObj = xmlXPathEval((const xmlChar*)XPath, xpathCtxt);
72
73                         if(xpathObj && xpathObj->nodesetval->nodeMax) 
74                         {
75                                 /* the Xpath has been understood and there are node */
76                                 int     i;
77                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
78                                 {
79                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->parent->properties;
80                                         while (attrib != NULL) 
81                                         {
82                                                 /* loop until when have read all the attributes */
83                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "value"))
84                                                 { 
85                                                         // @TODO Check if it has been loaded before
86
87                                                         /* we found the tag value  & load the jar */
88                                                         /* replaces $SCILAB by current path of SCI */
89                                                         #define KEYWORDSCILAB "$SCILAB" \r
90                                                         char *sciPath = getSCIpath();\r
91                                                         char *FullClasspath = NULL;\r
92                                                         char *classpath = (char*)attrib->children->content;\r
93                                                 \r
94                                                         if (strncmp(classpath,KEYWORDSCILAB,strlen(KEYWORDSCILAB))==0)\r
95                                                         {\r
96                                                                 FullClasspath = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(classpath)+1));\r
97                                                                 if (FullClasspath)\r
98                                                                 {\r
99                                                                         strcpy(FullClasspath,sciPath);\r
100                                                                         strcat(FullClasspath,&classpath[strlen(KEYWORDSCILAB)]);\r
101                                                                 }\r
102                                                         }\r
103                                                         else\r
104                                                         {\r
105                                                                 FullClasspath = strdup(classpath);\r
106                                                         }\r
107
108                                                         addToClasspath(FullClasspath,STARTUP);
109                                                         FREE(FullClasspath);
110                                                 }
111                                                 attrib = attrib->next;
112                                         }
113                                 }
114                         }
115                         else
116                         {
117                                 fprintf(stderr,_("Wrong format for %s.\n"), classpathfile);
118                         }
119
120                         if(xpathObj) xmlXPathFreeObject(xpathObj);
121                         if(xpathCtxt) xmlXPathFreeContext(xpathCtxt);
122                         xmlFreeDoc (doc);
123                         /*
124                         * Cleanup function for the XML library.
125                         */
126                         xmlCleanupParser();
127                 }
128                 else
129                 {
130                         fprintf(stderr,_("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n"), classpathfile, encoding);
131                 }
132                 if (encoding) {FREE(encoding);encoding=NULL;}
133
134         } 
135         else
136         {
137                 fprintf(stderr,_("Warning: could not find classpath declaration file %s.\n"), classpathfile);
138         }
139         return FALSE;   
140 }
141 /*--------------------------------------------------------------------------*/