<commit_before>package org.basex.query.util.pkg;

import static org.basex.query.QueryText.*;
import static org.basex.query.util.Err.*;
import static org.basex.util.Token.*;

import java.lang.reflect.*;
import java.net.*;
import java.util.*;

import org.basex.core.*;
import org.basex.io.*;
import org.basex.query.*;
import org.basex.query.util.pkg.Package.Component;
import org.basex.query.util.pkg.Package.Dependency;
import org.basex.util.*;
import org.basex.util.hash.*;

/**
 * Module loader.
 *
 * @author BaseX Team 2005-12, BSD License
 * @author <NAME>
 */
public final class ModuleLoader {
  /** Default class loader. */
  private static final ClassLoader LOADER =
      Thread.currentThread().getContextClassLoader();
  /** Cached URLs to be added to the class loader. */
  private final ArrayList<URL> urls = new ArrayList<URL>();
  /** Current class loader. */
  private ClassLoader loader = LOADER;
  /** Java modules. */
  private HashMap<Object, ArrayList<Method>> javaModules;
  /** Database context. */
  private final Context context;

  /**
   * Constructor.
   * @param ctx database context
   */
  public ModuleLoader(final Context ctx) {
    context = ctx;
  }

  /**
   * Closes opened jar files.
   */
  public void close() {
    if(loader instanceof JarLoader) ((JarLoader) loader).close();
  }

  /**
   * Adds a package from the repository or a Java class.
   * @param uri module uri
   * @param ii input info
   * @param qp query parser
   * @return if the package has been found
   * @throws QueryException query exception
   */
  public boolean addImport(final byte[] uri, final InputInfo ii, final QueryParser qp)
      throws QueryException {

    // add EXPath package
    final TokenSet pkgs = context.repo.nsDict().get(uri);
    if(pkgs != null) {
      Version ver = null;
      byte[] nm = null;
      for(final byte[] name : pkgs) {
        final Version v = new Version(Package.version(name));
        if(ver == null || v.compareTo(ver) > 0) {
          ver = v;
          nm = name;
        }
      }
      if(nm != null) {
        addRepo(nm, new TokenSet(), new TokenSet(), ii, qp);
        return true;
      }
    }

    // search module in repository: rewrite URI to file path
    final boolean java = startsWith(uri, JAVAPREF);
    String uriPath = uri2path(string(java ? substring(uri, JAVAPREF.length) : uri));
    if(uriPath == null) return false;

    final boolean suffix = !IO.suffix(uriPath).isEmpty();
    if(!java) {
      // no "java:" prefix: first try to import module as XQuery
      final String path = context.mprop.get(MainProp.REPOPATH) + uriPath;
      if(suffix) {
        // check if file has XQuery suffix
        final IOFile f = new IOFile(path);
        if(f.hasSuffix(IO.XQSUFFIXES)) return addModule(f, uri, qp);
      } else {
        // check for any file with XQuery suffix
        for(final String suf : IO.XQSUFFIXES) {
          if(addModule(new IOFile(path + suf), uri, qp)) return true;
        }
      }
    }

    // "java:" prefix, or no XQuery package found: try to load Java module
    uriPath = capitalize(uriPath);
    final String path = context.mprop.get(MainProp.REPOPATH) + uriPath;
    final IOFile file = new IOFile(path + (suffix ? "" : IO.JARSUFFIX));
    if(file.exists()) addURL(file);

    // try to create Java class instance
    addJava(uriPath, uri, ii);
    return true;
  }

  /**
   * Returns a reference to the specified class.
   * @param clz fully classified class name
   * @return found class, or {@code null}
   * @throws Throwable any exception or error: {@link ClassNotFoundException},
   *   {@link LinkageError} or {@link ExceptionInInitializerError}.<commit_msg>[FIX] XQuery, Module Loader: obsolete suffix check removed. Closes #633.<commit_after>package org.basex.query.util.pkg;

import static org.basex.query.QueryText.*;
import static org.basex.query.util.Err.*;
import static org.basex.util.Token.*;

import java.lang.reflect.*;
import java.net.*;
import java.util.*;

import org.basex.core.*;
import org.basex.io.*;
import org.basex.query.*;
import org.basex.query.util.pkg.Package.Component;
import org.basex.query.util.pkg.Package.Dependency;
import org.basex.util.*;
import org.basex.util.hash.*;

/**
 * Module loader.
 *
 * @author BaseX Team 2005-12, BSD License
 * @author <NAME>
 */
public final class ModuleLoader {
  /** Default class loader. */
  private static final ClassLoader LOADER =
      Thread.currentThread().getContextClassLoader();
  /** Cached URLs to be added to the class loader. */
  private final ArrayList<URL> urls = new ArrayList<URL>();
  /** Current class loader. */
  private ClassLoader loader = LOADER;
  /** Java modules. */
  private HashMap<Object, ArrayList<Method>> javaModules;
  /** Database context. */
  private final Context context;

  /**
   * Constructor.
   * @param ctx database context
   */
  public ModuleLoader(final Context ctx) {
    context = ctx;
  }

  /**
   * Closes opened jar files.
   */
  public void close() {
    if(loader instanceof JarLoader) ((JarLoader) loader).close();
  }

  /**
   * Adds a package from the repository or a Java class.
   * @param uri module uri
   * @param ii input info
   * @param qp query parser
   * @return if the package has been found
   * @throws QueryException query exception
   */
  public boolean addImport(final byte[] uri, final InputInfo ii, final QueryParser qp)
      throws QueryException {

    // add EXPath package
    final TokenSet pkgs = context.repo.nsDict().get(uri);
    if(pkgs != null) {
      Version ver = null;
      byte[] nm = null;
      for(final byte[] name : pkgs) {
        final Version v = new Version(Package.version(name));
        if(ver == null || v.compareTo(ver) > 0) {
          ver = v;
          nm = name;
        }
      }
      if(nm != null) {
        addRepo(nm, new TokenSet(), new TokenSet(), ii, qp);
        return true;
      }
    }

    // search module in repository: rewrite URI to file path
    final boolean java = startsWith(uri, JAVAPREF);
    String uriPath = uri2path(string(java ? substring(uri, JAVAPREF.length) : uri));
    if(uriPath == null) return false;

    if(!java) {
      // no "java:" prefix: first try to import module as XQuery
      final String path = context.mprop.get(MainProp.REPOPATH) + uriPath;
      // check for any file with XQuery suffix
      for(final String suf : IO.XQSUFFIXES) {
        if(addModule(new IOFile(path + suf), uri, qp)) return true;
      }
    }

    // "java:" prefix, or no XQuery package found: try to load Java module
    uriPath = capitalize(uriPath);
    final String path = context.mprop.get(MainProp.REPOPATH) + uriPath;
    final IOFile file = new IOFile(path + IO.JARSUFFIX);
    if(file.exists()) addURL(file);

    // try to create Java class instance
    addJava(uriPath, uri, ii);
    return true;
  }

  /**
   * Returns a reference to the specified class.
   * @param clz fully classified class name
   * @return found class, or {@code null}
   * @throws Throwable any exception or error: {@link ClassNotFoundException},
   *   {@link LinkageError} or {@link ExceptionInInitializerError}.
{'fraction_non_alphanumeric': 0.12265208858985141, 'fraction_numerical': 0.0026633024950939166, 'mean_word_length': 2.8714053174172545, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1788366', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2158, 'n_words': 687}