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.path;
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  
28  import org.apache.maven.api.model.ActivationFile;
29  import org.apache.maven.model.profile.ProfileActivationContext;
30  import org.apache.maven.model.root.RootLocator;
31  import org.codehaus.plexus.interpolation.AbstractValueSource;
32  import org.codehaus.plexus.interpolation.InterpolationException;
33  import org.codehaus.plexus.interpolation.MapBasedValueSource;
34  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
35  
36  /**
37   * Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
38   *
39   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
40   */
41  @Named
42  @Singleton
43  @Deprecated
44  public class ProfileActivationFilePathInterpolator {
45  
46      private final PathTranslator pathTranslator;
47  
48      private final RootLocator rootLocator;
49  
50      @Inject
51      public ProfileActivationFilePathInterpolator(PathTranslator pathTranslator, RootLocator rootLocator) {
52          this.pathTranslator = pathTranslator;
53          this.rootLocator = rootLocator;
54      }
55  
56      /**
57       * Interpolates given {@code path}.
58       *
59       * @return absolute path or {@code null} if the input was {@code null}
60       */
61      public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
62          if (path == null) {
63              return null;
64          }
65  
66          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
67  
68          final File basedir = context.getProjectDirectory();
69  
70          if (basedir != null) {
71              interpolator.addValueSource(new AbstractValueSource(false) {
72                  @Override
73                  public Object getValue(String expression) {
74                      if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
75                          return basedir.getAbsolutePath();
76                      }
77                      return null;
78                  }
79              });
80          } else if (path.contains("${basedir}")) {
81              return null;
82          }
83  
84          interpolator.addValueSource(new AbstractValueSource(false) {
85              @Override
86              public Object getValue(String expression) {
87                  if ("project.rootDirectory".equals(expression)) {
88                      Path base = basedir != null ? basedir.toPath() : null;
89                      Path root = rootLocator.findMandatoryRoot(base);
90                      return root.toFile().getAbsolutePath();
91                  }
92                  return null;
93              }
94          });
95  
96          interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
97  
98          interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
99  
100         interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
101 
102         String absolutePath = interpolator.interpolate(path, "");
103 
104         return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
105     }
106 }