annotate core/src/luan/modules/Utils.java @ 637:6ea90dc10375

bbcode parser now takes a quoter function
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Mar 2016 12:38:24 -0700
parents 31926755689e
children cdc70de628b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
167
4c0131c2b650 merge luan/lib into modules
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 161
diff changeset
1 package luan.modules;
75
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3 import java.io.Reader;
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4 import java.io.IOException;
115
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
5 import java.io.ByteArrayOutputStream;
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
6 import java.io.InputStream;
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
7 import java.io.OutputStream;
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
8 import java.io.File;
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
9 import java.net.URL;
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
10 import java.net.MalformedURLException;
86
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 75
diff changeset
11 import luan.LuanState;
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 75
diff changeset
12 import luan.LuanException;
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
13 import luan.LuanTable;
582
31926755689e add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
14 import luan.LuanFunction;
75
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
15
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
17 public final class Utils {
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
18 private Utils() {} // never
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
19
116
1ff1c32417eb more IoLib work and added init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 115
diff changeset
20 static final int bufSize = 8192;
115
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
21
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
22 private static void checkNotNull(LuanState luan,Object v,String expected,int pos) throws LuanException {
86
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 75
diff changeset
23 if( v == null )
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
24 throw new LuanException(luan,"bad argument #"+pos+" ("+expected+" expected, got nil)");
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
25 }
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
26
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
27 public static void checkNotNull(LuanState luan,String s,int pos) throws LuanException {
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
28 checkNotNull(luan,s,"string",pos);
86
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 75
diff changeset
29 }
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 75
diff changeset
30
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
31 public static void checkNotNull(LuanState luan,String s) throws LuanException {
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
32 checkNotNull(luan,s,1);
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
33 }
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
34
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
35 public static void checkNotNull(LuanState luan,byte[] b,int pos) throws LuanException {
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
36 checkNotNull(luan,b,"binary",pos);
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
37 }
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
38
305
5e7450ac27f2 rename String.byte() to String.unicode();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 260
diff changeset
39 public static void checkNotNull(LuanState luan,byte[] b) throws LuanException {
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
40 checkNotNull(luan,b,1);
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
41 }
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
42
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
43 public static void checkNotNull(LuanState luan,LuanTable t,int pos) throws LuanException {
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
44 checkNotNull(luan,t,"table",pos);
305
5e7450ac27f2 rename String.byte() to String.unicode();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 260
diff changeset
45 }
5e7450ac27f2 rename String.byte() to String.unicode();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 260
diff changeset
46
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
47 public static void checkNotNull(LuanState luan,LuanTable t) throws LuanException {
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
48 checkNotNull(luan,t,1);
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
49 }
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
50
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
51 public static void checkNotNull(LuanState luan,Number n,int pos) throws LuanException {
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
52 checkNotNull(luan,n,"number",pos);
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
53 }
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
54
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
55 public static void checkNotNull(LuanState luan,Number n) throws LuanException {
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
56 checkNotNull(luan,n,1);
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
57 }
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
58
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
59 public static void checkNotNull(LuanState luan,LuanFunction fn,int pos) throws LuanException {
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
60 checkNotNull(luan,fn,"function",pos);
251
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
61 }
705d14f4d8ee start web testing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 237
diff changeset
62
582
31926755689e add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
63 public static void checkNotNull(LuanState luan,LuanFunction fn) throws LuanException {
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 582
diff changeset
64 checkNotNull(luan,fn,1);
582
31926755689e add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
65 }
31926755689e add Thread.synchronized
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
66
75
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
67 public static String readAll(Reader in)
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
68 throws IOException
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
69 {
115
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
70 char[] a = new char[bufSize];
75
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
71 StringBuilder buf = new StringBuilder();
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
72 int n;
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
73 while( (n=in.read(a)) != -1 ) {
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
74 buf.append(a,0,n);
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
75 }
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
76 return buf.toString();
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
77 }
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
78
115
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
79 public static void copyAll(InputStream in,OutputStream out)
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
80 throws IOException
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
81 {
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
82 byte[] a = new byte[bufSize];
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
83 int n;
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
84 while( (n=in.read(a)) != -1 ) {
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
85 out.write(a,0,n);
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
86 }
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
87 }
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
88
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
89 public static byte[] readAll(InputStream in)
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
90 throws IOException
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
91 {
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
92 ByteArrayOutputStream out = new ByteArrayOutputStream();
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
93 copyAll(in,out);
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
94 return out.toByteArray();
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
95 }
237
97d175772fed implement mmake
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 219
diff changeset
96 /*
161
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
97 public static boolean exists(File file) {
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
98 try {
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
99 return file.exists() && file.getName().equals(file.getCanonicalFile().getName());
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
100 } catch(IOException e) {
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
101 throw new RuntimeException(e);
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
102 }
d310ebf4d6e7 add javaSearcher
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 143
diff changeset
103 }
237
97d175772fed implement mmake
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 219
diff changeset
104 */
260
f1f7d8c7e94e add Io.protocols
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 258
diff changeset
105 /*
f1f7d8c7e94e add Io.protocols
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 258
diff changeset
106 private static File toFile(String path) {
203
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
107 if( path.contains("//") )
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
108 return null;
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
109 File file = new File(path);
237
97d175772fed implement mmake
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 219
diff changeset
110 return file.exists() ? file : null;
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
111 }
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
112
260
f1f7d8c7e94e add Io.protocols
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 258
diff changeset
113 private static URL toUrl(String path) {
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
114 if( path.indexOf(':') == -1 )
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
115 return null;
258
4dfa86dbca45 rename "java:" to "classpath:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
116 if( path.startsWith("classpath:") ) {
4dfa86dbca45 rename "java:" to "classpath:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 251
diff changeset
117 path = path.substring(10);
187
1cb298d918b2 ban "//" in file and java paths
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
118 if( path.contains("//") )
1cb298d918b2 ban "//" in file and java paths
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 171
diff changeset
119 return null;
219
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
120 URL url;
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
121 if( !path.contains("#") ) {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
122 url = ClassLoader.getSystemResource(path);
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
123 } else {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
124 String[] a = path.split("#");
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
125 url = ClassLoader.getSystemResource(a[0]);
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
126 if( url==null ) {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
127 for( int i=1; i<a.length; i++ ) {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
128 url = ClassLoader.getSystemResource(a[0]+"/"+a[i]);
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
129 if( url != null ) {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
130 try {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
131 url = new URL(url,".");
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
132 } catch(MalformedURLException e) {
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
133 throw new RuntimeException(e);
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
134 }
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
135 break;
f9e3e64132e1 improve rev 217, allow "java:" paths to include sample files to check in jars
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 216
diff changeset
136 }
216
a1b142f9c5c0 make Utils.toUrl() check for "index.html" or "index.html.luan" in dirs for jars (hack)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 203
diff changeset
137 }
a1b142f9c5c0 make Utils.toUrl() check for "index.html" or "index.html.luan" in dirs for jars (hack)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 203
diff changeset
138 }
a1b142f9c5c0 make Utils.toUrl() check for "index.html" or "index.html.luan" in dirs for jars (hack)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 203
diff changeset
139 }
203
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
140 return url==null ? null : url;
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
141 }
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
142 try {
203
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
143 return new URL(path);
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
144 } catch(MalformedURLException e) {}
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
145 return null;
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
146 }
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
147
260
f1f7d8c7e94e add Io.protocols
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 258
diff changeset
148 static boolean exists(String path) {
203
99eef1d0e706 IO security
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 187
diff changeset
149 return toFile(path)!=null || toUrl(path)!=null;
143
fcb81fa2df0d handle urls and java resources as files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 130
diff changeset
150 }
260
f1f7d8c7e94e add Io.protocols
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 258
diff changeset
151 */
75
aa7538ae5fb6 can now load modules from classpath
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
152 }