changeset 260:f1f7d8c7e94e

add Io.protocols git-svn-id: https://luan-java.googlecode.com/svn/trunk@261 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 28 Oct 2014 23:25:13 +0000
parents f9b201530b85
children 715c4a6e1169
files core/src/luan/cmd_line.luan core/src/luan/impl/LuanParser.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/IoLuan.java core/src/luan/modules/PackageLuan.java core/src/luan/modules/Utils.java dist/jars/luan-core-trunk.jar dist/jars/luan-logging-trunk.jar dist/jars/luan-lucene-trunk.jar dist/jars/luan-mail-trunk.jar dist/jars/luan-web-trunk.jar web/src/luan/modules/web/Web_server.luan
diffstat 12 files changed, 93 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/cmd_line.luan	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/cmd_line.luan	Tue Oct 28 23:25:13 2014 +0000
@@ -67,7 +67,7 @@
 		_G.arg[j-i] = v
 	end
 	try
-		local main_file = load_file(file)
+		local main_file = load_file("file:"..file)
 		main_file( Table.unpack(_G.arg) )
 	catch e do
 		Io.print_to(Io.stderr, e )
--- a/core/src/luan/impl/LuanParser.java	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/impl/LuanParser.java	Tue Oct 28 23:25:13 2014 +0000
@@ -357,6 +357,8 @@
 		int i = modName.lastIndexOf('/');
 		if( i == -1 )
 			i = modName.lastIndexOf('.');
+		if( i == -1 )
+			i = modName.lastIndexOf(':');
 		String varName = modName.substring(i+1);
 		if( !isValidName(varName) )
 			throw parser.exception("invalid variable name '"+varName+"' in import");
