View Javadoc
1   package org.apache.maven.model.interpolation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * StringVisitorModelInterpolator
48   *
49   * @since 3.6.2
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 }