Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1412] Allow to customize Interpolator used by filter #104

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

kwin
Copy link
Member

@kwin kwin commented Jun 20, 2024

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MSHARED-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MSHARED-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify -Prun-its to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@kwin kwin requested review from gnodet and slachiewicz June 20, 2024 07:59
@kwin kwin force-pushed the feature/interpolator-customizer branch from f25600b to b8318ee Compare June 20, 2024 08:01
@gnodet
Copy link
Contributor

gnodet commented Jun 20, 2024

@kwin is there an immediate use case for this ? which maven plugin will use it ? just curious...

@kwin
Copy link
Member Author

kwin commented Jun 20, 2024

The use case for me is https://issues.apache.org/jira/browse/JCRVLT-757 (outside the Maven project). But I can imagine that for some advanced use cases also the maven-resources-plugin could leverage that (with automatically registered Sisu components)...

@olamy
Copy link
Member

olamy commented Jun 20, 2024

@kwin
Copy link
Member Author

kwin commented Jun 20, 2024

The problem is I want the default filter behaviour, which is currently not reusable in custom filter (i.e. all the code from

public List<FilterWrapper> getDefaultFilterWrappers(final AbstractMavenFilteringRequest request)
throws MavenFilteringException {
// backup values
boolean supportMultiLineFiltering = request.isSupportMultiLineFiltering();
request.setSupportMultiLineFiltering(supportMultiLineFiltering);
// Here we build some properties which will be used to read some properties files
// to interpolate the expression ${ } in this properties file
// Take a copy of filterProperties to ensure that evaluated filterTokens are not propagated
// to subsequent filter files. Note: this replicates current behaviour and seems to make sense.
final Properties baseProps = new Properties();
// Project properties
if (request.getMavenProject() != null) {
baseProps.putAll(
request.getMavenProject().getProperties() == null
? Collections.emptyMap()
: request.getMavenProject().getProperties());
}
// TODO this is NPE free but do we consider this as normal
// or do we have to throw an MavenFilteringException with mavenSession cannot be null
//
// khmarbaise: 2016-05-21:
// If we throw an MavenFilteringException tests will fail which is
// caused by for example:
// void copyFile( File from, final File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
// String encoding )
// in MavenFileFilter interface where no MavenSession is given.
// So changing here to throw a MavenFilteringException would make
// it necessary to change the interface or we need to find a better solution.
//
if (request.getMavenSession() != null) {
// User properties have precedence over system properties
putAll(baseProps, request.getMavenSession().getSystemProperties());
putAll(baseProps, request.getMavenSession().getUserProperties());
}
// now we build properties to use for resources interpolation
final Properties filterProperties = new Properties();
File basedir =
request.getMavenProject() != null ? request.getMavenProject().getBasedir() : new File(".");
loadProperties(filterProperties, basedir, request.getFileFilters(), baseProps);
if (filterProperties.isEmpty()) {
putAll(filterProperties, baseProps);
}
if (request.getMavenProject() != null) {
if (request.isInjectProjectBuildFilters()) {
List<String> buildFilters =
new ArrayList<>(request.getMavenProject().getBuild().getFilters());
// JDK-8015656: (coll) unexpected NPE from removeAll
if (request.getFileFilters() != null) {
buildFilters.removeAll(request.getFileFilters());
}
loadProperties(filterProperties, basedir, buildFilters, baseProps);
}
// Project properties
filterProperties.putAll(
request.getMavenProject().getProperties() == null
? Collections.emptyMap()
: request.getMavenProject().getProperties());
}
if (request.getMavenSession() != null) {
// User properties have precedence over system properties
putAll(filterProperties, request.getMavenSession().getSystemProperties());
putAll(filterProperties, request.getMavenSession().getUserProperties());
}
if (request.getAdditionalProperties() != null) {
// additional properties wins
putAll(filterProperties, request.getAdditionalProperties());
}
List<FilterWrapper> defaultFilterWrappers =
new ArrayList<>(request.getDelimiters().size() + 1);
if (getLogger().isDebugEnabled()) {
getLogger().debug("properties used:");
for (String s : new TreeSet<>(filterProperties.stringPropertyNames())) {
getLogger().debug(s + ": " + filterProperties.getProperty(s));
}
}
final ValueSource propertiesValueSource = new PropertiesBasedValueSource(filterProperties);
FilterWrapper wrapper = new Wrapper(
request.getDelimiters(),
request.getMavenProject(),
request.getMavenSession(),
propertiesValueSource,
request.getProjectStartExpressions(),
request.getEscapeString(),
request.isEscapeWindowsPaths(),
request.isSupportMultiLineFiltering());
defaultFilterWrappers.add(wrapper);
return defaultFilterWrappers;
}
and
private static final class Wrapper extends FilterWrapper {
private LinkedHashSet<String> delimiters;
private MavenProject project;
private ValueSource propertiesValueSource;
private List<String> projectStartExpressions;
private String escapeString;
private boolean escapeWindowsPaths;
private final MavenSession mavenSession;
private boolean supportMultiLineFiltering;
Wrapper(
LinkedHashSet<String> delimiters,
MavenProject project,
MavenSession mavenSession,
ValueSource propertiesValueSource,
List<String> projectStartExpressions,
String escapeString,
boolean escapeWindowsPaths,
boolean supportMultiLineFiltering) {
super();
this.delimiters = delimiters;
this.project = project;
this.mavenSession = mavenSession;
this.propertiesValueSource = propertiesValueSource;
this.projectStartExpressions = projectStartExpressions;
this.escapeString = escapeString;
this.escapeWindowsPaths = escapeWindowsPaths;
this.supportMultiLineFiltering = supportMultiLineFiltering;
}
@Override
public Reader getReader(Reader reader) {
Interpolator interpolator = createInterpolator(
delimiters,
projectStartExpressions,
propertiesValueSource,
project,
mavenSession,
escapeString,
escapeWindowsPaths);
MultiDelimiterInterpolatorFilterReaderLineEnding filterReader =
new MultiDelimiterInterpolatorFilterReaderLineEnding(
reader, interpolator, supportMultiLineFiltering);
final RecursionInterceptor ri;
if (projectStartExpressions != null && !projectStartExpressions.isEmpty()) {
ri = new PrefixAwareRecursionInterceptor(projectStartExpressions, true);
} else {
ri = new SimpleRecursionInterceptor();
}
filterReader.setRecursionInterceptor(ri);
filterReader.setDelimiterSpecs(delimiters);
filterReader.setInterpolateWithPrefixPattern(false);
filterReader.setEscapeString(escapeString);
return filterReader;
}
}
private static Interpolator createInterpolator(
LinkedHashSet<String> delimiters,
List<String> projectStartExpressions,
ValueSource propertiesValueSource,
MavenProject project,
MavenSession mavenSession,
String escapeString,
boolean escapeWindowsPaths) {
MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator();
interpolator.setDelimiterSpecs(delimiters);
interpolator.addValueSource(propertiesValueSource);
if (project != null) {
interpolator.addValueSource(new PrefixedObjectValueSource(projectStartExpressions, project, true));
}
if (mavenSession != null) {
interpolator.addValueSource(new PrefixedObjectValueSource("session", mavenSession));
final Settings settings = mavenSession.getSettings();
if (settings != null) {
interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
interpolator.addValueSource(
new SingleResponseValueSource("localRepository", settings.getLocalRepository()));
}
}
interpolator.setEscapeString(escapeString);
if (escapeWindowsPaths) {
interpolator.addPostProcessor((expression, value) ->
(value instanceof String) ? FilteringUtils.escapeWindowsPath((String) value) : value);
}
return interpolator;
}
}
would need to be duplicated for a custom filter). The interpolator customizer allows to leverage the default filterwhile still extending interpolation features of it.

@kwin
Copy link
Member Author

kwin commented Jul 16, 2024

If there is no further input I will merge at the end of the week.

@kwin kwin force-pushed the feature/interpolator-customizer branch from b8318ee to 9abcf84 Compare August 5, 2024 15:21
@kwin kwin merged commit 0880aea into master Aug 5, 2024
18 checks passed
kwin added a commit that referenced this pull request Aug 5, 2024
kwin added a commit that referenced this pull request Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants