rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 22 May 2014 19:06:44 +0200
branchclosure
changeset 1587 bf08bd96d408
parent 1558 0c5a8b83182a
child 1707 61dd2c555a1f
permissions -rw-r--r--
Don't include @JavaScriptResource resources in generated JavaScript
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 import net.java.html.js.JavaScriptBody;
    25 import net.java.html.js.JavaScriptResource;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 @JavaScriptResource("obj.js")
    32 public class Resources {
    33     @JavaScriptBody(args = {}, body = "return obj;")
    34     static Object retObj() {
    35         return null;
    36     }
    37     
    38     public static boolean isObj() {
    39         return retObj() != null;
    40     }
    41     public static boolean isResource() {
    42         return Resources.class.getResource("obj.js") != null;
    43     }
    44     
    45     public static String loadKO() throws IOException {
    46         InputStream is = Resources.class.getResourceAsStream("ko.js");
    47         return readIS(is, false);
    48     }
    49     
    50     static String loadClazz() throws IOException {
    51         InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
    52         return readIS(is, false);
    53     }
    54 
    55     private static String readIS(InputStream is, boolean asString) throws IOException {
    56         if (is == null) {
    57             return "No resource found!";
    58         }
    59         byte[] arr = new byte[4092];
    60         int len = is.read(arr);
    61         if (len < 5) {
    62             return "No data read! Len: " + len;
    63         }
    64         
    65         if (asString) {
    66             return new String(arr, 0, len, "UTF-8").toString().toString();
    67         }
    68         
    69         StringBuilder sb = new StringBuilder();
    70         sb.append("[");
    71         for (int i = 0; i < len; i++) {
    72             sb.append(arr[i]).append(", ");
    73         }
    74         
    75         return sb.toString().toString();
    76     }
    77     static long bytesToLong(byte b1, byte b2, int shift) {
    78         return (((long)b1 << 56) +
    79                 ((long)b2 & 255) << 48) >> shift;
    80     }
    81 
    82     public static String loadHello() throws IOException {
    83         Enumeration<URL> en;
    84         try {
    85             en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    86         } catch (SecurityException ex) {
    87             return "SecurityException";
    88         }
    89         StringBuilder sb = new StringBuilder();
    90         while (en.hasMoreElements()) {
    91             URL url = en.nextElement();
    92             sb.append(readIS(url.openStream(), true));
    93         }
    94         return sb.toString().toString();
    95     }
    96     public static String loadJustHello() throws IOException {
    97         URL url = Resources.class.getResource("/META-INF/ahoj");
    98         StringBuilder sb = new StringBuilder();
    99         sb.append(readIS(url.openStream(), true));
   100         return sb.toString().toString();
   101     }
   102 }