Mercurial Hosting > luan
comparison src/luan/lib/PackageLib.java @ 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 |
comparison
equal
deleted
inserted
replaced
142:05f8c21160ef | 143:fcb81fa2df0d |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.io.File; | |
4 import java.io.IOException; | 3 import java.io.IOException; |
5 import java.net.URL; | |
6 import java.util.Arrays; | 4 import java.util.Arrays; |
7 import java.util.Collections; | 5 import java.util.Collections; |
8 import java.util.List; | 6 import java.util.List; |
9 import luan.Luan; | 7 import luan.Luan; |
10 import luan.LuanState; | 8 import luan.LuanState; |
32 throw new RuntimeException(e); | 30 throw new RuntimeException(e); |
33 } | 31 } |
34 LuanTable searchers = luan.searchers(); | 32 LuanTable searchers = luan.searchers(); |
35 searchers.add(preloadSearcher); | 33 searchers.add(preloadSearcher); |
36 searchers.add(fileSearcher); | 34 searchers.add(fileSearcher); |
37 searchers.put("java",javaFileSearcher); | |
38 module.put("searchers",searchers); | 35 module.put("searchers",searchers); |
39 return module; | 36 return module; |
40 } | 37 } |
41 }; | 38 }; |
42 | 39 |
52 String searchFor = modName; | 49 String searchFor = modName; |
53 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); | 50 LuanTable searchers = (LuanTable)luan.get("Package.searchers"); |
54 if( searchers == null ) { | 51 if( searchers == null ) { |
55 list = Collections.<Object>singletonList(preloadSearcher); | 52 list = Collections.<Object>singletonList(preloadSearcher); |
56 } else { | 53 } else { |
57 int i = modName.indexOf(':'); | 54 list = searchers.asList(); |
58 if( i != -1 ) { | |
59 String prefix = modName.substring(0,i); | |
60 Object searcher = searchers.get(prefix); | |
61 if( searcher != null ) { | |
62 list = Collections.<Object>singletonList(searcher); | |
63 searchFor = modName.substring(i+1); | |
64 } | |
65 } | |
66 if( list == null ) | |
67 list = searchers.asList(); | |
68 } | 55 } |
69 for( Object s : list ) { | 56 for( Object s : list ) { |
70 LuanFunction searcher = (LuanFunction)s; | 57 LuanFunction searcher = (LuanFunction)s; |
71 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{searchFor})); | 58 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{searchFor})); |
72 if( a.length >= 1 && a[0] instanceof LuanFunction ) { | 59 if( a.length >= 1 && a[0] instanceof LuanFunction ) { |
90 } | 77 } |
91 | 78 |
92 public static String search_path(String name,String path) { | 79 public static String search_path(String name,String path) { |
93 for( String s : path.split(";") ) { | 80 for( String s : path.split(";") ) { |
94 String file = s.replaceAll("\\?",name); | 81 String file = s.replaceAll("\\?",name); |
95 if( new File(file).exists() ) | 82 if( Utils.exists(file) ) |
96 return file; | 83 return file; |
97 } | 84 } |
98 return null; | 85 return null; |
99 } | 86 } |
100 | 87 |
124 } | 111 } |
125 }; | 112 }; |
126 | 113 |
127 | 114 |
128 | 115 |
129 | |
130 public static final LuanFunction javaFileLoader = new LuanFunction() { | |
131 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | |
132 String urlStr = (String)args[1]; | |
133 String path = (String)args[2]; | |
134 try { | |
135 String src = new IoLib.LuanUrl(urlStr).read_text(); | |
136 LuanFunction fn = BasicLib.load(luan,src,path,false,false); | |
137 return fn.call(luan,args); | |
138 } catch(IOException e) { | |
139 throw luan.exception(e); | |
140 } | |
141 } | |
142 }; | |
143 | |
144 public static final LuanFunction javaFileSearcher = new LuanFunction() { | |
145 @Override public Object[] call(LuanState luan,Object[] args) { | |
146 String path = (String)args[0]; | |
147 String url = IoLib.java_resource_to_url(path); | |
148 if( url != null ) { | |
149 return new Object[]{javaFileLoader,url,path}; | |
150 } | |
151 return LuanFunction.NOTHING; | |
152 } | |
153 }; | |
154 | |
155 | |
156 public static LuanFunction get_loader(String path) | 116 public static LuanFunction get_loader(String path) |
157 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException | 117 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException |
158 { | 118 { |
159 int i = path.lastIndexOf('.'); | 119 int i = path.lastIndexOf('.'); |
160 String clsPath = path.substring(0,i); | 120 String clsPath = path.substring(0,i); |