xml load take care of scilab current directory 92/13892/2
Antoine ELIAS [Tue, 4 Mar 2014 09:52:33 +0000 (10:52 +0100)]
Change-Id: I8566dd6029173d9e6d07f3c03e538f31d721c5a4

scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/xmlloader/XMLDomLoader.java

index db5d995..ba7ddf7 100644 (file)
@@ -9,6 +9,7 @@ import java.util.StringTokenizer;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
+import org.scilab.modules.commons.CommonFileUtils;
 import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.graphic_objects.builder.Builder;
 import org.scilab.modules.graphic_objects.console.Console;
@@ -198,19 +199,52 @@ public class XMLDomLoader {
     public int parse(String filename) {
         try {
             this.filename = filename;
-            File file = new File(filename);
+            File f = new File(filename);
+            String currentPath = "";
+
+            if (f.exists()) {
+                //add filename filepath in ScilabSwingUtilities paths
+                if (f.isAbsolute()) {
+                    String filePath = f.getAbsolutePath();
+                    currentPath = filePath.substring(0, filePath.lastIndexOf(File.separator));
+                    FindIconHelper.addThemePath(currentPath);
+                } else {
+                    String initialDirectoryPath = CommonFileUtils.getCWD();
+                    String filePath = "";
+                    Integer index = filename.lastIndexOf(File.separator);
+                    if (index != -1) {
+                        filePath = filename.substring(0, index);
+                    }
+
+                    currentPath = initialDirectoryPath + File.separator + filePath;
+                    FindIconHelper.addThemePath(currentPath);
 
-            if (file.exists()) {
-                //add filename filepath in FindIconHelper paths
-                String absoluteFilePath = file.getAbsolutePath();
-                String path = absoluteFilePath.substring(0, absoluteFilePath.lastIndexOf(File.separator));
-                FindIconHelper.addThemePath(path);
+                    f = new File(currentPath + File.separator + filename);
+                }
             } else {
-                return 1;
+                //try to find file in currentPath
+                if (f.isAbsolute()) {
+                    //failed
+                    return 1;
+                } else {
+                    String initialDirectoryPath = CommonFileUtils.getCWD();
+                    String filePath = "";
+                    Integer index = filename.lastIndexOf(File.separator);
+                    if (index != -1) {
+                        filePath = filename.substring(0, index);
+                    }
+
+                    currentPath = initialDirectoryPath + File.separator + filePath;
+                }
+
+                f = new File(currentPath + File.separator + filename);
+                if (f.exists() == false) {
+                    return 1;
+                }
             }
 
             DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document doc = dBuilder.parse(file);
+            Document doc = dBuilder.parse(f);
             doc.getDocumentElement().normalize();
 
             if (doc.hasChildNodes()) {