Skip to content

Commit

Permalink
Pass delimiter information to ValueSource
Browse files Browse the repository at this point in the history
This closes #76
  • Loading branch information
kwin committed Sep 18, 2024
1 parent 1ffe34b commit 041e6bc
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ protected ValueSource getDelegate() {
return delegate;
}

@Override
public Object getValue(String expression, String delimiterStart, String delimiterEnd) {
return getDelegate().getValue(expression, delimiterStart, delimiterEnd);
}

@Override
public Object getValue(String expression) {
return getDelegate().getValue(expression);
}

@Override
public void clearFeedback() {
delegate.clearFeedback();
}

@Override
public List getFeedback() {
return delegate.getFeedback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,36 @@ public String interpolate(String input, String thisPrefixPattern, RecursionInter

int realExprGroup = 2;
Pattern expressionPattern;
final String expressionDelimiterStart;
final String expressionDelimiterEnd;
if (startRegex != null || endRegex != null) {
if (thisPrefixPattern == null) {
expressionPattern = getPattern(startRegex + endRegex);
realExprGroup = 1;
} else {
expressionPattern = getPattern(startRegex + thisPrefixPattern + endRegex);
}
expressionDelimiterStart = startRegex;
expressionDelimiterEnd = endRegex;

} else if (thisPrefixPattern != null) {
expressionPattern = getPattern("\\$\\{(" + thisPrefixPattern + ")?(.+?)\\}");
} else {
expressionPattern = getPattern(DEFAULT_REGEXP);
realExprGroup = 1;
expressionDelimiterStart = "${";
expressionDelimiterEnd = "}";
if (thisPrefixPattern != null) {
expressionPattern = getPattern("\\$\\{(" + thisPrefixPattern + ")?(.+?)\\}");
} else {
expressionPattern = getPattern(DEFAULT_REGEXP);
realExprGroup = 1;
}
}

try {
return interpolate(input, recursionInterceptor, expressionPattern, realExprGroup);
return interpolate(
input,
recursionInterceptor,
expressionPattern,
expressionDelimiterStart,
expressionDelimiterEnd,
realExprGroup);
} finally {
if (!cacheAnswers) {
clearAnswers();
Expand Down Expand Up @@ -228,7 +241,12 @@ private Pattern getPattern(String regExp) {
* @todo Ensure unresolvable expressions don't trigger infinite recursion.
*/
private String interpolate(
String input, RecursionInterceptor recursionInterceptor, Pattern expressionPattern, int realExprGroup)
String input,
RecursionInterceptor recursionInterceptor,
Pattern expressionPattern,
String expressionDelimiterStart,
String expressionDelimiterEnd,
int realExprGroup)
throws InterpolationException {
if (input == null) {
// return empty String to prevent NPE too
Expand Down Expand Up @@ -256,11 +274,17 @@ private String interpolate(
for (ValueSource vs : valueSources) {
if (value != null) break;

value = vs.getValue(realExpr);
value = vs.getValue(realExpr, expressionDelimiterStart, expressionDelimiterEnd);
}

if (value != null) {
value = interpolate(String.valueOf(value), recursionInterceptor, expressionPattern, realExprGroup);
value = interpolate(
String.valueOf(value),
recursionInterceptor,
expressionPattern,
expressionDelimiterStart,
expressionDelimiterEnd,
realExprGroup);

if (postProcessors != null && !postProcessors.isEmpty()) {
for (InterpolationPostProcessor postProcessor : postProcessors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private String interpolate(String input, RecursionInterceptor recursionIntercept
if (value != null) {
break;
}
value = valueSource.getValue(realExpr);
value = valueSource.getValue(realExpr, startExpr, endExpr);

if (value != null && value.toString().contains(wholeExpr)) {
bestAnswer = value;
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/codehaus/plexus/interpolation/ValueSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@
public interface ValueSource {

/**
* Returns a value resolved from an expression. The return value is recursively resolved via {@link Interpolator#interpolate(String)}, i.e. might contain expressions as well.
* @param expression The string expression.
* @return the value related to the expression, or null if not found.
* @param expressionStartDelimiter A valid start delimiter of the expression to be used with the calling {@link Interpolator} (by default <code>${</code>).
* @param expressionEndDelimiter A valid end delimiter of the expression to be used with the calling {@link Interpolator} (by default <code>}</code>).
* @return the value related to the expression, or {@code null} if not found. This value might contain other expressions separated by {@code expressionStartDelimiter} and {@code expressionEndDelimiter}
* @since 1.28
*/
default Object getValue(String expression, String expressionStartDelimiter, String expressionEndDelimiter) {
return getValue(expression);
}

/**
* @param expression The string expression.
* @return the value related to the expression, or {@code null} if not found.
* @see #getValue(String, String, String)
*/
public Object getValue(String expression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private String interpolate(String input, RecursionInterceptor recursionIntercept
for (ValueSource vs : valueSources) {
if (value != null) break;

value = vs.getValue(realExpr);
value = vs.getValue(realExpr, startExpr, endExpr);

if (value != null && value.toString().contains(wholeExpr)) {
bestAnswer = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ public void testShouldResolveByContextValue() throws InterpolationException {
assertEquals("this is a testVar", result);
}

@Test
public void testDelimitersPassedToValueSource() throws InterpolationException {
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
interpolator.addValueSource(new AbstractValueSource(false) {

@Override
public Object getValue(String expression, String expressionStartDelimiter, String expressionEndDelimiter) {
assertEquals("${", expressionStartDelimiter);
assertEquals("}", expressionEndDelimiter);
return expression;
}

@Override
public Object getValue(String expression) {
fail("This method is not supposed to be called");
return null;
}
});

assertEquals("test", interpolator.interpolate("${test}"));
}

@Test
public void testShouldResolveByEnvar() throws IOException, InterpolationException {
OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ public void testLongDelimitersWithNoContext() throws InterpolationException {
assertEquals(result, interpolator.interpolate(src));
}

@Test
public void testLongDelimitersPassedToValueSource() throws InterpolationException {
String src = "<expression>test</expression>";

StringSearchInterpolator interpolator = new StringSearchInterpolator("<expression>", "</expression>");
interpolator.addValueSource(new AbstractValueSource(false) {

@Override
public Object getValue(String expression, String expressionStartDelimiter, String expressionEndDelimiter) {
assertEquals("<expression>", expressionStartDelimiter);
assertEquals("</expression>", expressionEndDelimiter);
return expression;
}

@Override
public Object getValue(String expression) {
fail("This method is not supposed to be called");
return null;
}
});

assertEquals("test", interpolator.interpolate(src));
}

@Test
public void testSimpleSubstitution() throws InterpolationException {
Properties p = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.ValueSource;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class MultiDelimiterStringSearchInterpolatorTest {

Expand Down Expand Up @@ -119,4 +121,29 @@ public void testInterpolationWithMultipleEscapes3() throws InterpolationExceptio

assertEquals("##${first} and #${second} and beer", result);
}

@Test
public void testDelimitersPassedToValueSource() throws InterpolationException {
ValueSource vs = new AbstractValueSource(false) {

@Override
public Object getValue(String expression, String expressionStartDelimiter, String expressionEndDelimiter) {
assertEquals("#(", expressionStartDelimiter);
assertEquals(")", expressionEndDelimiter);
return expression;
}

@Override
public Object getValue(String expression) {
fail("This method is not supposed to be called");
return null;
}
};
MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator() //
.withValueSource(vs) //
.escapeString("#");
interpolator.addDelimiterSpec("#(*)");

assertEquals("test", interpolator.interpolate("#(test)"));
}
}

0 comments on commit 041e6bc

Please sign in to comment.