Revision: 1976
          http://svn.sourceforge.net/vexi/?rev=1976&view=rev
Author:   mkpg2
Date:     2007-07-10 09:15:26 -0700 (Tue, 10 Jul 2007)

Log Message:
-----------
Fix. Couldn't delete files on windows. Was due to io streams not being closed.

Modified Paths:
--------------
    core/trunk/org.vexi.core/src/org/vexi/core/Resources.java
    core/trunk/org.vexi.core/src/org/vexi/core/Vexi.jpp
    core/trunk/org.vexi.core/src_junit/test/core/stream/TestStream.java
    core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf8.t

Added Paths:
-----------
    core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf82.t

Modified: core/trunk/org.vexi.core/src/org/vexi/core/Resources.java
===================================================================
--- core/trunk/org.vexi.core/src/org/vexi/core/Resources.java   2007-07-10 
15:22:32 UTC (rev 1975)
+++ core/trunk/org.vexi.core/src/org/vexi/core/Resources.java   2007-07-10 
16:15:26 UTC (rev 1976)
@@ -3,14 +3,12 @@
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.io.Writer;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.text.DateFormat;
@@ -27,7 +25,10 @@
 import org.ibex.util.Basket;
 import org.ibex.util.Callable;
 import org.ibex.util.Encode;
+import org.ibex.util.InputStreamToByteArray;
 import org.ibex.util.Log;
+import org.ibex.util.Tree;
+import org.ibex.util.XML;
 import org.ibex.util.Basket.Stack;
 import org.vexi.plat.Platform;
 
@@ -36,7 +37,7 @@
  * 
  * @author mikeg
  */
-public class Resources {
+public class Resources implements Constants{
 
        private static final String fileSep = 
System.getProperty("file.separator");
 
@@ -434,7 +435,19 @@
        }
        
     }*/
-    
+    public static JS parseUTF8(JS arg0) throws JSExn {
+       if(arg0 == null) return null;
+       try {
+               InputStream is = JSU.getInputStream(arg0);
+               if(is==null) return SC_;
+               try{
+                       return JSU.S(new 
String(InputStreamToByteArray.convert(is)));
+               }finally{
+                       is.close();
+               }
+       }catch (Exception e) { Log.warn(Resources.class, e); }
+       return null;
+    }
 
 
        public static void writeUTF8(JS[] args) throws JSExn {
@@ -449,6 +462,12 @@
                        throw new JSExn(e.getMessage());
                }
        } 
+       public static void parseXML(JS[] args) throws JSExn{
+               if(args[0] == null) return;
+               new XMLHelper(args[1]).doParse(args[0]);
+               return;
+       }
+       
        public static JS writeXML(JS f) throws JSExn {
                try{
                        final BufferedWriter out = new BufferedWriter(
@@ -507,4 +526,99 @@
                 
        }
                
+       
+    
+    static private class XMLHelper extends XML {
+        private class Wrapper extends XML.Exn {
+               public JSExn wrapee;
+               public Wrapper(JSExn jse) {
+                       super("");
+                       wrapee = jse;
+               }
+        }
+        
+        private JS characters, whitespace, endElement, startElement;
+        public XMLHelper(JS b) throws JSExn {
+            super(BUFFER_SIZE, true);
+            startElement = b.getAndTriggerTraps(SC_startElement);
+            endElement   = b.getAndTriggerTraps(SC_endElement);
+            characters   = b.getAndTriggerTraps(SC_characters);
+            whitespace   = b.getAndTriggerTraps(SC_whitespace);
+        }
+
+        private final JS[] callargs1 = new JS[1], callargs2 = new JS[2], 
callargs3 = new JS[3];
+        public void startElement(Tree.Element c) throws XML.Exn {
+               try {
+                Tree.Attributes a = c.getAttributes();
+                JS attrs = new JS.Obj();
+                // FIXME attribute URIs? add an additional hash?
+                for(int i=0; i<a.attrSize(); i++) 
attrs.put(JSU.S(a.getKey(i)), JSU.S(a.getVal(i)));
+                callargs3[0] = JSU.S(c.getLocalName());
+                callargs3[1] = attrs;
+                callargs3[2] = JSU.S(c.getUri());
+                startElement.call(null, callargs3);
+               } catch (JSExn jse) {
+                       throw new Wrapper(jse);
+               } finally {
+                       callargs3[0] = callargs3[1] = callargs3[2] = null;
+               }
+        }
+
+        public void endElement(Tree.Element c) throws XML.Exn {
+               try {
+                callargs2[0] = JSU.S(c.getLocalName());
+                callargs2[1] = JSU.S(c.getUri());
+                endElement.call(null, callargs2);
+               } catch (JSExn jse) {
+                       throw new Wrapper(jse);
+               } finally {
+                       callargs2[0] = callargs2[1] = null;
+               }
+        }
+        
+        public void characters(char[] ch, int start, int length) throws 
XML.Exn {
+               try {
+                callargs1[0] = JSU.S(new String(ch, start, length));
+                characters.call(null, callargs1);
+               } catch (JSExn jse) {
+                       throw new Wrapper(jse);
+               } finally {
+                       callargs1[0] = null;
+               }
+        }
+
+        public void whitespace(char[] ch, int start, int length) throws 
XML.Exn {
+               try {
+                callargs1[0] = JSU.S(new String(ch, start, length));
+                if(whitespace!=null)whitespace.call(null, callargs1);
+               } catch (JSExn jse) {
+                       throw new Wrapper(jse);
+               } finally {
+                       callargs1[0] = null;
+               }
+        }
+
+        public void doParse(JS s) throws JSExn {
+            try { 
+               InputStream is = JSU.getInputStream(s);
+               if(is==null) return;
+               BufferedReader br = new BufferedReader(new 
InputStreamReader(is)); 
+               try{
+                       parse(br);
+               }finally{
+                       br.close();
+               }
+            } catch (Wrapper e) {
+                throw e.wrapee;
+            } catch (XML.Exn e) {
+                throw new JSExn("error parsing XML: " + e.toString());
+            } catch (IOException e) {
+                Log.warn(this, "IO Exception while reading from file");
+                Log.warn(this, e);
+                throw new JSExn("error reading from Resource");
+            }
+        }
+    }
+
+  
 }

Modified: core/trunk/org.vexi.core/src/org/vexi/core/Vexi.jpp
===================================================================
--- core/trunk/org.vexi.core/src/org/vexi/core/Vexi.jpp 2007-07-10 15:22:32 UTC 
(rev 1975)
+++ core/trunk/org.vexi.core/src/org/vexi/core/Vexi.jpp 2007-07-10 16:15:26 UTC 
(rev 1976)
@@ -4,11 +4,6 @@
 
 package org.vexi.core;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.InputStream;
-
 import org.ibex.crypto.MD5;
 import org.ibex.js.Fountain;
 import org.ibex.js.JS;
@@ -22,11 +17,8 @@
 import org.ibex.util.Cache;
 import org.ibex.util.Callable;
 import org.ibex.util.Encode;
-import org.ibex.util.InputStreamToByteArray;
 import org.ibex.util.Log;
 import org.ibex.util.Pausable;
-import org.ibex.util.Tree;
-import org.ibex.util.XML;
 import org.ibex.util.Pausable.NotPausableException;
 import org.vexi.graphics.Font;
 import org.vexi.net.HTTP;
@@ -237,16 +229,10 @@
                        case "stream.cache":
                            //try { return args[0] == null ? null : new 
Fountain.CachedStream((Stream)args[0], "resources", true); }
                            //catch (Stream.NotCacheableException e) { throw 
