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
53 @Named
54 @Singleton
55 @Deprecated(since = "4.0.0")
56 public class StringVisitorModelInterpolator extends AbstractStringBasedModelInterpolator {
57 @Inject
58 public StringVisitorModelInterpolator(
59 PathTranslator pathTranslator, UrlNormalizer urlNormalizer, RootLocator rootLocator) {
60 super(pathTranslator, urlNormalizer, rootLocator);
61 }
62
63 interface InnerInterpolator {
64 String interpolate(String value);
65 }
66
67 @Deprecated
68 @Override
69 public Model interpolateModel(
70 Model model, File projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
71 return interpolateModel(model, projectDir != null ? projectDir.toPath() : null, config, problems);
72 }
73
74 @Override
75 public Model interpolateModel(
76 Model model, Path projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
77 List<? extends ValueSource> valueSources = createValueSources(model, projectDir, config, problems);
78 List<? extends InterpolationPostProcessor> postProcessors = createPostProcessors(model, projectDir, config);
79
80 InnerInterpolator innerInterpolator = createInterpolator(valueSources, postProcessors, problems, config);
81
82 return new MavenTransformer(innerInterpolator::interpolate).visit(model);
83 }
84
85 private InnerInterpolator createInterpolator(
86 List<? extends ValueSource> valueSources,
87 List<? extends InterpolationPostProcessor> postProcessors,
88 final ModelProblemCollector problems,
89 ModelBuildingRequest config) {
90 final Map<String, String> cache = new HashMap<>();
91 final StringSearchInterpolator interpolator = new StringSearchInterpolator();
92 interpolator.setCacheAnswers(true);
93 for (ValueSource vs : valueSources) {
94 interpolator.addValueSource(vs);
95 }
96 for (InterpolationPostProcessor postProcessor : postProcessors) {
97 interpolator.addPostProcessor(postProcessor);
98 }
99 final RecursionInterceptor recursionInterceptor = createRecursionInterceptor(config);
100 return value -> {
101 if (value != null && value.contains("${")) {
102 String c = cache.get(value);
103 if (c == null) {
104 try {
105 c = interpolator.interpolate(value, recursionInterceptor);
106 } catch (InterpolationException e) {
107 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
108 .setMessage(e.getMessage())
109 .setException(e));
110 }
111 cache.put(value, c);
112 }
113 return c;
114 }
115 return value;
116 };
117 }
118 }