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