Class AbstractClassPathModelFinder

java.lang.Object
opennlp.tools.models.AbstractClassPathModelFinder
All Implemented Interfaces:
ClassPathModelFinder
Direct Known Subclasses:
ClassgraphModelFinder, DirectoryModelFinder, SimpleClassPathModelFinder

@ThreadSafe public abstract class AbstractClassPathModelFinder extends Object implements ClassPathModelFinder
A base implementation of a ClassPathModelFinder for the detection of OpenNLP models in the classpath. By default, AbstractClassPathModelFinder will scan for JAR files starting with "opennlp-models-*".

This search mask can be adjusted by using the one argument constructor.

Finders that scan with getMatchingJarEntryURIs(List, String) or matchesWildcard(URL, String), such as SimpleClassPathModelFinder and DirectoryModelFinder, accept * for any run of characters and ? for one Unicode code point, and match masks against decoded names, including spaces and non-ASCII characters. Other subclasses may apply their own mask rules: ClassgraphModelFinder follows the wildcard rules of the ClassGraph library.

findModels(boolean) may be called from several threads. Concurrent first calls scan the classpath once, and every caller receives the complete, unmodifiable set of models. Subclasses must keep getContext() and getMatchingURIs(String, Object) free of shared mutable state.

See Also:
  • Field Details

  • Constructor Details

    • AbstractClassPathModelFinder

      public AbstractClassPathModelFinder()
    • AbstractClassPathModelFinder

      public AbstractClassPathModelFinder(String jarModelPrefix)
      Parameters:
      jarModelPrefix - The leafnames of the jars that should be scanned (e.g. "opennlp.jar"). May contain a wildcard glob ("opennlp-*.jar"). It must not be null.
      Throws:
      IllegalArgumentException - Thrown if jarModelPrefix is null.
  • Method Details

    • findModels

      public Set<ClassPathModelEntry> findModels(boolean reloadCache)
      Finds OpenNLP models available within an applications's classpath.

      The result is cached; concurrent first calls scan the classpath once. The returned set is unmodifiable and does not change when the cache is reloaded.

      Specified by:
      findModels in interface ClassPathModelFinder
      Parameters:
      reloadCache - true, if the internal cache should explicitly be reloaded, false otherwise.
      Returns:
      A Set of model entries. It might be empty, if none were found.
    • scanModels

      private Set<ClassPathModelEntry> scanModels()
      Scans the classpath for models and pairs each model with the model.properties of its jar.
      Returns:
      The unmodifiable set of model entries. It may be empty.
    • getContext

      protected abstract Object getContext()
      Returns:
      A context information object. May be null.
    • getMatchingURIs

      protected abstract List<URI> getMatchingURIs(String wildcardPattern, Object context)
      Retrieve matching classpath URIs for the given wildcardPattern.
      Parameters:
      wildcardPattern - The pattern to use for scanning. Must not be null.
      context - An object holding context information. It might be null.
      Returns:
      A list of matching classpath URIs.
    • jarPathsMatch

      protected boolean jarPathsMatch(URI uri1, URI uri2)
    • parseJarURI

      protected String[] parseJarURI(URI uri)
    • getJarModelPrefix

      protected String getJarModelPrefix()
    • getMatchingJarEntryURIs

      protected final List<URI> getMatchingJarEntryURIs(List<URL> candidates, String wildcardPattern)
      Returns the entries of every jar in candidates whose decoded file part matches * followed by the jar model prefix, keeping only the entries whose decoded file part matches * followed by wildcardPattern. Both masks follow the rules of matchesWildcard(URL, String). A jar that cannot be read is logged and skipped.
      Parameters:
      candidates - The URLs to check against the jar model prefix. Must not be null.
      wildcardPattern - The mask for jar entries. If null, nothing matches.
      Returns:
      The matching jar entry URIs in scan order. It may be empty.
      Throws:
      IllegalArgumentException - Thrown if candidates is null.
    • matchesWildcard

      protected final boolean matchesWildcard(URL url, String wildcard)
      Checks whether the decoded file part of url matches wildcard from start to end, where * stands for any run of characters, ? for exactly one Unicode code point, and every other character for itself. URI percent escapes are decoded once; literal plus signs remain plus signs. A url that is not a valid URI, such as one with an unescaped space, is matched on URL.getFile() as is.
      Parameters:
      url - The URL whose decoded file part is tested. Must not be null.
      wildcard - The wildcard expression. Must not be null.
      Returns:
      true if the file part matches, false otherwise.
      Throws:
      IllegalArgumentException - Thrown if url or wildcard is null.
    • toURL

      protected static URL toURL(String location) throws IOException
      Converts a location in String form to a URL.
      Parameters:
      location - The resource path and/or reference.
      Returns:
      The converted URL form.
      Throws:
      IOException - Thrown if IO errors occurred during conversion
    • getURIsFromJar

      protected List<URI> getURIsFromJar(URL fileUrl, boolean isWindows) throws IOException
      Lists the jar: URIs of all non-directory entries in the jar file at fileUrl. Entry paths come back URI-escaped, so names with spaces or non-ASCII characters yield valid URIs. An entry whose name cannot be converted to a URI is skipped.
      Parameters:
      fileUrl - The URL of the jar file.
      isWindows - true to turn back slashes in fileUrl into forward slashes.
      Returns:
      The entry URIs in jar order. It may be empty.
      Throws:
      IOException - Thrown if the jar file cannot be opened or read.
    • isWindows

      protected boolean isWindows()