Skip to content

Commit

Permalink
Add the option to use multiple parameters in 1 field (#3456)
Browse files Browse the repository at this point in the history
* Add the option to use multiple parameters in 1 field

---------

Co-authored-by: Niek Raaijmakers <[email protected]>
  • Loading branch information
niekraaijmakers and Niek Raaijmakers authored Oct 29, 2024
1 parent 2bb97f0 commit 49e826c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

## Unreleased ([details][unreleased changes details])

### Fixed

- #3460 - Fixes issue where double parameters were not working for the parameterized include

### Changed

- #3420 - Redirect Map Manager - enable Redirect Map Manager in AEM CS (would require a specific - not public yet - AEM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public class NamespaceDecoratedValueMapBuilder {
private final Map<String,Object> copyMap;
private final String[] namespacedProperties;

static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("(\\$\\{\\{([a-zA-Z0-9]+?)(:(.+?))??\\}\\})+?");
static final Pattern PLACEHOLDER_TYPE_HINTED_PATTERN = Pattern.compile("(.*)\\$\\{\\{(\\(([a-zA-Z]+)\\)){1}([a-zA-Z0-9]+)(:(.+))?\\}\\}(.*)?");
static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{\\{(?:\\(([a-zA-Z]+)\\))?([a-zA-Z0-9]+)(:(.*?))?\\}\\}");

public NamespaceDecoratedValueMapBuilder(SlingHttpServletRequest request, Resource resource, String[] namespacedProperties) {
this.request = request;
Expand Down Expand Up @@ -129,59 +128,34 @@ private void applyDynamicVariables() {


private Object filter(String value, SlingHttpServletRequest request) {
Object filtered = applyTypeHintedPlaceHolders(value, request);

if(filtered != null){
return filtered;
}

return applyPlaceHolders(value, request);
}

private Object applyTypeHintedPlaceHolders(String value, SlingHttpServletRequest request) {
Matcher matcher = PLACEHOLDER_TYPE_HINTED_PATTERN.matcher(value);

if (matcher.find()) {

String prefix = matcher.group(1);
String typeHint = matcher.group(3);
String paramKey = matcher.group(4);
String defaultValue = matcher.group(6);
String suffix = matcher.group(7);

String requestParamValue = (request.getAttribute(PREFIX + paramKey) != null) ? request.getAttribute(PREFIX + paramKey).toString() : null;
String chosenValue = defaultString(requestParamValue, defaultValue);
String finalValue = defaultIfEmpty(prefix, EMPTY) + chosenValue + defaultIfEmpty(suffix, EMPTY);

return isNotEmpty(typeHint) ? castTypeHintedValue(typeHint, finalValue) : finalValue;
}

return null;
}

private String applyPlaceHolders(String value, SlingHttpServletRequest request) {
Matcher matcher = PLACEHOLDER_PATTERN.matcher(value);
StringBuffer buffer = new StringBuffer();

// Replace all occurrences
StringBuffer result = new StringBuffer();

while (matcher.find()) {
// Retrieve groups for Typecast, paramKey, and default value

String typeHint = matcher.group(1);
String paramKey = matcher.group(2);
String defaultValue = matcher.group(4);

String requestParamValue = (request.getAttribute(PREFIX + paramKey) != null) ? request.getAttribute(PREFIX + paramKey).toString() : null;
String chosenValue = defaultString(requestParamValue, defaultValue);

if(chosenValue == null){
chosenValue = StringUtils.EMPTY;
}

matcher.appendReplacement(buffer, chosenValue);
String replacement = isNotEmpty(typeHint) ? castTypeHintedValue(typeHint, chosenValue).toString() : chosenValue;

if(replacement == null){
replacement = "";
}
// Append the replacement to the result
matcher.appendReplacement(result, replacement);
}

matcher.appendTail(buffer);
// Append the remaining text
matcher.appendTail(result);

return buffer.toString();
return result.toString();
}

private Object castTypeHintedValue(String typeHint, String chosenValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ public void test() {
parameters.put("doubleFieldDefaultValue", 11.34d);
parameters.put("hideAField", Boolean.TRUE);
parameters.put("fieldLabelText", "Some Text from parameters");
parameters.put("fieldDescriptionText", "someFieldDescription");
parameters.put("suffixText", "SuffixTextTest");

setParameters(parameters);

systemUnderTest = new NamespaceResourceWrapper(context.currentResource(), expressionResolver, context.request(), properties);



Resource someMultiExpressionField = systemUnderTest.getChild("someMultiExpressionField");
String multiExpressionValue = someMultiExpressionField.getValueMap().get("fieldDescription", "");
assertEquals("someFieldDescription otherText someFieldDescription evenMoreText SuffixTextTest", multiExpressionValue);

Resource someDoubleField = systemUnderTest.getChild("someDoubleField");
Double doubleDefaultValue = someDoubleField.getValueMap().get("defaultValue", Double.class);
Expand Down Expand Up @@ -119,6 +122,11 @@ public void test_default_values() {
Resource regularTextField = systemUnderTest.getChild("someRegularField");
String fieldLabelValue = regularTextField.getValueMap().get("fieldLabel", "");
assertEquals("defaultText", fieldLabelValue);

Resource someMultiExpressionField = systemUnderTest.getChild("someMultiExpressionField");
String multiExpressionValue = someMultiExpressionField.getValueMap().get("fieldDescription", "");
assertEquals("defaultDescription otherText otherDefaultDescription evenMoreText ", multiExpressionValue);

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
"sling:resourceType": "granite/ui/components/foundation/container",
"items": {
"jcr:primaryType": "nt:unstructured",
"someMultiExpressionField": {
"jcr:primaryType": "nt:unstructured",
"name": "./multiExpressionField",
"text": "Some multiExpressionField",
"value": "true",
"sling:resourceType": "granite/ui/components/foundation/form/textfield",
"fieldDescription": "${{(String)fieldDescriptionText:defaultDescription}} otherText ${{(String)fieldDescriptionText:otherDefaultDescription}} evenMoreText ${{suffixText}}"
},
"someDoubleField": {
"jcr:primaryType": "nt:unstructured",
"name": "./doubleField",
Expand Down

0 comments on commit 49e826c

Please sign in to comment.