--- a/core/src/luan/modules/BasicLuan.java	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/modules/BasicLuan.java	Tue Oct 28 23:25:13 2014 +0000
@@ -75,7 +75,16 @@
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
-			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : IoLuan.luanIo(luan,fileName).read_text();
+			String src;
+			if( fileName==null ) {
+				src = Utils.readAll(new InputStreamReader(System.in));
+			} else {
+				LuanTable t = IoLuan.get(luan,fileName);
+				if( t == null )
+					throw luan.exception( "file '"+fileName+"' not found" );
+				LuanFunction fn = (LuanFunction)t.get("read_text");
+				src = (String)Luan.first(luan.call(fn));
+			}
 			return load(luan,src,fileName,null,false);
 		} catch(IOException e) {
 			throw luan.exception(e);
--- a/core/src/luan/modules/IoLuan.java	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Tue Oct 28 23:25:13 2014 +0000
@@ -38,9 +38,12 @@
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = Luan.newTable();
 			try {
-				add( module, "File", LuanState.class, String.class );
 				add( module, "read_console_line", String.class );
 
+				module.put( "protocols", newProtocols() );
+
+				add( module, "get", LuanState.class, String.class );
+
 				LuanTable stdin = Luan.newTable();
 				stdin.put( "read_text", new LuanJavaFunction(
 					IoLuan.class.getMethod( "stdin_read_text" ), null
@@ -410,20 +413,73 @@
 		}
 	}
 
-	public static LuanIn luanIo(LuanState luan,String name) throws LuanException {
-		check(luan,name);
-		File file = Utils.toFile(name);
-		if( file != null )
-			return new LuanFile(file);
-		URL url = Utils.toUrl(name);
-		if( url != null ) {
-			return new LuanUrl(url);
-		}
-		throw luan.exception( "file '"+name+"' not found" );
+	public static LuanTable file(LuanState luan,String name) throws LuanException {
+		File file = new File(name);
+		if( !file.exists() )
+			return null;
+		return new LuanFile(file).table();
 	}
 
-	public static LuanTable File(LuanState luan,String name) throws LuanException {
-		return luanIo(luan,name).table();
+	public static LuanTable classpath(LuanState luan,String path) throws LuanException {
+		if( path.contains("//") )
+			return null;
+		URL url;
+		if( !path.contains("#") ) {
+			url = ClassLoader.getSystemResource(path);
+		} else {
+			String[] a = path.split("#");
+			url = ClassLoader.getSystemResource(a[0]);
+			if( url==null ) {
+				for( int i=1; i<a.length; i++ ) {
+					url = ClassLoader.getSystemResource(a[0]+"/"+a[i]);
+					if( url != null ) {
+						try {
+							url = new URL(url,".");
+						} catch(MalformedURLException e) {
+							throw new RuntimeException(e);
+						}
+						break;
+					}
+				}
+			}
+		}
+		if( url == null )
+			return null;
+		return new LuanUrl(url).table();
+	}
+
+	private static LuanTable newProtocols() {
+		LuanTable protocols = Luan.newTable();
+		try {
+			add( protocols, "file", LuanState.class, String.class );
+			add( protocols, "classpath", LuanState.class, String.class );
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+		return protocols;
+	}
+
+	private static LuanTable protocols(LuanState luan) {
+		LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("Io");
+		if( t == null )
+			return newProtocols();
+		t = (LuanTable)t.get("protocols");
+		if( t == null )
+			return newProtocols();
+		return t;
+	}
+
+	public static LuanTable get(LuanState luan,String name) throws LuanException {
+		int i = name.indexOf(':');
+		if( i == -1 )
+			throw luan.exception( "invalid IO name '"+name+"', missing protocol" );
+		String protocol = name.substring(0,i);
+		String location = name.substring(i+1);
+		LuanTable protocols = protocols(luan);
+		LuanFunction opener = (LuanFunction)protocols.get(protocol);
+		if( opener == null )
+			throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" );
+		return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location}));
 	}
 
 	public static final class LuanSocket extends LuanIO {
--- a/core/src/luan/modules/PackageLuan.java	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/modules/PackageLuan.java	Tue Oct 28 23:25:13 2014 +0000
@@ -27,7 +27,7 @@
 				module.put("require",requireFn);
 				add( module, "load", LuanState.class, String.class );
 				add( module, "load_lib", LuanState.class, String.class );
-				add( module, "search_path", String.class, String.class );
+				add( module, "search_path", LuanState.class, String.class, String.class );
 				add( module, "search", LuanState.class, String.class );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -116,10 +116,10 @@
 		return null;
 	}
 
-	public static String search_path(String name,String path) {
+	public static String search_path(LuanState luan,String name,String path) throws LuanException {
 		for( String s : path.split(";") ) {
 			String file = s.replaceAll("\\?",name);
-			if( Utils.exists(file) )
+			if( file.indexOf(':') > 0 && IoLuan.get(luan,file) != null )
 				return file;
 		}
 		return null;
@@ -134,12 +134,12 @@
 	};
 
 	public static final LuanFunction fileSearcher = new LuanFunction() {
-		@Override public Object[] call(LuanState luan,Object[] args) {
+		@Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
 			String modName = (String)args[0];
 			String path = (String)pkg(luan,"path");
 			if( path==null )
 				return LuanFunction.NOTHING;
-			String file = search_path(modName,path);
+			String file = search_path(luan,modName,path);
 			return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file};
 		}
 	};
--- a/core/src/luan/modules/Utils.java	Tue Oct 28 21:20:48 2014 +0000
+++ b/core/src/luan/modules/Utils.java	Tue Oct 28 23:25:13 2014 +0000
@@ -73,14 +73,15 @@
 		}
 	}
 */
-	public static File toFile(String path) {
+/*
+	private static File toFile(String path) {
 		if( path.contains("//") )
 			return null;
 		File file = new File(path);
 		return file.exists() ? file : null;
 	}
 
-	public static URL toUrl(String path) {
+	private static URL toUrl(String path) {
 		if( path.indexOf(':') == -1 )
 			return null;
 		if( path.startsWith("classpath:") ) {
@@ -115,7 +116,8 @@
 		return null;
 	}
 
-	public static boolean exists(String path) {
+	static boolean exists(String path) {
 		return toFile(path)!=null || toUrl(path)!=null;
 	}
+*/
 }
Binary file dist/jars/luan-core-trunk.jar has changed
Binary file dist/jars/luan-logging-trunk.jar has changed
Binary file dist/jars/luan-lucene-trunk.jar has changed
Binary file dist/jars/luan-mail-trunk.jar has changed
Binary file dist/jars/luan-web-trunk.jar has changed
--- a/web/src/luan/modules/web/Web_server.luan	Tue Oct 28 21:20:48 2014 +0000
+++ b/web/src/luan/modules/web/Web_server.luan	Tue Oct 28 23:25:13 2014 +0000
@@ -79,7 +79,7 @@
 	if base.match("^classpath:") ~= nil then
 		base = dir.."#"..welcome_file.."#"..welcome_file..".luan"
 	end
-	resource_handler.setResourceBase(Io.File(base).to_string())
+	resource_handler.setResourceBase(Io.get(base).to_string())
 	resource_handler.setWelcomeFiles {welcome_file}
 	luan_handler.setWelcomeFile(welcome_file)
 	local server = Server.new(port)