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   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
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 }