changeset 143:fcb81fa2df0d

handle urls and java resources as files git-svn-id: https://luan-java.googlecode.com/svn/trunk@144 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 13 Jun 2014 19:04:05 +0000
parents 05f8c21160ef
children 2e92f0a6fcac
files src/luan/Luan.java src/luan/LuanState.java src/luan/lib/BasicLib.java src/luan/lib/IoLib.java src/luan/lib/PackageLib.java src/luan/lib/Utils.java
diffstat 6 files changed, 62 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/Luan.java
--- a/src/luan/Luan.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/Luan.java	Fri Jun 13 19:04:05 2014 +0000
@@ -9,7 +9,7 @@
 	public static void main(String[] args) {
 		LuanState luan = LuanState.newStandard();
 		try {
-			LuanFunction standalone = (LuanFunction)BasicLib.load_java_resource(luan,"luan/tools/cmd_line.luan");
+			LuanFunction standalone = (LuanFunction)BasicLib.load_file(luan,"java:luan/tools/cmd_line.luan");
 			luan.call(standalone,args);
 		} catch(LuanException e) {
 			System.err.println(e.getMessage());
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/LuanState.java
--- a/src/luan/LuanState.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/LuanState.java	Fri Jun 13 19:04:05 2014 +0000
@@ -143,7 +143,7 @@
 		try {
 			LuanState luan = LuanCompiler.newLuanState();
 			luan.load("Package",PackageLib.LOADER);
-			BasicLib.do_java_resource(luan,"luan/lib/init.luan");
+			BasicLib.do_file(luan,"java:luan/lib/init.luan");
 			return luan;
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/lib/BasicLib.java
--- a/src/luan/lib/BasicLib.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri Jun 13 19:04:05 2014 +0000
@@ -50,9 +50,6 @@
 				add( global, "to_string", LuanState.class, Object.class );
 				add( global, "type", Object.class );
 				global.put( "_VERSION", Luan.version );
-				add( module, "do_java_resource", LuanState.class, String.class );
-				add( module, "load_java_resource", LuanState.class, String.class );
-//				add( module, "new_luan" );
 				add( module, "values", new Object[0].getClass() );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -82,32 +79,18 @@
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
-			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text();
+			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : IoLib.luanIo(luan,fileName).read_text();
 			return load(luan,src,fileName,false,false);
 		} catch(IOException e) {
 			throw luan.exception(e);
 		}
 	}
 
-	public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException {
-		try {
-			String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text();
-			return load(luan,src,path,false,false);
-		} catch(IOException e) {
-			throw luan.exception(e);
-		}
-	}
-
 	public static Object do_file(LuanState luan,String fileName) throws LuanException {
 		LuanFunction fn = load_file(luan,fileName);
 		return luan.call(fn);
 	}
 
-	public static Object do_java_resource(LuanState luan,String path) throws LuanException {
-		LuanFunction fn = load_java_resource(luan,path);
-		return luan.call(fn);
-	}
-
 	private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) {
 		return new LuanFunction() {
 			@Override public Object[] call(LuanState luan,Object[] args) {
@@ -240,9 +223,5 @@
 			}
 		};
 	}
-/*
-	public static LuanTable new_luan() {
-		return LuanState.newStandard().global();
-	}
-*/
+
 }
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/lib/IoLib.java
--- a/src/luan/lib/IoLib.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/lib/IoLib.java	Fri Jun 13 19:04:05 2014 +0000
@@ -37,10 +37,7 @@
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
 			try {
-				add( module, "file", String.class );
-				add( module, "java_resource_to_url", String.class );
-				add( module, "url", String.class );
-				add( module, "java_resource", String.class );
+				add( module, "file", LuanState.class, String.class );
 				add( module, "read_console_line", String.class );
 
 				LuanTable stdin = new LuanTable();
@@ -91,15 +88,6 @@
 		return blocks(System.in,n);
 	}
 
-	public static String java_resource_to_url(String path) {
-		URL url = ClassLoader.getSystemResource(path);
-		return url==null ? null : url.toString();
-	}
-
-	public static LuanTable java_resource(String path) throws IOException {
-		return url(java_resource_to_url(path));
-	}
-
 	public static String read_console_line(String prompt) throws IOException {
 		if( prompt==null )
 			prompt = "> ";
@@ -224,6 +212,7 @@
 
 	public static abstract class LuanIn {
 		abstract InputStream inputStream() throws IOException;
+		abstract String name();
 
 		public String read_text() throws IOException {
 			Reader in = new InputStreamReader(inputStream());
@@ -251,6 +240,9 @@
 		LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
+				tbl.put( "name", new LuanJavaFunction(
+					LuanIn.class.getMethod( "name" ), this
+				) );
 				tbl.put( "read_text", new LuanJavaFunction(
 					LuanIn.class.getMethod( "read_text" ), this
 				) );
@@ -328,10 +320,10 @@
 		InputStream inputStream() throws IOException {
 			return url.openStream();
 		}
-	}
 
-	public static LuanTable url(String s) throws MalformedURLException {
-		return new LuanUrl(s).table();
+		String name() {
+			return url.toString();
+		}
 	}
 
 	public static final class LuanFile extends LuanIO {
@@ -348,10 +340,27 @@
 		OutputStream outputStream() throws IOException {
 			return new FileOutputStream(file);
 		}
+
+		String name() {
+			return file.toString();
+		}
 	}
 
-	public static LuanTable file(String name) {
-		return new LuanFile(name).table();
+	public static LuanIn luanIo(LuanState luan,String name) throws LuanException {
+		if( Utils.isFile(name) )
+			return new LuanFile(name);
+		String url = Utils.toUrl(name);
+		if( url == null )
+			throw luan.exception( "file '"+name+"' not found" );
+		try {
+			return new LuanUrl(url);
+		} catch(MalformedURLException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public static LuanTable file(LuanState luan,String name) throws LuanException {
+		return luanIo(luan,name).table();
 	}
 
 	public static final class LuanSocket extends LuanIO {
@@ -373,6 +382,10 @@
 			return socket.getOutputStream();
 		}
 
+		String name() {
+			return socket.toString();
+		}
+
 		public LuanTable pickle_client(LuanState luan) throws IOException {
 			DataInputStream in = new DataInputStream(new BufferedInputStream(inputStream()));
 			DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outputStream()));
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/lib/PackageLib.java
--- a/src/luan/lib/PackageLib.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/lib/PackageLib.java	Fri Jun 13 19:04:05 2014 +0000
@@ -1,8 +1,6 @@
 package luan.lib;
 
-import java.io.File;
 import java.io.IOException;
-import java.net.URL;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -34,7 +32,6 @@
 			LuanTable searchers = luan.searchers();
 			searchers.add(preloadSearcher);
 			searchers.add(fileSearcher);
-			searchers.put("java",javaFileSearcher);
 			module.put("searchers",searchers);
 			return module;
 		}
@@ -54,17 +51,7 @@
 			if( searchers == null ) {
 				list = Collections.<Object>singletonList(preloadSearcher);
 			} else {
-				int i = modName.indexOf(':');
-				if( i != -1 ) {
-					String prefix = modName.substring(0,i);
-					Object searcher = searchers.get(prefix);
-					if( searcher != null ) {
-						list = Collections.<Object>singletonList(searcher);
-						searchFor = modName.substring(i+1);
-					}
-				}
-				if( list == null )
-					list = searchers.asList();
+				list = searchers.asList();
 			}
 			for( Object s : list ) {
 				LuanFunction searcher = (LuanFunction)s;
@@ -92,7 +79,7 @@
 	public static String search_path(String name,String path) {
 		for( String s : path.split(";") ) {
 			String file = s.replaceAll("\\?",name);
-			if( new File(file).exists() )
+			if( Utils.exists(file) )
 				return file;
 		}
 		return null;
@@ -126,33 +113,6 @@
 
 
 
-
-	public static final LuanFunction javaFileLoader = new LuanFunction() {
-		@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-			String urlStr = (String)args[1];
-			String path = (String)args[2];
-			try {
-				String src = new IoLib.LuanUrl(urlStr).read_text();
-				LuanFunction fn = BasicLib.load(luan,src,path,false,false);
-				return fn.call(luan,args);
-			} catch(IOException e) {
-				throw luan.exception(e);
-			}
-		}
-	};
-
-	public static final LuanFunction javaFileSearcher = new LuanFunction() {
-		@Override public Object[] call(LuanState luan,Object[] args) {
-			String path = (String)args[0];
-			String url = IoLib.java_resource_to_url(path);
-			if( url != null ) {
-				return new Object[]{javaFileLoader,url,path};
-			}
-			return LuanFunction.NOTHING;
-		}
-	};
-
-
 	public static LuanFunction get_loader(String path)
 		throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException
 	{
diff -r 05f8c21160ef -r fcb81fa2df0d src/luan/lib/Utils.java
--- a/src/luan/lib/Utils.java	Fri Jun 13 17:05:31 2014 +0000
+++ b/src/luan/lib/Utils.java	Fri Jun 13 19:04:05 2014 +0000
@@ -5,6 +5,9 @@
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.File;
+import java.net.URL;
+import java.net.MalformedURLException;
 import luan.LuanState;
 import luan.LuanException;
 
@@ -49,4 +52,26 @@
 		return out.toByteArray();
 	}
 
+	public static boolean isFile(String path) {
+		return new File(path).exists();
+	}
+
+	public static String toUrl(String path) {
+		if( path.indexOf(':') == -1 )
+			return null;
+		if( path.startsWith("java:") ) {
+			path = path.substring(5);
+			URL url = ClassLoader.getSystemResource(path);
+			return url==null ? null : url.toString();
+		}
+		try {
+			new URL(path);
+			return path;
+		} catch(MalformedURLException e) {}
+		return null;
+	}
+
+	public static boolean exists(String path) {
+		return isFile(path) || toUrl(path)!=null;
+	}
 }