This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch feature/customize-interpolator-3.x in repository https://gitbox.apache.org/repos/asf/maven-filtering.git
commit 035d1d3700983a0683d67db81cdf93fb84e510de Author: Konrad Windszus <k...@apache.org> AuthorDate: Mon Aug 5 17:29:29 2024 +0200 [MSHARED-1412] Allow to customize Interpolator used by filter (#104) Backport to 3.x --- pom.xml | 2 +- .../filtering/AbstractMavenFilteringRequest.java | 22 +++++++++++++++++++ .../apache/maven/shared/filtering/BaseFilter.java | 13 +++++++++-- .../filtering/DefaultMavenFileFilterTest.java | 25 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 97ab32e..3e4c2b3 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ </parent> <artifactId>maven-filtering</artifactId> - <version>3.3.3-SNAPSHOT</version> + <version>3.4.0-SNAPSHOT</version> <name>Apache Maven Filtering</name> <description>A component to assist in filtering of resource files with properties from a Maven project.</description> diff --git a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java index 503e70e..6daebd8 100644 --- a/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java +++ b/src/main/java/org/apache/maven/shared/filtering/AbstractMavenFilteringRequest.java @@ -22,9 +22,11 @@ import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; +import java.util.function.Consumer; import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.interpolation.Interpolator; /** * @since 1.0-beta-3 @@ -79,6 +81,8 @@ public class AbstractMavenFilteringRequest { */ private boolean supportMultiLineFiltering; + private Consumer<Interpolator> interpolatorCustomizer; + /** * Create instance. */ @@ -338,4 +342,22 @@ public class AbstractMavenFilteringRequest { public void setSupportMultiLineFiltering(boolean supportMultiLineFiltering) { this.supportMultiLineFiltering = supportMultiLineFiltering; } + + /** + * + * @return the customizer which is supposed to be used by filters creating an {@link Interpolator} like those based on {@link BaseFilter}. + * @since 3.4.0 + */ + public Consumer<Interpolator> getInterpolatorCustomizer() { + return interpolatorCustomizer; + } + + /** + * + * @param interpolatorCustomizer the customizer which is supposed to be used by filters creating an {@link Interpolator} like those based on {@link BaseFilter}. + * @since 3.4.0 + */ + public void setInterpolatorCustomizer(Consumer<Interpolator> interpolatorCustomizer) { + this.interpolatorCustomizer = interpolatorCustomizer; + } } diff --git a/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java b/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java index 256c2c5..bb72f1f 100644 --- a/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java +++ b/src/main/java/org/apache/maven/shared/filtering/BaseFilter.java @@ -27,6 +27,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; import java.util.TreeSet; +import java.util.function.Consumer; import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; @@ -173,7 +174,8 @@ class BaseFilter implements DefaultFilterInfo { request.getProjectStartExpressions(), request.getEscapeString(), request.isEscapeWindowsPaths(), - request.isSupportMultiLineFiltering()); + request.isSupportMultiLineFiltering(), + request.getInterpolatorCustomizer()); defaultFilterWrappers.add(wrapper); @@ -232,6 +234,8 @@ class BaseFilter implements DefaultFilterInfo { private boolean supportMultiLineFiltering; + private Consumer<Interpolator> interpolatorCustomizer; + Wrapper( LinkedHashSet<String> delimiters, MavenProject project, @@ -240,7 +244,8 @@ class BaseFilter implements DefaultFilterInfo { List<String> projectStartExpressions, String escapeString, boolean escapeWindowsPaths, - boolean supportMultiLineFiltering) { + boolean supportMultiLineFiltering, + Consumer<Interpolator> interpolatorCustomizer) { super(); this.delimiters = delimiters; this.project = project; @@ -250,6 +255,7 @@ class BaseFilter implements DefaultFilterInfo { this.escapeString = escapeString; this.escapeWindowsPaths = escapeWindowsPaths; this.supportMultiLineFiltering = supportMultiLineFiltering; + this.interpolatorCustomizer = interpolatorCustomizer; } @Override @@ -262,6 +268,9 @@ class BaseFilter implements DefaultFilterInfo { mavenSession, escapeString, escapeWindowsPaths); + if (interpolatorCustomizer != null) { + interpolatorCustomizer.accept(interpolator); + } MultiDelimiterInterpolatorFilterReaderLineEnding filterReader = new MultiDelimiterInterpolatorFilterReaderLineEnding( diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java index b0ca05a..db0a71b 100644 --- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java +++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenFileFilterTest.java @@ -21,6 +21,7 @@ package org.apache.maven.shared.filtering; import javax.inject.Inject; import java.io.File; +import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.nio.file.Files; @@ -33,6 +34,7 @@ import java.util.Properties; import org.apache.commons.io.IOUtils; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.interpolation.AbstractValueSource; import org.codehaus.plexus.testing.PlexusTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -154,4 +156,27 @@ class DefaultMavenFileFilterTest { assertEquals("t...@titi.com bar", IOUtils.toString(reader)); } } + + @Test + void testInterpolatorCustomizer() throws MavenFilteringException, IOException { + AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest(); + req.setInterpolatorCustomizer(i -> { + i.addValueSource(new AbstractValueSource(false) { + + @Override + public Object getValue(String expression) { + if (expression.equals("foo")) { + return "bar"; + } + return null; + } + }); + }); + + List<FilterWrapper> wrappers = mavenFileFilter.getDefaultFilterWrappers(req); + + try (Reader reader = wrappers.get(0).getReader(new StringReader("t...@titi.com ${foo}"))) { + assertEquals("t...@titi.com bar", IOUtils.toString(reader)); + } + } }