-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use separate
MapResolver
for map resolving
- Loading branch information
1 parent
0d27dcb
commit 090b918
Showing
6 changed files
with
82 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/docutools/jocument/impl/MapResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.docutools.jocument.impl; | ||
|
||
import com.docutools.jocument.CustomPlaceholderRegistry; | ||
import com.docutools.jocument.GenerationOptions; | ||
import com.docutools.jocument.PlaceholderData; | ||
import com.docutools.jocument.PlaceholderResolver; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.commons.lang3.ClassUtils; | ||
|
||
public class MapResolver extends PlaceholderResolver { | ||
|
||
private final Map<String, Object> map; | ||
private final CustomPlaceholderRegistry customPlaceholderRegistry; //TODO this should be refactored to a separate resolver or class-adjacent methods | ||
private final PlaceholderResolver parent; //FIXME add this field to FutureReflectionResolver | ||
private final GenerationOptions generationOptions; | ||
|
||
/** | ||
* Create a new {@link PlaceholderResolver} to resolve from {@link Map}s. | ||
* | ||
* @param map The {@link Map} to resolve from | ||
* @param customPlaceholderRegistry The custom placeholder registry to check for custom placeholders | ||
* @param parent The parent registry | ||
* @param generationOptions the {@link GenerationOptions} | ||
*/ | ||
public MapResolver(Map<String, Object> map, CustomPlaceholderRegistry customPlaceholderRegistry, PlaceholderResolver parent, | ||
GenerationOptions generationOptions) { | ||
this.map = map; | ||
this.customPlaceholderRegistry = customPlaceholderRegistry; | ||
this.parent = parent; | ||
this.generationOptions = generationOptions; | ||
} | ||
|
||
@Override | ||
protected Optional<PlaceholderData> doResolve(String placeholderName, Locale locale) { | ||
if (map.containsKey(placeholderName)) { | ||
var value = map.get(placeholderName); | ||
if (value instanceof Map childMap) { | ||
return Optional.of(new IterablePlaceholderData(new MapResolver(childMap, customPlaceholderRegistry, this, generationOptions))); | ||
} else if (ClassUtils.isPrimitiveOrWrapper(value.getClass()) || value instanceof String) { | ||
return Optional.of(new ScalarPlaceholderData<>(value)); | ||
} else { | ||
var futureResolver = | ||
new FutureReflectionResolver(value, customPlaceholderRegistry, options, 10L); //TODO find way to decouple timout time from resolver | ||
return futureResolver.doResolve(placeholderName, locale); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters