Class RegexMacroReplacer
java.lang.Object
com.winterhavenmc.library.messagebuilder.adapters.pipeline.replacers.RegexMacroReplacer
- All Implemented Interfaces:
MacroReplacer
Default implementation of the
MacroReplacer interface that performs macro substitution
on input strings using values derived from a MacroObjectMap.
This replacer extracts macros from the message using configurable regular expressions,
then delegates resolution of each macro to a ValueResolver.
Once resolved, macros are replaced in the string using their corresponding values.
The class supports two levels of macro detection:
- BASE_KEY_PATTERN — for determining candidate base keys used in resolution
- FULL_KEY_PATTERN — for actual replacement of placeholders in the final string
Unresolvable macros are left intact in the output string, ensuring safe and fail-tolerant rendering behavior.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternPattern used to match base macro keys (e.g.,[ITEM}) for macro resolution via theValueResolver.static final PatternPattern used to match fully qualified macro placeholders (e.g.,%ITEM.NAME_SINGULAR%) for the purpose of final string replacement. -
Constructor Summary
ConstructorsConstructorDescriptionRegexMacroReplacer(ValueResolver resolver, PlaceholderMatcher placeholderMatcher) Constructs aRegexMacroReplacerwith the given resolver and placeholder matcher. -
Method Summary
Modifier and TypeMethodDescriptionreplace(MacroObjectMap macroObjectMap, String messageString) Replaces all macro placeholders found in the given message string using the providedMacroObjectMap.
-
Field Details
-
FULL_KEY_PATTERN
Pattern used to match fully qualified macro placeholders (e.g.,%ITEM.NAME_SINGULAR%) for the purpose of final string replacement. -
BASE_KEY_PATTERN
Pattern used to match base macro keys (e.g.,[ITEM}) for macro resolution via theValueResolver.This pattern determines which objects should be passed to the resolver to populate values for all matching derived keys.
-
-
Constructor Details
-
RegexMacroReplacer
Constructs aRegexMacroReplacerwith the given resolver and placeholder matcher.- Parameters:
resolver- the macro resolver responsible for producing string values for each macroplaceholderMatcher- the matcher used to identify macro patterns in message strings
-
-
Method Details
-
replace
Replaces all macro placeholders found in the given message string using the providedMacroObjectMap.The replacement process is performed in two stages:
- Extract base macro keys from the string using BASE_KEY_PATTERN
- Resolve and populate a
MacroStringMapwith string values - Substitute each matching placeholder using FULL_KEY_PATTERN
If a macro cannot be resolved, it is left in-place as its original placeholder form.
- Specified by:
replacein interfaceMacroReplacer- Parameters:
macroObjectMap- a map of macro keys to context objects used for resolutionmessageString- the input string containing macro placeholders (may benull)- Returns:
- the final formatted message with macros replaced
-