Skip to content

Commit

Permalink
[MSHARED-1412] Allow to customize Interpolator used by filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Jun 20, 2024
1 parent 9d528d0 commit f25600b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
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
Expand Down Expand Up @@ -79,6 +81,8 @@ public class AbstractMavenFilteringRequest {
*/
private boolean supportMultiLineFiltering;

private Consumer<Interpolator> interpolatorCustomizer;

/**
* Create instance.
*/
Expand Down Expand Up @@ -338,4 +342,22 @@ public boolean isSupportMultiLineFiltering() {
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;
}
}
13 changes: 11 additions & 2 deletions src/main/java/org/apache/maven/shared/filtering/BaseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -173,7 +174,8 @@ public List<FilterWrapper> getDefaultFilterWrappers(final AbstractMavenFiltering
request.getProjectStartExpressions(),
request.getEscapeString(),
request.isEscapeWindowsPaths(),
request.isSupportMultiLineFiltering());
request.isSupportMultiLineFiltering(),
request.getInterpolatorCustomizer());

defaultFilterWrappers.add(wrapper);

Expand Down Expand Up @@ -232,6 +234,8 @@ private static final class Wrapper extends FilterWrapper {

private boolean supportMultiLineFiltering;

private Consumer<Interpolator> interpolatorCustomizer;

Wrapper(
LinkedHashSet<String> delimiters,
MavenProject project,
Expand All @@ -240,7 +244,8 @@ private static final class Wrapper extends FilterWrapper {
List<String> projectStartExpressions,
String escapeString,
boolean escapeWindowsPaths,
boolean supportMultiLineFiltering) {
boolean supportMultiLineFiltering,
Consumer<Interpolator> interpolatorCustomizer) {
super();
this.delimiters = delimiters;
this.project = project;
Expand All @@ -250,6 +255,7 @@ private static final class Wrapper extends FilterWrapper {
this.escapeString = escapeString;
this.escapeWindowsPaths = escapeWindowsPaths;
this.supportMultiLineFiltering = supportMultiLineFiltering;
this.interpolatorCustomizer = interpolatorCustomizer;
}

@Override
Expand All @@ -262,6 +268,9 @@ public Reader getReader(Reader reader) {
mavenSession,
escapeString,
escapeWindowsPaths);
if (interpolatorCustomizer != null) {
interpolatorCustomizer.accept(interpolator);
}

MultiDelimiterInterpolatorFilterReaderLineEnding filterReader =
new MultiDelimiterInterpolatorFilterReaderLineEnding(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.shared.filtering;

import java.util.function.Consumer;

import org.codehaus.plexus.interpolation.Interpolator;

@FunctionalInterface
public interface InterpolatorCustomizerSupplier {
Consumer<Interpolator> getInterpolatorCustomizer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -33,6 +34,7 @@

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;
Expand Down Expand Up @@ -154,4 +156,27 @@ void lineWithSingleAtAndExpression() throws Exception {
assertEquals("[email protected] 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("[email protected] ${foo}"))) {
assertEquals("[email protected] bar", IOUtils.toString(reader));
}
}
}

0 comments on commit f25600b

Please sign in to comment.