new JSExn("this resource cannot be cached"); }
-                       case "stream.write.xml":
-                               return Resources.writeXML(args[0]);
+                       case "stream.write.xml": return 
Resources.writeXML(args[0]);
                        case "stream.parse.html": throw new JSExn("not 
implemented yet"); //return null;
                            // FIXME backgrounding
-                       case "stream.parse.utf8": if(args[0] == null) return 
null;
-                               try {
-                                       InputStream is = 
JSU.getInputStream(args[0]);
-                                       if(is==null) return SC_;
-                                       return JSU.S(new 
String(InputStreamToByteArray.convert(is))); }
-                           catch (Exception e) { Log.warn(this, e); }
+                       case "stream.parse.utf8": return 
Resources.parseUTF8(args[0]);
                        case "thread.sleep": sleep(JSU.toInt(args[0])); return 
null;
                        case "ui.browser": 
Platform.newBrowserWindow(JSU.toString(args[0])); return null;
                     case "ui.insets":
@@ -268,7 +254,7 @@
                 case 2:
                     //#switch(JSU.toString(method))
                        case "regexp": return new JSRegexp(args[0], args[1]);
-                       case "stream.parse.xml": if(args[0] == null) return 
null; new XMLHelper(args[1]).doParse(args[0]); return null;
+                       case "stream.parse.xml": Resources.parseXML(args); 
return null;
                     case "stream.watch":
                         final JS func = args[1];
                         return new Fountain.ProgressWatcher((Fountain)args[0],
@@ -278,9 +264,7 @@
                                                                     return 
func.call(null, args);
                                                                 }
                                                             });
-                    case "stream.write.utf8": 
-                       Resources.writeUTF8(args);
-                       return null;
+                    case "stream.write.utf8": Resources.writeUTF8(args); 
return null;
                     //#end
                 case 3:
                     //#switch(JSU.toString(method))
@@ -385,94 +369,6 @@
             }
     };
 
-    
-    private class XMLHelper extends XML {
-        private class Wrapper extends XML.Exn {
-               public JSExn wrapee;
-               public Wrapper(JSExn jse) {
-                       super("");
-                       wrapee = jse;
-               }
-        }
-        
-        private JS characters, whitespace, endElement, startElement;
-        public XMLHelper(JS b) throws JSExn {
-            super(BUFFER_SIZE, true);
-            startElement = b.getAndTriggerTraps(SC_startElement);
-            endElement   = b.getAndTriggerTraps(SC_endElement);
-            characters   = b.getAndTriggerTraps(SC_characters);
-            whitespace   = b.getAndTriggerTraps(SC_whitespace);
-        }
-
-        private final JS[] callargs1 = new JS[1], callargs2 = new JS[2], 
callargs3 = new JS[3];
-        public void startElement(Tree.Element c) throws XML.Exn {
-               try {
-                Tree.Attributes a = c.getAttributes();
-                JS attrs = new JS.Obj();
-                // FIXME attribute URIs? add an additional hash?
-                for(int i=0; i<a.attrSize(); i++) 
attrs.put(JSU.S(a.getKey(i)), JSU.S(a.getVal(i)));
-                callargs3[0] = JSU.S(c.getLocalName());
-                callargs3[1] = attrs;
-                callargs3[2] = JSU.S(c.getUri());
-                startElement.call(null, callargs3);
-               } catch (JSExn jse) {
-                       throw new Wrapper(jse);
-               } finally {
-                       callargs3[0] = callargs3[1] = callargs3[2] = null;
-               }
-        }
-
-        public void endElement(Tree.Element c) throws XML.Exn {
-               try {
-                callargs2[0] = JSU.S(c.getLocalName());
-                callargs2[1] = JSU.S(c.getUri());
-                endElement.call(null, callargs2);
-               } catch (JSExn jse) {
-                       throw new Wrapper(jse);
-               } finally {
-                       callargs2[0] = callargs2[1] = null;
-               }
-        }
-        
-        public void characters(char[] ch, int start, int length) throws 
XML.Exn {
-               try {
-                callargs1[0] = JSU.S(new String(ch, start, length));
-                characters.call(null, callargs1);
-               } catch (JSExn jse) {
-                       throw new Wrapper(jse);
-               } finally {
-                       callargs1[0] = null;
-               }
-        }
-
-        public void whitespace(char[] ch, int start, int length) throws 
XML.Exn {
-               try {
-                callargs1[0] = JSU.S(new String(ch, start, length));
-                if(whitespace!=null)whitespace.call(null, callargs1);
-               } catch (JSExn jse) {
-                       throw new Wrapper(jse);
-               } finally {
-                       callargs1[0] = null;
-               }
-        }
-
-        public void doParse(JS s) throws JSExn {
-            try { 
-               InputStream is = JSU.getInputStream(s);
-               if(is==null) return;
-                parse(new BufferedReader(new InputStreamReader(is)));
-            } catch (Wrapper e) {
-                throw e.wrapee;
-            } catch (XML.Exn e) {
-                throw new JSExn("error parsing XML: " + e.toString());
-            } catch (IOException e) {
-                Log.warn(this, "IO Exception while reading from file");
-                Log.warn(this, e);
-                throw new JSExn("error reading from Resource");
-            }
-        }
-    }
-
     // FEATURE: move this into builtin.vexi
     // From JS thread.
     static private Blessing bless_jsthread(final JS vexi, final JS fountain) 
