comparison core/src/luan/modules/IoLuan.java @ 299:a74559240b4f

simplify PackageLuan and remove IO loading param git-svn-id: https://luan-java.googlecode.com/svn/trunk@300 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 06:24:49 +0000
parents 899253043270
children a6bf8ff720f8
comparison
equal deleted inserted replaced
298:2ce23c7e2342 299:a74559240b4f
186 public LuanFunction read_blocks(Integer blockSize) throws IOException { 186 public LuanFunction read_blocks(Integer blockSize) throws IOException {
187 int n = blockSize!=null ? blockSize : Utils.bufSize; 187 int n = blockSize!=null ? blockSize : Utils.bufSize;
188 return blocks(inputStream(),n); 188 return blocks(inputStream(),n);
189 } 189 }
190 190
191 public LuanFunction loader(LuanState luan,String name,LuanTable env) throws LuanException {
192 try {
193 String src = read_text();
194 return BasicLuan.load(luan,src,name,env,false);
195 } catch(FileNotFoundException e) {
196 return null;
197 } catch(IOException e) {
198 throw luan.exception(e);
199 }
200 }
201
202 public boolean exists() throws IOException { 191 public boolean exists() throws IOException {
203 try { 192 try {
204 inputStream().close(); 193 inputStream().close();
205 return true; 194 return true;
206 } catch(FileNotFoundException e) { 195 } catch(FileNotFoundException e) {
223 tbl.put( "read_lines", new LuanJavaFunction( 212 tbl.put( "read_lines", new LuanJavaFunction(
224 LuanIn.class.getMethod( "read_lines" ), this 213 LuanIn.class.getMethod( "read_lines" ), this
225 ) ); 214 ) );
226 tbl.put( "read_blocks", new LuanJavaFunction( 215 tbl.put( "read_blocks", new LuanJavaFunction(
227 LuanIn.class.getMethod( "read_blocks", Integer.class ), this 216 LuanIn.class.getMethod( "read_blocks", Integer.class ), this
228 ) );
229 tbl.put( "loader", new LuanJavaFunction(
230 LuanIn.class.getMethod( "loader", LuanState.class, String.class, LuanTable.class ), this
231 ) ); 217 ) );
232 tbl.put( "exists", new LuanJavaFunction( 218 tbl.put( "exists", new LuanJavaFunction(
233 LuanIn.class.getMethod( "exists" ), this 219 LuanIn.class.getMethod( "exists" ), this
234 ) ); 220 ) );
235 } catch(NoSuchMethodException e) { 221 } catch(NoSuchMethodException e) {
427 } 413 }
428 return tbl; 414 return tbl;
429 } 415 }
430 } 416 }
431 417
432 public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException { 418 public static LuanTable file(LuanState luan,String name) throws LuanException {
433 if( Boolean.TRUE.equals(loading) )
434 name += ".luan";
435 File file = new File(name); 419 File file = new File(name);
436 return new LuanFile(file).table(); 420 return new LuanFile(file).table();
437 } 421 }
438 422
439 public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException { 423 public static LuanTable classpath(LuanState luan,String name) throws LuanException {
440 if( name.contains("//") ) 424 if( name.contains("//") )
441 return null; 425 return null;
442 String path = name; 426 String path = name;
443 boolean isLoading = Boolean.TRUE.equals(loading);
444 if( isLoading )
445 path += ".luan";
446 check(luan,"classpath",path); 427 check(luan,"classpath",path);
447 URL url; 428 URL url;
448 if( !path.contains("#") ) { 429 if( !path.contains("#") ) {
449 url = ClassLoader.getSystemResource(path); 430 url = ClassLoader.getSystemResource(path);
450 } else { 431 } else {
468 return new LuanUrl(url).table(); 449 return new LuanUrl(url).table();
469 450
470 return null; 451 return null;
471 } 452 }
472 453
473 private static LuanTable url(String url,Boolean loading) throws IOException { 454 private static LuanTable url(String url) throws IOException {
474 if( Boolean.TRUE.equals(loading) )
475 url += ".luan";
476 return new LuanUrl(new URL(url)).table(); 455 return new LuanUrl(new URL(url)).table();
477 } 456 }
478 457
479 public static LuanTable http(String path,Boolean loading) throws IOException { 458 public static LuanTable http(String path) throws IOException {
480 return url("http:"+path,loading); 459 return url("http:"+path);
481 } 460 }
482 461
483 public static LuanTable https(String path,Boolean loading) throws IOException { 462 public static LuanTable https(String path) throws IOException {
484 return url("https:"+path,loading); 463 return url("https:"+path);
485 } 464 }
486 465
487 public static LuanTable java(LuanState luan,String path,Boolean loading) throws LuanException { 466 public static LuanTable luan(LuanState luan,String path) throws LuanException {
488 if( !Boolean.TRUE.equals(loading) ) 467 return classpath( luan, "luan/modules/" + path );
489 return null;
490 final LuanFunction fn = JavaLuan.javaLoader(luan,path);
491 if( fn==null )
492 return null;
493 LuanFunction loader = new LuanFunction() {
494 @Override public Object call(LuanState luan,Object[] args) {
495 return fn;
496 }
497 };
498 LuanTable tbl = Luan.newTable();
499 tbl.put( "loader", loader );
500 return tbl;
501 }
502
503 public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException {
504 return classpath( luan, "luan/modules/" + path, loading );
505 } 468 }
506 469
507 public static LuanTable stdin(LuanState luan) throws LuanException { 470 public static LuanTable stdin(LuanState luan) throws LuanException {
508 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); 471 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io");
509 return (LuanTable)io.get("stdin"); 472 return (LuanTable)io.get("stdin");
510 } 473 }
511 474
512 public static LuanTable newSchemes() { 475 public static LuanTable newSchemes() {
513 LuanTable schemes = Luan.newTable(); 476 LuanTable schemes = Luan.newTable();
514 try { 477 try {
515 add( schemes, "file", LuanState.class, String.class, Boolean.class ); 478 add( schemes, "file", LuanState.class, String.class );
516 add( schemes, "classpath", LuanState.class, String.class, Boolean.class ); 479 add( schemes, "classpath", LuanState.class, String.class );
517 add( schemes, "socket", LuanState.class, String.class ); 480 add( schemes, "socket", LuanState.class, String.class );
518 add( schemes, "http", String.class, Boolean.class ); 481 add( schemes, "http", String.class );
519 add( schemes, "https", String.class, Boolean.class ); 482 add( schemes, "https", String.class );
520 add( schemes, "java", LuanState.class, String.class, Boolean.class ); 483 add( schemes, "luan", LuanState.class, String.class );
521 add( schemes, "luan", LuanState.class, String.class, Boolean.class );
522 add( schemes, "stdin", LuanState.class ); 484 add( schemes, "stdin", LuanState.class );
523 } catch(NoSuchMethodException e) { 485 } catch(NoSuchMethodException e) {
524 throw new RuntimeException(e); 486 throw new RuntimeException(e);
525 } 487 }
526 return schemes; 488 return schemes;
534 if( t == null ) 496 if( t == null )
535 return newSchemes(); 497 return newSchemes();
536 return t; 498 return t;
537 } 499 }
538 500
539 public static LuanTable Uri(LuanState luan,String name,Boolean loading) throws LuanException { 501 public static LuanTable Uri(LuanState luan,String name) throws LuanException {
540 int i = name.indexOf(':'); 502 int i = name.indexOf(':');
541 if( i == -1 ) 503 if( i == -1 )
542 throw luan.exception( "invalid Io name '"+name+"', missing scheme" ); 504 throw luan.exception( "invalid Io name '"+name+"', missing scheme" );
543 String scheme = name.substring(0,i); 505 String scheme = name.substring(0,i);
544 String location = name.substring(i+1); 506 String location = name.substring(i+1);
545 LuanTable schemes = schemes(luan); 507 LuanTable schemes = schemes(luan);
546 LuanFunction opener = (LuanFunction)schemes.get(scheme); 508 LuanFunction opener = (LuanFunction)schemes.get(scheme);
547 if( opener == null ) 509 if( opener == null )
548 throw luan.exception( "invalid scheme '"+scheme+"' in '"+name+"'" ); 510 throw luan.exception( "invalid scheme '"+scheme+"' in '"+name+"'" );
549 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading})); 511 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location}));
550 } 512 }
551 513
552 public static final class LuanSocket extends LuanIO { 514 public static final class LuanSocket extends LuanIO {
553 private final Socket socket; 515 private final Socket socket;
554 516