<commit_before>/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.swarm.bootstrap.modules;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.jboss.modules.ResourceLoader;
import org.jboss.modules.ResourceLoaders;
import org.wildfly.swarm.bootstrap.util.TempFileManager;

/**
 * @author <NAME>
 */
public class NestedJarResourceLoader {

    private NestedJarResourceLoader() {
    }

    public static ResourceLoader loaderFor(URL base, String rootPath, String loaderPath, String loaderName) throws IOException {

        String urlString = base.toExternalForm();
        if (urlString.startsWith("jar:file:")) {
            int endLoc = urlString.indexOf(".jar!");
            if (endLoc > 0) {
                String jarPath = urlString.substring(9, endLoc + 4);

                File exp = exploded.get(jarPath);

                if (exp == null) {
                    exp = TempFileManager.INSTANCE.newTempDirectory("module-jar", ".jar_d");

                    JarFile jarFile = new JarFile(jarPath);

                    Enumeration<JarEntry> entries = jarFile.entries();

                    while (entries.hasMoreElements()) {
                        JarEntry each = entries.nextElement();

                        if (!each.isDirectory()) {
                            File out = new File(exp, each.getName());
                            out.getParentFile().mkdirs();
                            Files.copy(jarFile.getInputStream(each), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        }
                    }
                }

                String relativeRoot = urlString.substring(endLoc + 5);
                File resourceRoot = new File(new File(exp, relativeRoot), loaderPath);
                if (!resourceRoot.isDirectory() && (resourceRoot.getName().endsWith(".jar") || resourceRoot.getName().endsWith(".war"))) {
                    JarFile jar = new JarFile(resourceRoot);
                    return ResourceLoaders.createJarResourceLoader(loaderName, jar);
                } else {
                    return ResourceLoaders.createFileResourceLoader(loaderName, resourceRoot);
                }
            }
        } else if (urlString.startsWith("file:")) {
            return ResourceLoaders.createFileResourceLoader(
                    loaderPath,
                    new File(urlString.substring(5, urlString.length()))
            );
        }

        throw new IllegalArgumentException("Illegal module loader base: " + base);
    }<commit_msg>SWARM-966: Resource root in module.xml not found running in IDE (#303)<commit_after>/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wildfly.swarm.bootstrap.modules;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.jboss.modules.ResourceLoader;
import org.jboss.modules.ResourceLoaders;
import org.wildfly.swarm.bootstrap.util.TempFileManager;

/**
 * @author <NAME>
 */
public class NestedJarResourceLoader {

    private NestedJarResourceLoader() {
    }

    public static ResourceLoader loaderFor(URL base, String rootPath, String loaderPath, String loaderName) throws IOException {

        String urlString = base.toExternalForm();
        if (urlString.startsWith("jar:file:")) {
            int endLoc = urlString.indexOf(".jar!");
            if (endLoc > 0) {
                String jarPath = urlString.substring(9, endLoc + 4);

                File exp = exploded.get(jarPath);

                if (exp == null) {
                    exp = TempFileManager.INSTANCE.newTempDirectory("module-jar", ".jar_d");

                    JarFile jarFile = new JarFile(jarPath);

                    Enumeration<JarEntry> entries = jarFile.entries();

                    while (entries.hasMoreElements()) {
                        JarEntry each = entries.nextElement();

                        if (!each.isDirectory()) {
                            File out = new File(exp, each.getName());
                            out.getParentFile().mkdirs();
                            Files.copy(jarFile.getInputStream(each), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        }
                    }
                }

                String relativeRoot = urlString.substring(endLoc + 5);
                File resourceRoot = new File(new File(exp, relativeRoot), loaderPath);
                if (!resourceRoot.isDirectory() && (resourceRoot.getName().endsWith(".jar") || resourceRoot.getName().endsWith(".war"))) {
                    JarFile jar = new JarFile(resourceRoot);
                    return ResourceLoaders.createJarResourceLoader(loaderName, jar);
                } else {
                    return ResourceLoaders.createFileResourceLoader(loaderName, resourceRoot);
                }
            }
        } else if (urlString.startsWith("file:")) {
            if (loaderName.endsWith(".jar") || loaderName.endsWith(".war")) {
                return ResourceLoaders.createJarResourceLoader(
                        loaderName,
                        new JarFile(new File(new File(urlString.substring(5, urlString.length())), loaderPath)));
            }
            return ResourceLoaders.createFileResourceLoader(
                    loaderPath,
                    new File(urlString.substring(5, urlString.length()))
            );
        }

        throw new IllegalArgumentException("Illegal module loader base: " + base);
    }
{'fraction_non_alphanumeric': 0.10143282735139736, 'fraction_numerical': 0.0058164278621081, 'mean_word_length': 2.25635103926097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665173', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1748, 'n_words': 472}