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