1   package org.apache.maven.model.path;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
36  
37  
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  
55  
56  
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  
78  
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 }