No need to perform this check twice... it has been done on Scilab startup and it...
[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                 /* Don't care about line return / empty line */
45                 xmlKeepBlanksDefault(0);
46                 /* check if the XML file has been encoded with utf8 (unicode) or not */
47
48                         xmlDocPtr doc;
49                         xmlXPathContextPtr xpathCtxt = NULL;
50                         xmlXPathObjectPtr xpathObj = NULL;
51                         /* 
52                          * Xpath query: Retrieve all nodes what have the property
53                          * load="onUse" and the tag we are looking for 
54                          */
55                         #define XPATH "//classpaths/path[@load='onUse']/load[@on='%s']"
56                         char * XPath=(char*)MALLOC(sizeof(char)*(strlen(XPATH)+strlen(tag)-2+1)); /* -2 = strlen(%s) */
57                         sprintf(XPath,XPATH,tag);
58                         doc = xmlParseFile (classpathfile);
59                         if (doc == NULL) 
60                         {
61                                 fprintf(stderr,_("Error: could not parse file %s\n"), classpathfile);
62                                 return bOK;
63                         }
64
65                         xpathCtxt = xmlXPathNewContext(doc);
66                         // Look for all the tag which matches
67                         xpathObj = xmlXPathEval((const xmlChar*)XPath, xpathCtxt);
68
69                         if(xpathObj && xpathObj->nodesetval->nodeMax) 
70                         {
71                                 /* the Xpath has been understood and there are node */
72                                 int     i;
73                                 for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)
74                                 {
75                                         xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->parent->properties;
76                                         while (attrib != NULL) 
77                                         {
78                                                 /* loop until when have read all the attributes */
79                                                 if (xmlStrEqual (attrib->name, (const xmlChar*) "value"))
80                                                 { 
81                                                         // @TODO Check if it has been loaded before
82
83                                                         /* we found the tag value  & load the jar */
84                                                         /* replaces $SCILAB by current path of SCI */
85                                                         #define KEYWORDSCILAB "$SCILAB" 
86                                                         char *sciPath = getSCIpath();
87                                                         char *FullClasspath = NULL;
88                                                         char *classpath = (char*)attrib->children->content;
89                                                 
90                                                         if (strncmp(classpath,KEYWORDSCILAB,strlen(KEYWORDSCILAB))==0)
91                                                         {
92                                                                 FullClasspath = (char*)MALLOC(sizeof(char)*(strlen(sciPath)+strlen(classpath)+1));
93                                                                 if (FullClasspath)
94                                                                 {
95                                                                         strcpy(FullClasspath,sciPath);
96                                                                         strcat(FullClasspath,&classpath[strlen(KEYWORDSCILAB)]);
97                                                                 }
98                                                         }
99                                                         else
100                                                         {
101                                                                 FullClasspath = strdup(classpath);
102                                                         }
103
104                                                         addToClasspath(FullClasspath,STARTUP);
105                                                         FREE(FullClasspath);
106                                                 }
107                                                 attrib = attrib->next;
108                                         }
109                                 }
110                         }
111                         else
112                         {
113                                 fprintf(stderr,_("Wrong format for %s.\n"), classpathfile);
114                         }
115
116                         if(xpathObj) xmlXPathFreeObject(xpathObj);
117                         if(xpathCtxt) xmlXPathFreeContext(xpathCtxt);
118                         xmlFreeDoc (doc);
119                         /*
120                         * Cleanup function for the XML library.
121                         */
122                         xmlCleanupParser();
123
124         } 
125         else
126         {
127                 fprintf(stderr,_("Warning: could not find classpath declaration file %s.\n"), classpathfile);
128         }
129         return FALSE;   
130 }
131 /*--------------------------------------------------------------------------*/