Package org.apache.tika.config
Class JsonConfigHelper
java.lang.Object
org.apache.tika.config.JsonConfigHelper
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.JsonNodeapplyReplacements(com.fasterxml.jackson.databind.JsonNode root, Map<String, Object> replacements) Applies replacements to a JsonNode tree, modifying it in place.static com.fasterxml.jackson.databind.ObjectMapperReturns the ObjectMapper used by this helper.static com.fasterxml.jackson.databind.JsonNodeLoads a JSON config template from a file path and applies replacements.static com.fasterxml.jackson.databind.JsonNodeLoads a JSON config template from a resource path and applies replacements.static com.fasterxml.jackson.databind.JsonNodeloadFromString(String jsonTemplate, Map<String, Object> replacements) Loads a JSON config template from a string and applies replacements.static StringtoJsonPath(Path path) Converts a Path to a JSON-safe string with forward slashes.static PathLoads a template, applies replacements, and writes to an output file.static PathwriteConfigFromResource(String resourcePath, Class<?> clazz, Map<String, Object> replacements, Path outputPath) Loads a template from resources, applies replacements, and writes to an output file.
-
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 IOExceptionLoads 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 loadingreplacements- 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 IOExceptionLoads a JSON config template from a file path and applies replacements.- Parameters:
templatePath- path to the template filereplacements- 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 IOExceptionLoads a JSON config template from a string and applies replacements.- Parameters:
jsonTemplate- the JSON template stringreplacements- 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 IOExceptionLoads a template, applies replacements, and writes to an output file.- Parameters:
templatePath- path to the template filereplacements- map of placeholder names to replacement valuesoutputPath- 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 IOExceptionLoads a template from resources, applies replacements, and writes to an output file.- Parameters:
resourcePath- path to template resourceclazz- class to use for resource loadingreplacements- map of placeholder names to replacement valuesoutputPath- 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 modifyreplacements- map of placeholder names to replacement values- Returns:
- the modified root node
-
toJsonPath
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
-