* Bug 9501 fixed - With a localized TMPDIR, block edition could throw an HDF5 exception. 34/4834/2
Clément DAVID [Fri, 2 Sep 2011 13:22:00 +0000 (15:22 +0200)]
To workaround this jhdf5/hdf5 bug, we use DOS 8.5 short name for temporary HDF5 files.

Change-Id: If701182c36e4b38909bcc23bcec188729b9b2849

25 files changed:
scilab/CHANGES_5.4.X
scilab/modules/commons/Makefile.am
scilab/modules/commons/Makefile.in
scilab/modules/commons/src/c/commons.vcxproj
scilab/modules/commons/src/java/org/scilab/modules/commons/ScilabCommons.java
scilab/modules/commons/src/java/org/scilab/modules/commons/ScilabCommonsJNI.java
scilab/modules/commons/src/jni/ScilabCommons.i
scilab/modules/commons/src/jni/ScilabCommons_wrap.c
scilab/modules/fileio/includes/createtempfilename.h
scilab/modules/xcos/src/java/org/scilab/modules/xcos/Xcos.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/CompileAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/DumpAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/InitModelicaAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/StartAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/ViewDiagramBrowserAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/BasicBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/CodeGenerationAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/ViewDetailsAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ContextUpdate.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/CompilationEngineStatus.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/PaletteDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/scicos/H5RWHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/FileUtils.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosFileType.java

index e54f1c2..46b363a 100644 (file)
@@ -74,6 +74,9 @@ Xcos:
 * bug 9478 fixed - Block structure was not checked while adding them to a new
                    palette.
 
+* Bug 9501 fixed - With a localized TMPDIR, block edition could throw an HDF5 
+                   exception.
+
 * bug 9506 fixed - some libraries dependencies were not loaded for xcos_skeleton startup.
 
 * bug 9532 fixed - Users defined blocks had no icons when saved
index 7ff4e66..b18f5b7 100644 (file)
@@ -58,7 +58,8 @@ BUILT_SOURCES=swig
 endif
 
 libscicommons_la_LIBADD = $(top_builddir)/libs/MALLOC/libscimalloc.la \
-$(top_builddir)/modules/output_stream/libscioutput_stream.la
+$(top_builddir)/modules/output_stream/libscioutput_stream.la \
+$(top_builddir)/modules/fileio/libscifileio.la
 
 
 #### commons : gateway declaration ####
index 1b75488..829bcc3 100644 (file)
@@ -124,7 +124,8 @@ libscicommons_disable_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
        $(libscicommons_disable_la_LDFLAGS) $(LDFLAGS) -o $@
 libscicommons_la_DEPENDENCIES =  \
        $(top_builddir)/libs/MALLOC/libscimalloc.la \
-       $(top_builddir)/modules/output_stream/libscioutput_stream.la
+       $(top_builddir)/modules/output_stream/libscioutput_stream.la \
+       $(top_builddir)/modules/fileio/libscifileio.la
 am__objects_1 = libscicommons_la-ScilabCommons_wrap.lo
 am__objects_2 = empty.lo
 am_libscicommons_la_OBJECTS = $(am__objects_1) $(am__objects_2)
@@ -503,7 +504,8 @@ INCLUDE_FLAGS = $(libscicommons_la_CFLAGS)
 SWIG_WRAPPERS = src/jni/ScilabCommons.i
 @SWIG_TRUE@BUILT_SOURCES = swig
 libscicommons_la_LIBADD = $(top_builddir)/libs/MALLOC/libscimalloc.la \
-$(top_builddir)/modules/output_stream/libscioutput_stream.la
+$(top_builddir)/modules/output_stream/libscioutput_stream.la \
+$(top_builddir)/modules/fileio/libscifileio.la
 
 
 #### commons : gateway declaration ####
index d39b8e7..0f6f4e0 100644 (file)
@@ -262,6 +262,9 @@ lib /DEF:"$(ProjectDir)commons_f_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Platf
       <Project>{dd8a0506-8d31-4cf8-856a-c10ece9c13a4}</Project>
       <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
     </ProjectReference>
