jaroslav@123
|
1 |
/**
|
jaroslav@123
|
2 |
* HTML via Java(tm) Language Bindings
|
jaroslav@123
|
3 |
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@123
|
4 |
*
|
jaroslav@123
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@123
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@123
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@123
|
8 |
*
|
jaroslav@123
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@123
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@123
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@123
|
12 |
* GNU General Public License for more details. apidesign.org
|
jaroslav@123
|
13 |
* designates this particular file as subject to the
|
jaroslav@123
|
14 |
* "Classpath" exception as provided by apidesign.org
|
jaroslav@123
|
15 |
* in the License file that accompanied this code.
|
jaroslav@123
|
16 |
*
|
jaroslav@123
|
17 |
* You should have received a copy of the GNU General Public License
|
jaroslav@123
|
18 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@123
|
19 |
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
|
jaroslav@123
|
20 |
*/
|
jaroslav@123
|
21 |
package org.apidesign.html.boot.impl;
|
jaroslav@123
|
22 |
|
jaroslav@163
|
23 |
import java.io.InputStream;
|
jaroslav@163
|
24 |
import java.io.InputStreamReader;
|
jaroslav@163
|
25 |
import java.io.Reader;
|
jaroslav@123
|
26 |
import java.net.URL;
|
jaroslav@123
|
27 |
import java.util.ArrayList;
|
jaroslav@123
|
28 |
import java.util.Collections;
|
jaroslav@123
|
29 |
import java.util.Enumeration;
|
jaroslav@123
|
30 |
import java.util.List;
|
jaroslav@123
|
31 |
import org.apidesign.html.boot.spi.Fn;
|
jaroslav@123
|
32 |
|
jaroslav@123
|
33 |
/**
|
jaroslav@123
|
34 |
*
|
jaroslav@123
|
35 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@123
|
36 |
*/
|
jaroslav@123
|
37 |
public final class FnUtils {
|
jaroslav@123
|
38 |
private FnUtils() {
|
jaroslav@123
|
39 |
}
|
jaroslav@123
|
40 |
|
jaroslav@123
|
41 |
public static Fn define(Class<?> caller, String code, String... names) {
|
jaroslav@123
|
42 |
JsClassLoader cl = (JsClassLoader)caller.getClassLoader();
|
jaroslav@123
|
43 |
return cl.defineFn(code, names);
|
jaroslav@123
|
44 |
}
|
jaroslav@123
|
45 |
|
jaroslav@128
|
46 |
public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
|
jaroslav@123
|
47 |
return new JsClassLoader(parent) {
|
jaroslav@123
|
48 |
@Override
|
jaroslav@123
|
49 |
protected URL findResource(String name) {
|
jaroslav@123
|
50 |
List<URL> l = res(name, true);
|
jaroslav@123
|
51 |
return l.isEmpty() ? null : l.get(0);
|
jaroslav@123
|
52 |
}
|
jaroslav@123
|
53 |
|
jaroslav@123
|
54 |
@Override
|
jaroslav@123
|
55 |
protected Enumeration<URL> findResources(String name) {
|
jaroslav@123
|
56 |
return Collections.enumeration(res(name, false));
|
jaroslav@123
|
57 |
}
|
jaroslav@123
|
58 |
|
jaroslav@123
|
59 |
private List<URL> res(String name, boolean oneIsEnough) {
|
jaroslav@123
|
60 |
List<URL> l = new ArrayList<URL>();
|
jaroslav@123
|
61 |
f.findResources(name, l, oneIsEnough);
|
jaroslav@123
|
62 |
return l;
|
jaroslav@123
|
63 |
}
|
jaroslav@123
|
64 |
|
jaroslav@123
|
65 |
@Override
|
jaroslav@123
|
66 |
protected Fn defineFn(String code, String... names) {
|
jaroslav@123
|
67 |
return d.defineFn(code, names);
|
jaroslav@123
|
68 |
}
|
jaroslav@163
|
69 |
|
jaroslav@163
|
70 |
@Override
|
jaroslav@163
|
71 |
protected void loadScript(Reader code) throws Exception {
|
jaroslav@163
|
72 |
d.loadScript(code);
|
jaroslav@163
|
73 |
}
|
jaroslav@123
|
74 |
};
|
jaroslav@123
|
75 |
}
|
jaroslav@160
|
76 |
|
jaroslav@189
|
77 |
static String callback(final String body) {
|
jaroslav@184
|
78 |
return new JsCallback() {
|
jaroslav@184
|
79 |
@Override
|
jaroslav@184
|
80 |
protected CharSequence callMethod(
|
jaroslav@184
|
81 |
String ident, String fqn, String method, String params
|
jaroslav@184
|
82 |
) {
|
jaroslav@188
|
83 |
StringBuilder sb = new StringBuilder();
|
jaroslav@188
|
84 |
sb.append("vm.").append(mangle(fqn, method, params));
|
jaroslav@191
|
85 |
sb.append("(");
|
jaroslav@191
|
86 |
if (ident != null) {
|
jaroslav@191
|
87 |
sb.append(ident);
|
jaroslav@191
|
88 |
}
|
jaroslav@188
|
89 |
return sb;
|
jaroslav@184
|
90 |
}
|
jaroslav@184
|
91 |
|
jaroslav@184
|
92 |
}.parse(body);
|
jaroslav@160
|
93 |
}
|
jaroslav@163
|
94 |
|
jaroslav@163
|
95 |
static void loadScript(JsClassLoader jcl, String resource) {
|
jaroslav@163
|
96 |
final InputStream script = jcl.getResourceAsStream(resource);
|
jaroslav@163
|
97 |
if (script == null) {
|
jaroslav@163
|
98 |
throw new NullPointerException("Can't find " + resource);
|
jaroslav@163
|
99 |
}
|
jaroslav@163
|
100 |
try {
|
jaroslav@163
|
101 |
Reader isr = null;
|
jaroslav@163
|
102 |
try {
|
jaroslav@163
|
103 |
isr = new InputStreamReader(script, "UTF-8");
|
jaroslav@163
|
104 |
jcl.loadScript(isr);
|
jaroslav@163
|
105 |
} finally {
|
jaroslav@163
|
106 |
if (isr != null) {
|
jaroslav@163
|
107 |
isr.close();
|
jaroslav@163
|
108 |
}
|
jaroslav@163
|
109 |
}
|
jaroslav@163
|
110 |
} catch (Exception ex) {
|
jaroslav@163
|
111 |
throw new IllegalStateException("Can't execute " + resource, ex);
|
jaroslav@163
|
112 |
}
|
jaroslav@163
|
113 |
}
|
jaroslav@123
|
114 |
}
|