Class ExtensionLoader

Namespace
NOpenNLP.Tools.Util.Ext
Assembly
NOpenNLP.Tools.dll

The ExtensionLoader is responsible to load extensions to the OpenNLP library.

Only types whose full name starts with a registered namespace prefix are permitted. The default allowed prefixes are NOpenNLP. and opennlp., which cover all built-in factories and serializers, both under their ported names and under the Java names that serialized OpenNLP models record.

To allow custom extension types from other namespaces, either:

  • Call RegisterAllowedPackage(string) programmatically before loading any model that uses the custom type.
  • Set the OPENNLP_EXT_ALLOWED_PACKAGES setting to a comma-separated list of namespace prefixes before the type is first used.

Note: Do not use this class, internal use only!
public class ExtensionLoader
Inheritance
ExtensionLoader
Inherited Members

Fields

ALLOWED_PACKAGES_PROPERTY

Setting for supplying additional allowed namespace prefixes. The value is a comma-separated list, e.g. Acme.Nlp.,Other..

This setting is read once when the type is initialized. If it cannot be supplied that early, call RegisterAllowedPackage(string) before loading any model that uses a custom factory or serializer.

public const string ALLOWED_PACKAGES_PROPERTY = "OPENNLP_EXT_ALLOWED_PACKAGES"

Field Value

string

Methods

InstantiateExtension<T>(string)

Instantiates an user provided extension to OpenNLP.

The extension is either loaded from the class path or if running inside an OSGi environment via an OSGi service.

Initially it tries using the public default constructor. If it is not found, it will check if the class follows the singleton pattern: a static field named INSTANCE that returns an object of the type T.

public static T? InstantiateExtension<T>(string extensionClassName)

Parameters

extensionClassName string

Returns

T

the instance of the extension class

Type Parameters

T

RegisterAllowedPackage(string)

Registers an additional namespace prefix whose types are permitted to be loaded as OpenNLP extensions. Call this once at application startup, before loading any model that uses a custom factory or serializer from that namespace.

The prefix is normalized to end with '.' to prevent collision attacks (e.g. registering "Acme" cannot be exploited via "AcmeEvil.*").

public static void RegisterAllowedPackage(string packagePrefix)

Parameters

packagePrefix string

The namespace prefix to allow, e.g. "Example.Nlp". Must not be null or blank.

Exceptions

ArgumentNullException

Thrown if packagePrefix is null.

ArgumentException

Thrown if packagePrefix is blank.

UnregisterAllowedPackage(string)

Removes a previously registered namespace prefix. Has no effect if the prefix was not registered. The default NOpenNLP. and opennlp. prefixes can also be removed, though this is not recommended.

The prefix is normalized to end with '.' before removal, matching the normalization applied in RegisterAllowedPackage(string).

public static void UnregisterAllowedPackage(string packagePrefix)

Parameters

packagePrefix string

The namespace prefix to remove, e.g. "Example.Nlp". Must not be null.

Exceptions

ArgumentNullException

Thrown if packagePrefix is null.