+    <ProjectReference Include="..\..\..\fileio\fileio.vcxproj">
+      <Project>{4fc72d4a-80ee-4b1a-8724-0201c1a35621}</Project>
+    </ProjectReference>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
index f103cbd..23d4e1c 100644 (file)
@@ -35,4 +35,8 @@ public static String getSCIHOME() {
     return ScilabCommonsJNI.getTMPDIR();
   }
 
+  public static String createtempfilename(String prefix, int bShortFormat) {
+    return ScilabCommonsJNI.createtempfilename(prefix, bShortFormat);
+  }
+
 }
index e3b4091..70882b9 100644 (file)
@@ -46,4 +46,5 @@ public class ScilabCommonsJNI {
 
   public final static native String getSCIHOME();
   public final static native String getTMPDIR();
+  public final static native String createtempfilename(String jarg1, int jarg2);
 }
index 6016c81..505469c 100644 (file)
@@ -20,6 +20,8 @@
 %{
 #include "../../../core/includes/SCIHOME.h"
 #include "../../../core/includes/tmpdir.h"
+
+#include "../../../fileio/includes/createtempfilename.h"
 %}
 
 /* JavaDoc for ScilabCommonsJNI class */
@@ -101,3 +103,17 @@ char* getSCIHOME();
    */
 public";
 char *getTMPDIR(void);
+
+
+/* JavaDoc */
+%javamethodmodifiers createTempFileName() "
+ /**
+  * Create file with unique file name in TMPDIR
+  * TMPDIR/PREFIX[XXXXXXX]
+  * @param prefix the file prefix
+  * @param bShortFormat set 1 for a 8.5 filename, 0 otherwise
+  * @return a unique filename
+  */
+public";
+char *createtempfilename(const char* prefix, int bShortFormat);
+
index 6ae8296..af1925c 100644 (file)
@@ -190,6 +190,8 @@ static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionC
 #include "../../../core/includes/SCIHOME.h"
 #include "../../../core/includes/tmpdir.h"
 
+#include "../../../fileio/includes/createtempfilename.h"
+
 
 #ifdef __cplusplus
 extern "C" {
@@ -219,6 +221,27 @@ SWIGEXPORT jstring JNICALL Java_org_scilab_modules_commons_ScilabCommonsJNI_getT
 }
 
 
+SWIGEXPORT jstring JNICALL Java_org_scilab_modules_commons_ScilabCommonsJNI_createtempfilename(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) {
+  jstring jresult = 0 ;
+  char *arg1 = (char *) 0 ;
+  int arg2 ;
+  char *result = 0 ;
+  
+  (void)jenv;
+  (void)jcls;
+  arg1 = 0;
+  if (jarg1) {
+    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0);
+    if (!arg1) return 0;
+  }
+  arg2 = (int)jarg2; 
+  result = (char *)createtempfilename((char const *)arg1,arg2);
+  if (result) jresult = (*jenv)->NewStringUTF(jenv, (const char *)result);
+  if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, (const char *)arg1);
+  return jresult;
+}
+
+
 #ifdef __cplusplus
 }
 #endif
index 62c1e17..a822fc4 100644 (file)
@@ -13,6 +13,7 @@
 #ifndef __CREATETEMPFILENAME_H__
 #define __CREATETEMPFILENAME_H__
 
+#include "dynlib_fileio.h"
 #include "charEncoding.h"
 #include "BOOL.h"
 
@@ -23,9 +24,9 @@
 * @param[in] bShortFormat 
 * @return a unique filename
 */
-char *createtempfilename(const char *prefix, BOOL bShortFormat);
+FILEIO_IMPEXP char *createtempfilename(const char *prefix, BOOL bShortFormat);
 
-wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat);
+FILEIO_IMPEXP wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat);
 
 #endif /* __CREATETEMPFILENAME_H__ */
 /*--------------------------------------------------------------------------*/
index 9c7f326..d53d84e 100644 (file)
@@ -14,6 +14,7 @@
 package org.scilab.modules.xcos;
 
 import static org.scilab.modules.xcos.utils.FileUtils.delete;
+import static org.scilab.modules.xcos.utils.FileUtils.exists;
 
 import java.io.File;
 import java.io.IOException;
