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.IOException;
21 import java.io.InputStream;
23 import java.util.Enumeration;
25 /** Implementation of Resources that delegates to some class loader.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 final class LdrRsrcs implements Bck2Brwsr.Resources {
30 private final ClassLoader loader;
32 LdrRsrcs(ClassLoader loader) {
37 public InputStream get(String name) throws IOException {
38 return findSource(name).openStream();
42 public String getModule(String name) throws IOException {
43 final URL url = findSource(name);
45 if (!"jar".equalsIgnoreCase(url.getProtocol())) {
49 final String fullPathString = url.getPath();
50 final int sepIndex = fullPathString.indexOf('!');
51 final String jarPathString =
52 (sepIndex != -1) ? fullPathString.substring(0, sepIndex)
54 if (!jarPathString.endsWith(".jar")) {
59 jarPathString.substring(
60 jarPathString.lastIndexOf('/') + 1,
61 jarPathString.length() - 4);
62 if (moduleName.endsWith("-SNAPSHOT")) {
63 moduleName = moduleName.substring(
64 0, moduleName.length() - 9);
66 final int dashIndex = moduleName.lastIndexOf('-');
67 if (dashIndex != -1) {
68 moduleName = moduleName.substring(0, dashIndex);
74 private URL findSource(String name) throws IOException {
75 Enumeration<URL> en = loader.getResources(name);
77 while (en.hasMoreElements()) {
81 throw new IOException("Can't find " + name);