Class ParseContextConfig

java.lang.Object
org.apache.tika.config.ParseContextConfig

public class ParseContextConfig extends Object
Facade for accessing runtime configuration from ParseContext's jsonConfigs.

This wrapper provides a safe way for parsers to access runtime configuration without directly depending on tika-serialization. It performs these critical checks:

  • If ParseContext has JSON config for the requested key but ConfigDeserializer is not on the classpath, throws TikaConfigException with a clear error message
  • If ConfigDeserializer is available, delegates to it for deserialization
  • If no config is present, returns the default config

Usage in parsers:

 PDFParserConfig localConfig = ParseContextConfig.getConfig(
     context, "pdf-parser", PDFParserConfig.class, defaultConfig);
 
Since:
Apache Tika 4.0
  • Constructor Details

    • ParseContextConfig

      public ParseContextConfig()
  • Method Details

    • getConfig

      public static <T> T getConfig(ParseContext context, String configKey, Class<T> configClass, T defaultConfig) throws TikaConfigException, IOException
      Retrieves runtime configuration from ParseContext.

      This method first checks if the config is already resolved in ParseContext (via context.get(configClass)). If found, it returns immediately without re-deserializing. This is efficient for embedded documents where the config was already deserialized for the parent document.

      If not found, it checks jsonConfigs for the config key and deserializes the JSON. The deserialized config is cached in resolvedConfigs and also set in the main ParseContext for future lookups.

      This method performs defensive checking: if the ParseContext has JSON configuration for the requested key but the ConfigDeserializer is not available on the classpath, it throws TikaConfigException to prevent silent failures.

      Type Parameters:
      T - the configuration type
      Parameters:
      context - the parse context (may be null)
      configKey - the configuration key (e.g., "pdf-parser", "html-parser")
      configClass - the configuration class
      defaultConfig - the default configuration to use if no runtime config exists
      Returns:
      the runtime config merged with defaults, or the default config if no runtime config
      Throws:
      TikaConfigException - if ParseContext has JSON config but ConfigDeserializer is not on classpath
      IOException - if deserialization fails
    • hasConfig

      public static boolean hasConfig(ParseContext context, String configKey)
      Checks if runtime configuration exists for the given key.

      Unlike getConfig(org.apache.tika.parser.ParseContext, java.lang.String, java.lang.Class<T>, T), this method does NOT throw if ConfigDeserializer is missing - it only checks for the presence of config.

      Parameters:
      context - the parse context
      configKey - the configuration key
      Returns:
      true if JSON config exists for this key
    • isConfigDeserializerAvailable

      public static boolean isConfigDeserializerAvailable()
      Checks if ConfigDeserializer is available on the classpath.
      Returns:
      true if tika-serialization is available