Class AbstractClassPathModelFinder
- All Implemented Interfaces:
ClassPathModelFinder
- Direct Known Subclasses:
ClassgraphModelFinder,DirectoryModelFinder,SimpleClassPathModelFinder
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprotected static final Stringprivate final Stringprivate static final org.slf4j.Loggerprivate Set<ClassPathModelEntry> private final Objectprivate static final Stringprivate static final StringFields inherited from interface opennlp.tools.models.ClassPathModelFinder
OPENNLP_MODEL_JAR_PREFIX -
Constructor Summary
ConstructorsConstructorDescriptionBy default, it scans forClassPathModelFinder.OPENNLP_MODEL_JAR_PREFIX.AbstractClassPathModelFinder(String jarModelPrefix) -
Method Summary
Modifier and TypeMethodDescriptionfindModels(boolean reloadCache) Finds OpenNLP models available within an applications's classpath.protected abstract Objectprotected StringgetMatchingJarEntryURIs(List<URL> candidates, String wildcardPattern) Returns the entries of every jar incandidateswhose decoded file part matches*followed by thejar model prefix, keeping only the entries whose decoded file part matches*followed bywildcardPattern.getMatchingURIs(String wildcardPattern, Object context) Retrieve matching classpathURIsfor the givenwildcardPattern.getURIsFromJar(URL fileUrl, boolean isWindows) protected booleanprotected booleanjarPathsMatch(URI uri1, URI uri2) protected final booleanmatchesWildcard(URL url, String wildcard) Checks whether the decoded file part ofurlmatcheswildcardfrom start to end, where*stands for any run of characters,?for exactly one Unicode code point, and every other character for itself.protected String[]parseJarURI(URI uri) private Set<ClassPathModelEntry> Scans the classpath for models and pairs each model with themodel.propertiesof its jar.protected static URLConverts alocationin String form to aURL.
-
Field Details
-
JAR
- See Also:
-
logger
private static final org.slf4j.Logger logger -
ANY_RUN
- See Also:
-
WILDCARD_MUST_NOT_BE_NULL
- See Also:
-
URL_MUST_NOT_BE_NULL
- See Also:
-
jarModelPrefix
-
scanLock
-
models
-
-
Constructor Details
-
AbstractClassPathModelFinder
public AbstractClassPathModelFinder()By default, it scans forClassPathModelFinder.OPENNLP_MODEL_JAR_PREFIX. -
AbstractClassPathModelFinder
- 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 benull.- Throws:
IllegalArgumentException- Thrown ifjarModelPrefixisnull.
-
-
Method Details
-
findModels
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:
findModelsin interfaceClassPathModelFinder- Parameters:
reloadCache-true, if the internal cache should explicitly be reloaded,falseotherwise.- Returns:
- A
Setofmodel entries. It might be empty, if none were found.
-
scanModels
Scans the classpath for models and pairs each model with themodel.propertiesof its jar.- Returns:
- The unmodifiable set of model entries. It may be empty.
-
getContext
- Returns:
- A context information object. May be
null.
-
getMatchingURIs
Retrieve matching classpathURIsfor the givenwildcardPattern.- Parameters:
wildcardPattern- The pattern to use for scanning. Must not benull.context- An object holding context information. It might benull.- Returns:
- A list of matching classpath URIs.
-
jarPathsMatch
-
parseJarURI
-
getJarModelPrefix
-
getMatchingJarEntryURIs
Returns the entries of every jar incandidateswhose decoded file part matches*followed by thejar model prefix, keeping only the entries whose decoded file part matches*followed bywildcardPattern. Both masks follow the rules ofmatchesWildcard(URL, String). A jar that cannot be read is logged and skipped.- Parameters:
candidates- TheURLsto check against the jar model prefix. Must not benull.wildcardPattern- The mask for jar entries. Ifnull, nothing matches.- Returns:
- The matching jar entry
URIsin scan order. It may be empty. - Throws:
IllegalArgumentException- Thrown ifcandidatesisnull.
-
matchesWildcard
Checks whether the decoded file part ofurlmatcheswildcardfrom 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. Aurlthat is not a valid URI, such as one with an unescaped space, is matched onURL.getFile()as is.- Parameters:
url- TheURLwhose decoded file part is tested. Must not benull.wildcard- The wildcard expression. Must not benull.- Returns:
trueif the file part matches,falseotherwise.- Throws:
IllegalArgumentException- Thrown ifurlorwildcardisnull.
-
toURL
Converts alocationin String form to aURL.- Parameters:
location- The resource path and/or reference.- Returns:
- The converted
URLform. - Throws:
IOException- Thrown if IO errors occurred during conversion
-
getURIsFromJar
Lists thejar:URIsof all non-directory entries in the jar file atfileUrl. 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- TheURLof the jar file.isWindows-trueto turn back slashes infileUrlinto forward slashes.- Returns:
- The entry
URIsin jar order. It may be empty. - Throws:
IOException- Thrown if the jar file cannot be opened or read.
-
isWindows
protected boolean isWindows()
-