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  
27  import org.apache.maven.model.ActivationFile;
28  import org.apache.maven.model.profile.ProfileActivationContext;
29  import org.codehaus.plexus.interpolation.AbstractValueSource;
30  import org.codehaus.plexus.interpolation.InterpolationException;
31  import org.codehaus.plexus.interpolation.MapBasedValueSource;
32  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
33  
34  /**
35   * Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
36   *
37   * @author Ravil Galeyev
38   */
39  @Named
40  @Singleton
41  public class ProfileActivationFilePathInterpolator {
42  
43      @Inject
44      private PathTranslator pathTranslator;
45  
46      public ProfileActivationFilePathInterpolator setPathTranslator(PathTranslator pathTranslator) {
47          this.pathTranslator = pathTranslator;
48          return this;
49      }
50  
51      /**
52       * Interpolates given {@code path}.
53       *
54       * @return absolute path or {@code null} if the input was {@code null}
55       */
56      public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
57          if (path == null) {
58              return null;
59          }
60  
61          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
62  
63          final File basedir = context.getProjectDirectory();
64  
65          if (basedir != null) {
66              interpolator.addValueSource(new AbstractValueSource(false) {
67                  @Override
68                  public Object getValue(String expression) {
69                      /*
70                       * We intentionally only support ${basedir} and not ${project.basedir} as the latter form
71                       * would suggest that other project.* expressions can be used which is beyond the design.
72                       */
73                      if ("basedir".equals(expression)) {
74                          return basedir.getAbsolutePath();
75                      }
76                      return null;
77                  }
78              });
79          } else if (path.contains("${basedir}")) {
80              return null;
81          }
82  
83          interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
84  
85          interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
86  
87          interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
88  
89          String absolutePath = interpolator.interpolate(path, "");
90  
91          return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
92      }
93  }