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 String module;
31 private final ClassLoader loader;
33 LdrRsrcs(ClassLoader loader) {
37 LdrRsrcs(String module, ClassLoader loader) {
43 public InputStream get(String name) throws IOException {
44 final URL url = findSource(name);
49 final String resourceModule = getModule(url);
50 if ((resourceModule != null) && !module.equals(resourceModule)) {
55 return url.openStream();
58 private URL findSource(String name) throws IOException {
59 Enumeration<URL> en = loader.getResources(name);
61 while (en.hasMoreElements()) {
67 private static String getModule(URL url) throws IOException {
68 if (!"jar".equalsIgnoreCase(url.getProtocol())) {
72 final String fullPathString = url.getPath();
73 final int sepIndex = fullPathString.indexOf('!');
74 final String jarPathString =
75 (sepIndex != -1) ? fullPathString.substring(0, sepIndex)
77 if (!jarPathString.endsWith(".jar")) {
82 jarPathString.substring(
83 jarPathString.lastIndexOf('/') + 1,
84 jarPathString.length() - 4);
85 if (moduleName.endsWith("-SNAPSHOT")) {
86 moduleName = moduleName.substring(
87 0, moduleName.length() - 9);
89 final int dashIndex = moduleName.lastIndexOf('-');
90 if (dashIndex != -1) {
91 moduleName = moduleName.substring(0, dashIndex);