1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.interpolation;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26 import java.nio.file.Path;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.model.building.ModelBuildingRequest;
33 import org.apache.maven.model.building.ModelProblem.Severity;
34 import org.apache.maven.model.building.ModelProblem.Version;
35 import org.apache.maven.model.building.ModelProblemCollector;
36 import org.apache.maven.model.building.ModelProblemCollectorRequest;
37 import org.apache.maven.model.path.PathTranslator;
38 import org.apache.maven.model.path.UrlNormalizer;
39 import org.apache.maven.model.root.RootLocator;
40 import org.apache.maven.model.v4.MavenTransformer;
41 import org.codehaus.plexus.interpolation.InterpolationException;
42 import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
43 import org.codehaus.plexus.interpolation.RecursionInterceptor;
44 import org.codehaus.plexus.interpolation.StringSearchInterpolator;
45 import org.codehaus.plexus.interpolation.ValueSource;
46
47
48
49
50
51
52 @Named
53 @Singleton
54 public class StringVisitorModelInterpolator extends AbstractStringBasedModelInterpolator {
55 @Inject
56 public StringVisitorModelInterpolator(
57 PathTranslator pathTranslator, UrlNormalizer urlNormalizer, RootLocator rootLocator) {
58 super(pathTranslator, urlNormalizer, rootLocator);
59 }
60
61 interface InnerInterpolator {
62 String interpolate(String value);
63 }
64
65 @Deprecated
66 @Override
67 public Model interpolateModel(
68 Model model, File projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
69 return interpolateModel(model, projectDir != null ? projectDir.toPath() : null, config, problems);
70 }
71
72 @Override
73 public Model interpolateModel(
74 Model model, Path projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
75 List<? extends ValueSource> valueSources = createValueSources(model, projectDir, config, problems);
76 List<? extends InterpolationPostProcessor> postProcessors = createPostProcessors(model, projectDir, config);
77
78 InnerInterpolator innerInterpolator = createInterpolator(valueSources, postProcessors, problems, config);
79
80 return new MavenTransformer(innerInterpolator::interpolate).visit(model);
81 }
82
83 private InnerInterpolator createInterpolator(
84 List<? extends ValueSource> valueSources,
85 List<? extends InterpolationPostProcessor> postProcessors,
86 final ModelProblemCollector problems,
87 ModelBuildingRequest config) {
88 final Map<String, String> cache = new HashMap<>();
89 final StringSearchInterpolator interpolator = new StringSearchInterpolator();
90 interpolator.setCacheAnswers(true);
91 for (ValueSource vs : valueSources) {
92 interpolator.addValueSource(vs);
93 }
94 for (InterpolationPostProcessor postProcessor : postProcessors) {
95 interpolator.addPostProcessor(postProcessor);
96 }
97 final RecursionInterceptor recursionInterceptor = createRecursionInterceptor(config);
98 return value -> {
99 if (value != null && value.contains("${")) {
100 String c = cache.get(value);
101 if (c == null) {
102 try {
103 c = interpolator.interpolate(value, recursionInterceptor);
104 } catch (InterpolationException e) {
105 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
106 .setMessage(e.getMessage())
107 .setException(e));
108 }
109 cache.put(value, c);
110 }
111 return c;
112 }
113 return value;
114 };
115 }
116 }