View Javadoc
1   package org.apache.maven.plugin.assembly.interpolation;
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.plugin.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugin.assembly.io.DefaultAssemblyReader;
24  import org.apache.maven.plugin.assembly.utils.InterpolationConstants;
25  import org.apache.maven.project.MavenProject;
26  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
28  import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
29  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
30  import org.codehaus.plexus.interpolation.fixed.InterpolationState;
31  
32  import java.io.File;
33  
34  public class AssemblyExpressionEvaluator
35      implements ExpressionEvaluator
36  {
37  
38      private final AssemblerConfigurationSource configSource;
39  
40      private final FixedStringSearchInterpolator interpolator;
41  
42      private final PrefixAwareRecursionInterceptor interceptor;
43  
44      public AssemblyExpressionEvaluator( AssemblerConfigurationSource configSource )
45      {
46          this.configSource = configSource;
47  
48          final MavenProject project = configSource.getProject();
49          final FixedStringSearchInterpolator projectInterpolator =
50              DefaultAssemblyReader.createProjectInterpolator( project );
51          interpolator = AssemblyInterpolator.buildInterpolator( project, projectInterpolator, configSource );
52          interceptor = new PrefixAwareRecursionInterceptor( InterpolationConstants.PROJECT_PREFIXES, true );
53      }
54  
55      public File alignToBaseDirectory( File f )
56      {
57          String basePath = configSource.getBasedir().getAbsolutePath();
58          String path = f.getPath();
59  
60          if ( !f.isAbsolute() && !path.startsWith( basePath ) )
61          {
62              return new File( configSource.getBasedir(), path );
63          }
64          else
65          {
66              return f;
67          }
68      }
69  
70      public Object evaluate( String expression )
71          throws ExpressionEvaluationException
72      {
73          InterpolationState is = new InterpolationState();
74          is.setRecursionInterceptor( interceptor );
75          return interpolator.interpolate( expression, is );
76      }
77  
78  }