Splitting into two classes: one that deals with classpath, one that deals with ZIP content
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
25 /** Conversion from classpath to load function.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 final class ClassPath {
33 public static void init() {
35 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
36 private static native int length(Object arr);
37 @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
38 private static native Object at(Object arr, int index);
39 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
40 private static native Object set(Object arr, int index, Object value);
42 public static byte[] loadFromCp(Object classpath, String res, int skip)
43 throws IOException, ClassNotFoundException {
44 for (int i = 0; i < length(classpath); i++) {
45 Object c = at(classpath, i);
46 if (c instanceof String) {
48 String url = (String)c;
49 final ZipHandler z = ZipHandler.toZip(url);
50 c = set(classpath, i, z);
51 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
53 String mainClass = processClassPathAttr(man, url, classpath);
54 // if (mainClass != null) {
55 // Class.forName(mainClass);
58 } catch (IOException ex) {
59 set(classpath, i, ex);
60 log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
65 if (c instanceof ZipHandler) {
66 checkRes = ((ZipHandler)c).findRes(res);
67 if (checkRes != null && --skip < 0) {
71 checkRes = callFunction(c, res, skip);
72 if (checkRes != null) {
81 @JavaScriptBody(args = { "fn", "res", "skip" }, body =
82 "if (typeof fn === 'function') return fn(res, skip);\n"
85 private static native byte[] callFunction(Object fn, String res, int skip);
87 @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
88 private static native void log(String msg);
90 private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
91 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
92 String cp = is.toString();
95 for (int p = 0; p < cp.length();) {
96 int n = cp.indexOf(' ', p);
100 String el = cp.substring(p, n);
101 URL u = new URL(new URL(url), el);
102 classpath = addToArray(classpath, u.toString());
106 return is.getMainClass();
110 private static Object addToArray(Object arr, String value) {
111 final int last = length(arr);
112 Object ret = enlargeArray(arr, last + 1);
113 set(ret, last, value);
117 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
118 private static native Object enlargeArray(Object arr, int len);