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