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