Class JsonConfigHelper

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

public class JsonConfigHelper extends Object
Helper class for loading JSON config templates with placeholder replacement.

This provides a type-safe alternative to String.replace() for JSON configs, properly handling paths (converting backslashes to forward slashes on Windows) and different value types (strings, integers, doubles, booleans).

Example template JSON:

 {
   "fetchers": {
     "fs": {
       "file-system-fetcher": {
         "basePath": "FETCHER_BASE_PATH"
       }
     }
   },
   "pipes": {
     "maxFiles": "MAX_FILES",
     "enabled": "ENABLED"
   }
 }
 

Usage:

 Map<String, Object> replacements = Map.of(
     "FETCHER_BASE_PATH", tmpDir.resolve("input"),  // Path
     "MAX_FILES", 100,                               // Integer
     "ENABLED", true                                 // Boolean
 );
 JsonNode config = JsonConfigHelper.load(templatePath, replacements);
 
  • Constructor Details

    • JsonConfigHelper

      public JsonConfigHelper()
  • Method Details

    • loadFromResource

      public static com.fasterxml.jackson.databind.JsonNode loadFromResource(String resourcePath, Class<?> clazz, Map<String,Object> replacements) throws IOException
      Loads a JSON config template from a resource path and applies replacements.
      Parameters:
      resourcePath - path to template resource (e.g., "/configs/template.json")
      clazz - class to use for resource loading
      replacements - map of placeholder names to replacement values
      Returns:
      the modified JsonNode tree
      Throws:
      IOException - if the template cannot be read or parsed
    • load

      public static com.fasterxml.jackson.databind.JsonNode load(Path templatePath, Map<String,Object> replacements) throws IOException
      Loads a JSON config template from a file path and applies replacements.
      Parameters:
      templatePath - path to the template file
      replacements - map of placeholder names to replacement values
      Returns:
      the modified JsonNode tree
      Throws:
      IOException - if the template cannot be read or parsed
    • loadFromString

      public static com.fasterxml.jackson.databind.JsonNode loadFromString(String jsonTemplate, Map<String,Object> replacements) throws IOException
      Loads a JSON config template from a string and applies replacements.
      Parameters:
      jsonTemplate - the JSON template string
      replacements - map of placeholder names to replacement values
      Returns:
      the modified JsonNode tree
      Throws:
      IOException - if the template cannot be parsed
    • writeConfig

      public static Path writeConfig(Path templatePath, Map<String,Object> replacements, Path outputPath) throws IOException
      Loads a template, applies replacements, and writes to an output file.
      Parameters:
      templatePath - path to the template file
      replacements - map of placeholder names to replacement values
      outputPath - path to write the result
      Returns:
      the output path
      Throws:
      IOException - if reading or writing fails
    • writeConfigFromResource

      public static Path writeConfigFromResource(String resourcePath, Class<?> clazz, Map<String,Object> replacements, Path outputPath) throws IOException
      Loads a template from resources, applies replacements, and writes to an output file.
      Parameters:
      resourcePath - path to template resource
      clazz - class to use for resource loading
      replacements - map of placeholder names to replacement values
      outputPath - path to write the result
      Returns:
      the output path
      Throws:
      IOException - if reading or writing fails
    • applyReplacements

      public static com.fasterxml.jackson.databind.JsonNode applyReplacements(com.fasterxml.jackson.databind.JsonNode root, Map<String,Object> replacements)
      Applies replacements to a JsonNode tree, modifying it in place.
      Parameters:
      root - the root node to modify
      replacements - map of placeholder names to replacement values
      Returns:
      the modified root node
    • toJsonPath

      public static String toJsonPath(Path path)
      Converts a Path to a JSON-safe string with forward slashes. This handles Windows paths correctly.
      Parameters:
      path - the path to convert
      Returns:
      the path string with forward slashes
    • getMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getMapper()
      Returns the ObjectMapper used by this helper. Useful for additional JSON operations.
      Returns:
      the ObjectMapper instance