@@ -631,13 +632,12 @@ public final class Xcos {
        public static int xcosDiagramToHDF5(final String xcosFile, final String h5File,
                        final boolean overwrite) {
                final File file = new File(xcosFile);
-               final File temp = new File(h5File);
 
-               if (temp.exists()) {
+               if (exists(h5File)) {
                        if (!overwrite) {
                                return 1;
                        } else {
-                               delete(temp);
+                               delete(h5File);
                        }
                }
                
@@ -651,7 +651,7 @@ public final class Xcos {
                                public void run() {
                                        final XcosDiagram diagram = new XcosDiagram();
                                        diagram.openDiagramFromFile(file);
-                                       diagram.dumpToHdf5File(temp);
+                                       diagram.dumpToHdf5File(h5File);
                                }
                        });
                } catch (final InterruptedException e) {
index 5f42a8e..fda6863 100644 (file)
@@ -18,7 +18,6 @@ import static org.scilab.modules.xcos.utils.FileUtils.delete;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.File;
 import java.io.IOException;
 
 import javax.swing.SwingWorker;
@@ -70,7 +69,7 @@ public class CompileAction extends SimulationNotRunningAction {
        public void actionPerformed(ActionEvent e) {
                ((XcosDiagram) getGraph(null)).info(XcosMessages.EXPORT_IN_PROGRESS);
                
-               final File temp;
+               final String temp;
                try {
                        temp = FileUtils.createTempFile();
                } catch (IOException e1) {
@@ -91,7 +90,7 @@ public class CompileAction extends SimulationNotRunningAction {
                        protected void done() {
                                ((XcosDiagram) getGraph(null)).info(XcosMessages.COMPILATION_IN_PROGRESS);
                                
-                               String cmd = buildCall("import_from_hdf5", temp.getAbsolutePath()) 
+                               String cmd = buildCall("import_from_hdf5", temp) 
                                                     + "cpr = xcos_compile(scs_m);";
                                
                                final ActionListener action = new ActionListener() {
index 9b047ef..81d7db1 100644 (file)
@@ -77,12 +77,12 @@ public class DumpAction extends DefaultAction {
        @Override
     public void actionPerformed(ActionEvent e) {
                try {
-                   File temp = FileUtils.createTempFile();
-                   temp.deleteOnExit();
+                   String temp = FileUtils.createTempFile();
+                   new File(temp).deleteOnExit();
                    ((XcosDiagram) getGraph(e)).dumpToHdf5File(temp);
                    try {
-                       String cmd = buildCall("import_from_hdf5", temp.getAbsolutePath());
-                       cmd += buildCall("deletefile", temp.getAbsolutePath());
+                       String cmd = buildCall("import_from_hdf5", temp);
+                       cmd += buildCall("deletefile", temp);
                        ScilabInterpreterManagement.synchronousScilabExec(cmd);
                    } catch (InterpreterException e1) {
                                LogFactory.getLog(DumpAction.class).error(e1);
index 150226d..f13583a 100644 (file)
@@ -16,7 +16,6 @@ import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManag
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.logging.LogFactory;
@@ -74,16 +73,16 @@ public class InitModelicaAction extends DefaultAction {
      */
        @Override
     public void actionPerformed(ActionEvent e) {
-       File temp;
+       String temp;
 
        try {
            ((XcosDiagram) getGraph(null)).info(XcosMessages.INITIALIZING_MODELICA_COMPILER);
            temp = FileUtils.createTempFile();
            ((XcosDiagram) getGraph(e)).getRootDiagram().dumpToHdf5File(temp);
 
-           String cmd = buildCall("import_from_hdf5", temp.getAbsolutePath());
+           String cmd = buildCall("import_from_hdf5", temp);
            cmd += buildCall("xcosConfigureModelica");
-           cmd += buildCall("deletefile", temp.getAbsolutePath());
+           cmd += buildCall("deletefile", temp);
            
            final ActionListener action = new ActionListener() {
                        @Override
index 555217b..9659ab2 100644 (file)
@@ -19,7 +19,6 @@ import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManag
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.logging.Log;
@@ -130,10 +129,10 @@ public class StartAction extends OneBlockDependantAction {
                /*
                 * Import a valid scs_m structure into Scilab
                 */
-               final File temp = FileUtils.createTempFile();
+               final String temp = FileUtils.createTempFile();
                diagram.dumpToHdf5File(temp);
 
-               command.append(buildCall("import_from_hdf5", temp.getAbsolutePath()));
+               command.append(buildCall("import_from_hdf5", temp));
                command.append(buildCall("scicos_debug", diagram.getScicosParameters().getDebugLevel()));
                
                /*
index 4f65ede..17bb797 100644 (file)
@@ -64,13 +64,13 @@ public final class ViewDiagramBrowserAction extends OneBlockDependantAction {
        @Override
        public void actionPerformed(ActionEvent e) {
                try {
-                       File temp = FileUtils.createTempFile();
-                       temp.deleteOnExit();
+                       String temp = FileUtils.createTempFile();
+                       new File(temp).deleteOnExit();
                        ((XcosDiagram) getGraph(null)).dumpToHdf5File(temp);
                        try {
-                               String cmd = ScilabInterpreterManagement.buildCall("import_from_hdf5", temp.getAbsolutePath());
+                               String cmd = ScilabInterpreterManagement.buildCall("import_from_hdf5", temp);
                                cmd += "tree_show(scs_m); ";
-                               cmd += ScilabInterpreterManagement.buildCall("deletefile", temp.getAbsolutePath());
+                               cmd += ScilabInterpreterManagement.buildCall("deletefile", temp);
                                ScilabInterpreterManagement.synchronousScilabExec(cmd);
                        } catch (InterpreterException e2) {
                                e2.printStackTrace();
index e08791d..dba8385 100644 (file)
@@ -13,6 +13,7 @@
 package org.scilab.modules.xcos.block;
 
 import static org.scilab.modules.xcos.utils.FileUtils.delete;
+import static org.scilab.modules.xcos.utils.FileUtils.exists;
 
 import java.awt.MouseInfo;
 import java.awt.event.ActionEvent;
@@ -1169,9 +1170,9 @@ public class BasicBlock extends ScilabGraphUniqueObject implements Serializable
            return;
        }
        
-       final File tempOutput;
-       final File tempInput;
-       final File tempContext;
+       final String tempOutput;
+       final String tempInput;
+       final String tempContext;
        final BasicBlock currentBlock = this;
        
        try {
@@ -1185,7 +1186,7 @@ public class BasicBlock extends ScilabGraphUniqueObject implements Serializable
            final ActionListener action = new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
-                               if (tempInput.exists()) {
+                               if (exists(tempInput)) {
                                        LOG.trace("Updating data.");
                                        
                                // Now read new Block
@@ -1214,11 +1215,11 @@ public class BasicBlock extends ScilabGraphUniqueObject implements Serializable
                setLocked(true);
                        ScilabInterpreterManagement.asynchronousScilabExec(action, 
                                "xcosBlockInterface", 
-                               tempOutput.getAbsolutePath(),
-                               tempInput.getAbsolutePath(),
+                               tempOutput,
+                               tempInput,
                                getInterfaceFunctionName().toCharArray(),
                                "set",
-                               tempContext.getAbsolutePath());
+                               tempContext);
                } catch (InterpreterException e) {
                        LOG.error(e);
                        setLocked(false);
@@ -1232,13 +1233,14 @@ public class BasicBlock extends ScilabGraphUniqueObject implements Serializable
     /**
      * @return exported file
      */
-    protected File exportBlockStruct() {
+    protected String exportBlockStruct() {
 
        // Write scs_m
-       File tempOutput;
+       String tempOutput;
        try {
            tempOutput = FileUtils.createTempFile();
-           tempOutput.deleteOnExit();
+           File f = new File(tempOutput);
+           f.deleteOnExit();
            
            new H5RWHandler(tempOutput).writeBlock(this);
            return tempOutput;
@@ -1252,16 +1254,18 @@ public class BasicBlock extends ScilabGraphUniqueObject implements Serializable
      * @param context parent diagram context
      * @return exported file
      */
-    protected File exportContext(String[] context) {
+    protected String exportContext(String[] context) {
 
        // Write context
        try {
-           File tempContext = FileUtils.createTempFile();
+               String fileString = FileUtils.createTempFile();
+           File tempContext = new File(fileString);
            tempContext.deleteOnExit();
+           
            int contextFileId = H5Write.createFile(tempContext.getAbsolutePath());
            H5Write.writeInDataSet(contextFileId, "context", new ScilabString(context));
            H5Write.closeFile(contextFileId);
-           return tempContext;
+           return fileString;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HDF5Exception e) {
index ccc9ea5..b50b2aa 100644 (file)
@@ -16,10 +16,10 @@ package org.scilab.modules.xcos.block.actions;
 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.asynchronousScilabExec;
 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
 import static org.scilab.modules.xcos.utils.FileUtils.delete;
+import static org.scilab.modules.xcos.utils.FileUtils.exists;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.logging.LogFactory;
@@ -86,8 +86,8 @@ public class CodeGenerationAction extends SuperBlockSelectedAction {
                                /*
                                 * Prepare data
                                 */
-                               final File tempOutput = FileUtils.createTempFile();
-                               final File tempInput = FileUtils.createTempFile();
+                               final String tempOutput = FileUtils.createTempFile();
+                               final String tempInput = FileUtils.createTempFile();
                                
                            /*
                             * Export data
@@ -98,14 +98,14 @@ public class CodeGenerationAction extends SuperBlockSelectedAction {
                             * Prepare command and callback
                             */
                                String cmd = buildCall("xcosCodeGeneration",
-                                               tempOutput.getAbsolutePath(),
-                                               tempInput.getAbsolutePath());
+                                               tempOutput,
+                                               tempInput);
                                
                                final ActionListener callback = new ActionListener() {
                                        @Override
                                        public void actionPerformed(ActionEvent arg0) {
                                                
-                                               if (!tempInput.exists()) {
+                                               if (!exists(tempInput)) {
                                                        ((XcosDiagram) getGraph(null)).info(XcosMessages.EMPTY_INFO);
                                                        return;
                                                }
@@ -154,7 +154,7 @@ public class CodeGenerationAction extends SuperBlockSelectedAction {
      * @param tempInput Input file
      */
     private static void doAction(final SuperBlock block,
-                       final File tempInput) {
+                       final String tempInput) {
                try {
                        BasicBlock modifiedBlock = new H5RWHandler(tempInput).readBlock();
                        
index a6000e2..a86d32d 100644 (file)
@@ -17,7 +17,6 @@ package org.scilab.modules.xcos.block.actions;
 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
 
 import java.awt.event.ActionEvent;
-import java.io.File;
 import java.io.IOException;
 
 import org.apache.commons.logging.LogFactory;
@@ -91,7 +90,7 @@ public final class ViewDetailsAction extends VertexSelectionDependantAction {
                /*
                 * Export data
                 */
-               File temp;
+               String temp;
                try {
                        temp = FileUtils.createTempFile();
                        new H5RWHandler(temp).writeBlock(data);
@@ -103,9 +102,9 @@ public final class ViewDetailsAction extends VertexSelectionDependantAction {
                /*
                 * Build and execute the command
                 */
-               String cmd = buildCall("import_from_hdf5", temp.getAbsolutePath());
+               String cmd = buildCall("import_from_hdf5", temp);
                cmd += "tree_show(scs_m); ";
-               cmd += buildCall("deletefile", temp.getAbsolutePath());
+               cmd += buildCall("deletefile", temp);
                try {
                        ScilabInterpreterManagement.synchronousScilabExec(cmd);
                } catch (InterpreterException e1) {
index 1faae93..ca3b8f3 100644 (file)
@@ -415,12 +415,12 @@ public abstract class ContextUpdate extends BasicBlock {
                
                LOG_LOCAL.trace("Update the I/O value from the context");
                
-               File tempOutput;
-               File tempInput;
-               File tempContext;
+               String tempOutput;
+               String tempInput;
+               String tempContext;
                try {
                        tempInput = FileUtils.createTempFile();
-                       tempInput.deleteOnExit();
+                       new File(tempInput).deleteOnExit();
 
                        // Write scs_m
                        tempOutput = exportBlockStruct();
@@ -428,8 +428,8 @@ public abstract class ContextUpdate extends BasicBlock {
                        tempContext = exportContext(context);
 
                        String cmd = ScilabInterpreterManagement.buildCall("xcosBlockEval",
-                                       tempOutput.getAbsolutePath(), tempInput.getAbsolutePath(),
-                                       getInterfaceFunctionName().toCharArray(), tempContext.getAbsolutePath());
+                                       tempOutput, tempInput,
+                                       getInterfaceFunctionName().toCharArray(), tempContext);
 
                        try {
                                ScilabInterpreterManagement.synchronousScilabExec(cmd);
index 59ea362..4dda95e 100644 (file)
@@ -94,7 +94,7 @@ public class CompilationEngineStatus implements mxIEventListener, PropertyChange
                 */
                if (getCompilationData() == null || !getCompilationData().exists()) {
                        try {
-                               setCompilationData(FileUtils.createTempFile());
+                               setCompilationData(new File(FileUtils.createTempFile()));
                        } catch (IOException e) {
                                LOG.warn(e);
                                
index c1ddf80..6aa00f4 100644 (file)
@@ -66,7 +66,7 @@ public class PaletteDiagram extends XcosDiagram {
                File theFile = new File(diagramFileName);
 
                if (theFile.exists()) {
-                       boolean loaded = transformAndLoadFile(theFile, true);
+                       boolean loaded = transformAndLoadFile(diagramFileName, true);
                        if (!loaded) {
                                return false;
                        }
index a4ac951..71bc043 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.scilab.modules.xcos.graph;
 
+import static org.scilab.modules.xcos.utils.FileUtils.exists;
+
 import java.awt.Color;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
@@ -1518,8 +1520,8 @@ public class XcosDiagram extends ScilabGraph {
     /**
      * @param fileName HDF5 filename
      */
-    public void dumpToHdf5File(final File fileName) {
-       File writeFile = fileName;
+    public void dumpToHdf5File(final String fileName) {
+       String writeFile = fileName;
        if (fileName == null) {
            final FileChooser fc = ScilabFileChooser.createFileChooser();
            if (getSavedFile() != null) {
@@ -1535,7 +1537,7 @@ public class XcosDiagram extends ScilabGraph {
            if (fc.getSelection() == null || fc.getSelection().length == 0 || fc.getSelection()[0].equals("")) {
                return;
            }
-           writeFile = new File(fc.getSelection()[0]);
+           writeFile = fc.getSelection()[0];
        }
        
        new H5RWHandler(writeFile).writeDiagram(this);
@@ -2019,7 +2021,11 @@ public class XcosDiagram extends ScilabGraph {
                info(XcosMessages.LOADING_DIAGRAM);
 
                if (diagram.exists()) {
-                       transformAndLoadFile(diagram, false);
+                       try {
+                               transformAndLoadFile(diagram.getCanonicalPath(), false);
+                       } catch (IOException e) {
+                               LogFactory.getLog(XcosDiagram.class).error(e);
+                       }
                } else {
                        AnswerOption answer;
                        try {
@@ -2052,12 +2058,12 @@ public class XcosDiagram extends ScilabGraph {
      * @param wait wait end transform
      * @return the status of the operation
      */
-    protected boolean transformAndLoadFile(final File theFile, final boolean wait) {
-       final File fileToLoad = theFile;
+    protected boolean transformAndLoadFile(final String theFile, final boolean wait) {
+       final String fileToLoad = theFile;
        final XcosFileType filetype = XcosFileType.findFileType(fileToLoad);
        boolean result = false;
 
-       if (!fileToLoad.exists() || filetype == null) {
+       if (!exists(fileToLoad) || filetype == null) {
                XcosDialogs.couldNotLoadFile(this);
                return false;
        }
@@ -2066,14 +2072,14 @@ public class XcosDiagram extends ScilabGraph {
        case COSF:
        case COS:
            if (wait) {
-               File newFile;
+               String newFile;
                newFile = filetype.exportToHdf5(fileToLoad);
                result = transformAndLoadFile(newFile, wait);
            } else {
                final Thread transformAction = new Thread() {
                        @Override
                    public void run() {
-                       File newFile;
+                       String newFile;
                        newFile = filetype.exportToHdf5(fileToLoad);
                        transformAndLoadFile(newFile, false);
                    }
@@ -2085,15 +2091,16 @@ public class XcosDiagram extends ScilabGraph {
 
        case XCOS:
                try {
+                       File f = new File(theFile);
                        if (getModel().getChildCount(getDefaultParent()) == 0) {
-                       load(theFile);
-                       postLoad(theFile);
+                       load(f);
+                       postLoad(f);
                        setVisible(true);
                    } else {
                        info(XcosMessages.LOADING_DIAGRAM);
                        final XcosDiagram xcosDiagram = new XcosDiagram();
-                               xcosDiagram.load(theFile);
-                               xcosDiagram.postLoad(theFile);
+                               xcosDiagram.load(f);
+                               xcosDiagram.postLoad(f);
                                new XcosTab(xcosDiagram).setVisible(true);
                                info(XcosMessages.EMPTY_INFO);
                    }
@@ -2270,11 +2277,11 @@ public class XcosDiagram extends ScilabGraph {
                        }
                        str.append(']');
                                
-                       final File temp = FileUtils.createTempFile();
+                       final String temp = FileUtils.createTempFile();
                        
                        ScilabInterpreterManagement.synchronousScilabExec(
                                                  "vars = script2var(" + str.toString() + ", struct());"
-                                               + "export_to_hdf5('" + temp.getAbsolutePath() + "', 'vars');");
+                                               + "export_to_hdf5('" + temp + "', 'vars');");
                        
                        result = new H5RWHandler(temp).readContext();
                } catch (final IOException e) {
index 62e6cc4..c48aa01 100644 (file)
@@ -41,7 +41,7 @@ public class H5RWHandler {
        private static final String SCS_M = "scs_m";
        private static final Log LOG = LogFactory.getLog(H5RWHandler.class);
 
-       private final File h5File;
+       private final String h5File;
 
        /**
         * Constructor a new instance with a file.
@@ -50,7 +50,7 @@ public class H5RWHandler {
         *            the file path.
         */
        public H5RWHandler(File hdf5file) {
-               h5File = hdf5file;
+               h5File = hdf5file.getAbsolutePath();
        }
 
        /**
@@ -60,7 +60,7 @@ public class H5RWHandler {
         *            the file path.
         */
        public H5RWHandler(String hdf5file) {
-               h5File = new File(hdf5file);
+               h5File = hdf5file;
        }
 
        /*
@@ -93,7 +93,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int fileId = H5Read.openFile(h5File.getAbsolutePath());
+                       int fileId = H5Read.openFile(h5File);
                        H5Read.readDataFromFile(fileId, data);
                        H5Read.closeFile(fileId);
 
@@ -128,7 +128,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int handle = H5Read.openFile(h5File.getAbsolutePath());
+                       int handle = H5Read.openFile(h5File);
                        if (handle >= 0) {
                                H5Read.readDataFromFile(handle, list);
                        }
@@ -191,7 +191,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int fileId = H5Read.openFile(h5File.getAbsolutePath());
+                       int fileId = H5Read.openFile(h5File);
 
                        H5Read.readDataFromFile(fileId, data);
                        H5Read.closeFile(fileId);
@@ -241,7 +241,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int fileId = H5Write.createFile(h5File.getAbsolutePath());
+                       int fileId = H5Write.createFile(h5File);
 
                        H5Write.writeInDataSet(fileId, SCS_M, data);
 
@@ -275,7 +275,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int fileId = H5Write.createFile(h5File.getAbsolutePath());
+                       int fileId = H5Write.createFile(h5File);
 
                        H5Write.writeInDataSet(fileId, CONTEXT, string);
 
@@ -304,7 +304,7 @@ public class H5RWHandler {
                }
 
                try {
-                       int fileId = H5Write.createFile(h5File.getAbsolutePath());
+                       int fileId = H5Write.createFile(h5File);
 
                        H5Write.writeInDataSet(fileId, SCS_M, data);
 
index 9b4867f..9636c2c 100644 (file)
@@ -23,6 +23,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.commons.logging.LogFactory;
+import org.scilab.modules.commons.ScilabCommons;
 import org.scilab.modules.commons.ScilabConstants;
 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
 import org.w3c.dom.Document;
@@ -124,9 +125,8 @@ public final class FileUtils {
         * @return a new unique temporary file.
         * @throws IOException when an error occurs
         */
-       public static File createTempFile() throws IOException {
-               return File.createTempFile(XcosFileType.XCOS.getExtension(),
-                               XcosFileType.HDF5.getDottedExtension());
+       public static String createTempFile() throws IOException {
+               return ScilabCommons.createtempfilename(XcosFileType.HDF5.getExtension(), 1);
        }
        
        /**
@@ -140,6 +140,25 @@ public final class FileUtils {
        }
        
        /**
+        * Delete the file and log an error message if unable to do so.
+        * @param f the file to delete.
+        */
+       public static void delete(String f) {
+               if (!new File(f).delete()) {
+                       LogFactory.getLog(FileUtils.class).error(XcosMessages.UNABLE_TO_DELETE + f);
+               }
+       }
+       
+       /**
+        * Test if the file exists.
+        * @param f the file check.
+        * @return true if the file exists, false otherwise.
+        */
+       public static boolean exists(String f) {
+               return new File(f).exists();
+       }
+       
+       /**
         * Decode the style into the passed stylesheet.
         * 
         * @param styleSheet the current stylesheet
index 6c40575..a8c2d94 100644 (file)
@@ -12,7 +12,6 @@
 
 package org.scilab.modules.xcos.utils;
 
-import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.Arrays;
@@ -42,7 +41,7 @@ public enum XcosFileType {
                 * @return The HDF5 formatted file
                 */
                @Override
-               public File exportToHdf5(File arg0) {
+               public String exportToHdf5(String arg0) {
                        return loadScicosDiagram(arg0);
                }
        },
@@ -56,7 +55,7 @@ public enum XcosFileType {
                 * @return The HDF5 formatted file
                 */
                @Override
-               public File exportToHdf5(File arg0) {
+               public String exportToHdf5(String arg0) {
                        return loadScicosDiagram(arg0);
                }
        },
@@ -70,7 +69,7 @@ public enum XcosFileType {
                 * @return The HDF5 formatted file
                 */
                @Override
-               public File exportToHdf5(File arg0) {
+               public String exportToHdf5(String arg0) {
                        return arg0;
                }
        };
@@ -124,13 +123,13 @@ public enum XcosFileType {
         * @param theFile Current file
         * @return The determined filetype
         */
-       public static XcosFileType findFileType(File theFile) {
-               int dotPos = theFile.getName().lastIndexOf('.');
+       public static XcosFileType findFileType(String theFile) {
+               int dotPos = theFile.lastIndexOf('.');
                String extension = "";
                XcosFileType retValue = null;
 
-               if (dotPos > 0 && dotPos <= theFile.getName().length() - 2) {
-                       extension = theFile.getName().substring(dotPos + 1);
+               if (dotPos > 0 && dotPos <= theFile.length() - 2) {
+                       extension = theFile.substring(dotPos + 1);
                }
                
                for (XcosFileType currentFileType : XcosFileType.values()) {
@@ -153,7 +152,7 @@ public enum XcosFileType {
         * @param theFile the file to check
         * @return the found file type
         */
-       private static XcosFileType checkXmlHeader(File theFile) {
+       private static XcosFileType checkXmlHeader(String theFile) {
                XcosFileType retValue = null;
                
                final byte[] xmlMagic = "<?xml".getBytes();
@@ -201,7 +200,7 @@ public enum XcosFileType {
         * @param file The file to convert
         * @return The created file
         */
-       public File exportToHdf5(File file) {
+       public String exportToHdf5(String file) {
            throw new Error("Not implemented operation");
        }
        
@@ -253,21 +252,21 @@ public enum XcosFileType {
         * @param filename The file to execute in scilab.
         * @return The exported data in hdf5.
         */
-       public static File loadScicosDiagram(File filename) {
-           File tempOutput = null;
+       public static String loadScicosDiagram(String filename) {
+           String tempOutput = null;
            try {
                tempOutput = FileUtils.createTempFile();
                
                StringBuilder cmd = new StringBuilder();
                cmd.append("scs_m = importScicosDiagram(\"");
-               cmd.append(filename.getAbsolutePath());
+               cmd.append(filename);
                cmd.append("\");");
                cmd.append("result = export_to_hdf5(\"");
-               cmd.append(tempOutput.getAbsolutePath());
+               cmd.append(tempOutput);
                cmd.append("\", \"scs_m\");");
                
                cmd.append("if result <> %t then deletefile(\"");
-               cmd.append(tempOutput.getAbsolutePath());
+               cmd.append(tempOutput);
                cmd.append("\"); end; ");
                
                try {