View Javadoc
1   package org.apache.maven.model.path;
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 org.apache.maven.api.model.ActivationFile;
23  import org.apache.maven.model.profile.ProfileActivationContext;
24  import org.codehaus.plexus.interpolation.AbstractValueSource;
25  import org.codehaus.plexus.interpolation.InterpolationException;
26  import org.codehaus.plexus.interpolation.MapBasedValueSource;
27  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
28  
29  import javax.inject.Inject;
30  import javax.inject.Named;
31  import javax.inject.Singleton;
32  import java.io.File;
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  
44      private final PathTranslator pathTranslator;
45  
46      @Inject
47      public ProfileActivationFilePathInterpolator( PathTranslator pathTranslator )
48      {
49          this.pathTranslator = pathTranslator;
50      }
51  
52      /**
53       * Interpolates given {@code path}.
54       *
55       * @return absolute path or {@code null} if the input was {@code null}
56       */
57      public String interpolate( String path, ProfileActivationContext context ) throws InterpolationException
58      {
59          if ( path == null )
60          {
61              return null;
62          }
63  
64          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
65  
66          final File basedir = context.getProjectDirectory();
67  
68          if ( basedir != null )
69          {
70              interpolator.addValueSource( new AbstractValueSource( false )
71              {
72                  @Override
73                  public Object getValue( String expression )
74                  {
75                      /*
76                       * We intentionally only support ${basedir} and not ${project.basedir} as the latter form
77                       * would suggest that other project.* expressions can be used which is beyond the design.
78                       */
79                      if ( "basedir".equals( expression ) )
80                      {
81                          return basedir.getAbsolutePath();
82                      }
83                      return null;
84                  }
85              } );
86          }
87          else if ( path.contains( "${basedir}" ) )
88          {
89              return null;
90          }
91  
92          interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );
93  
94          interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
95  
96          interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
97  
98          String absolutePath = interpolator.interpolate( path, "" );
99  
100         return pathTranslator.alignToBaseDirectory( absolutePath, basedir );
101     }
102 }