throws JSExn {

Modified: core/trunk/org.vexi.core/src_junit/test/core/stream/TestStream.java
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/stream/TestStream.java 
2007-07-10 15:22:32 UTC (rev 1975)
+++ core/trunk/org.vexi.core/src_junit/test/core/stream/TestStream.java 
2007-07-10 16:15:26 UTC (rev 1976)
@@ -41,8 +41,13 @@
        public static Test suite() {
        return suite(new TestStream());
     }
-    
-       
+
+       protected boolean filter(String name) {
+               return super.filter(name) 
+               //&& !name.contains("saveloadxml")
+               //&& !name.contains("saveloadutf8")
+               ;
+       }
        static private class TestStreamCase extends CoreTestCase{
 
                public TestStreamCase(String[] resourceDirs, String 
templateFileName) {
@@ -67,9 +72,16 @@
                                        File save_ = new 
File(tmpDir,"save.txt");
                                        TestCase.assertTrue(!save_.exists() || 
save_.delete());
                                        Fountain.File save = new 
Fountain.File(save_.getPath(),true);
-                                       
-                                       JS function = Util.getStatic(v, main, 
"runtest");
-                                       Thread.runInNew(function, new 
JS[]{save});
+                                       try{
+                                               JS function = Util.getStatic(v, 
main, "runtest");
+                                               Thread.runInNew(function, new 
JS[]{save});
+                                       }finally{
+                                           // REMARK - seems to be ok here, 
but then
+                                           // we cannot do the same before the 
next testcase!?
+                                               File tmpDir2 = 
Util.createTmpDir();
+                                               File save_2 = new 
File(tmpDir2,"save.txt");
+                                               
TestCase.assertTrue(!save_2.exists() || save_2.delete());
+                                       }
                                        return null;
                                }
                        });

Modified: core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf8.t
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf8.t  
2007-07-10 15:22:32 UTC (rev 1975)
+++ core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf8.t  
2007-07-10 16:15:26 UTC (rev 1976)
@@ -8,16 +8,5 @@
     };
     
     <ui:box/>
-    
-    /*
-       vexi.thread = function(){
-               vexi.log.info("Starting file dialog test ...");
-               var outstream = vexi.stream.file.save();
-                       vexi.stream.write.utf8(outstream,"1\n2\n3");
-               
-                       var instream = vexi.stream.file.load();
-                       var x = vexi.stream.parse.utf8(instream);
-                       vexi.log.info(x);
-       };*/
                
 </vexi>
\ No newline at end of file

Added: core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf82.t
===================================================================
--- core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf82.t         
                (rev 0)
+++ core/trunk/org.vexi.core/src_junit/test/core/stream/saveloadutf82.t 
2007-07-10 16:15:26 UTC (rev 1976)
@@ -0,0 +1,17 @@
+<vexi xmlns:ui="vexi://ui" xmlns="">
+    // REMARK - repeat of saveloadutf8. 
+    // This is to check that the file is deleteable on windows
+    // , which requires that all streams be closed.
+    // Done like this as strangely it wasn't possible to test the file
+    // immediately after executing the test.
+    static.runtest = function(outstream){
+       vexi.log.info(outstream);
+               vexi.stream.write.utf8(outstream,"1\n2\n3");
+               var x = vexi.stream.parse.utf8(outstream);
+               .util..assertEquals("1\n2\n3",x);
+               vexi.log.info(x);
+    };
+    
+    <ui:box/>
+
+</vexi>
\ No newline at end of file


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to