boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 10 Oct 2013 14:02:18 +0200
changeset 309 7025177bd67e
parent 288 8c5b40231d26
child 323 86aabecda7a3
permissions -rw-r--r--
FnUtils are bloated and contain references to Asm classes. Separate the code needed by generated JsCallbacks into own class
     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 org.apidesign.html.boot.spi.Fn;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public final class FnUtils {
    38     
    39     private FnUtils() {
    40     }
    41     
    42     public static Fn define(Class<?> caller, String code, String... names) {
    43         return FnContext.currentPresenter().defineFn(code, names);
    44     }
    45     
    46     public static boolean isJavaScriptCapable(ClassLoader l) {
    47         return l instanceof JsClassLoader;
    48     }
    49     
    50     public static boolean isValid(Fn fn) {
    51         return fn != null && fn.isValid();
    52     }
    53 
    54     public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
    55         return new JsClassLoader(parent) {
    56             @Override
    57             protected URL findResource(String name) {
    58                 List<URL> l = res(name, true);
    59                 return l.isEmpty() ? null : l.get(0);
    60             }
    61             
    62             @Override
    63             protected Enumeration<URL> findResources(String name) {
    64                 return Collections.enumeration(res(name, false));
    65             }
    66             
    67             private List<URL> res(String name, boolean oneIsEnough) {
    68                 List<URL> l = new ArrayList<URL>();
    69                 f.findResources(name, l, oneIsEnough);
    70                 return l;
    71             }
    72             
    73             @Override
    74             protected Fn defineFn(String code, String... names) {
    75                 return d.defineFn(code, names);
    76             }
    77 
    78             @Override
    79             protected void loadScript(Reader code) throws Exception {
    80                 d.loadScript(code);
    81             }
    82         };
    83     }
    84 
    85     static String callback(final String body) {
    86         return new JsCallback() {
    87             @Override
    88             protected CharSequence callMethod(
    89                 String ident, String fqn, String method, String params
    90             ) {
    91                 StringBuilder sb = new StringBuilder();
    92                 sb.append("vm.").append(mangle(fqn, method, params));
    93                 sb.append("(");
    94                 if (ident != null) {
    95                     sb.append(ident);
    96                 }
    97                 return sb;
    98             }
    99 
   100         }.parse(body);
   101     }
   102 
   103     static void loadScript(JsClassLoader jcl, String resource) {
   104         final InputStream script = jcl.getResourceAsStream(resource);
   105         if (script == null) {
   106             throw new NullPointerException("Can't find " + resource);
   107         }
   108         try {
   109             Reader isr = null;
   110             try {
   111                 isr = new InputStreamReader(script, "UTF-8");
   112                 jcl.loadScript(isr);
   113             } finally {
   114                 if (isr != null) {
   115                     isr.close();
   116                 }
   117             }
   118         } catch (Exception ex) {
   119             throw new IllegalStateException("Can't execute " + resource, ex);
   120         } 
   121     }
   122 }