View Javadoc

1   package org.apache.maven.plugin.testing;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  
21  import org.codehaus.plexus.PlexusTestCase;
22  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
23  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
24  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
25  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
26  
27  /**
28   * StubResolverExpressionEvaluator:
29   *
30   * @author: jesse
31   * @date: Feb 24, 2006
32   * @version: $Id: ResolverExpressionEvaluatorStub.java 391561 2006-04-05 08:36:24Z epunzalan $
33   */
34  public class ResolverExpressionEvaluatorStub
35      implements ExpressionEvaluator
36  {
37  
38      public Object evaluate( String expr )
39          throws ExpressionEvaluationException
40      {
41  
42          Object value = null;
43  
44          if ( expr == null )
45          {
46              return null;
47          }
48  
49          String expression = stripTokens( expr );
50  
51          if ( expression.equals( expr ) )
52          {
53              int index = expr.indexOf( "${" );
54              if ( index >= 0 )
55              {
56                  int lastIndex = expr.indexOf( "}", index );
57                  if ( lastIndex >= 0 )
58                  {
59                      String retVal = expr.substring( 0, index );
60  
61                      if ( index > 0 && expr.charAt( index - 1 ) == '$' )
62                      {
63                          retVal += expr.substring( index + 1, lastIndex + 1 );
64                      }
65                      else
66                      {
67                          retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
68                      }
69  
70                      retVal += evaluate( expr.substring( lastIndex + 1 ) );
71                      return retVal;
72                  }
73              }
74  
75              // Was not an expression
76              if ( expression.indexOf( "$$" ) > -1 )
77              {
78                  return expression.replaceAll( "\\$\\$", "\\$" );
79              }
80          }
81  
82          if ( "basedir".equals( expression ) )
83          {
84              return PlexusTestCase.getBasedir();
85          }
86          else if ( expression.startsWith( "basedir" ) )
87          {
88              int pathSeparator = expression.indexOf( "/" );
89  
90              if ( pathSeparator > 0 )
91              {
92                  value = PlexusTestCase.getBasedir() + expression.substring( pathSeparator );
93              }
94              else
95              {
96                  System.out.println( "Got expression '" + expression + "' that was not recognised" );
97              }
98              return value;
99          }
100         else if ( "localRepository".equals( expression ) )
101         {
102             File localRepo = new File( PlexusTestCase.getBasedir(), "target/local-repo" );
103             return new DefaultArtifactRepository( "localRepository", "file://" + localRepo.getAbsolutePath(),
104                                                   new DefaultRepositoryLayout() );
105         }
106         else
107         {
108             return expr;
109         }
110     }
111 
112     private String stripTokens( String expr )
113     {
114         if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
115         {
116             expr = expr.substring( 2, expr.length() - 1 );
117         }
118 
119         return expr;
120     }
121 
122     public File alignToBaseDirectory( File file )
123     {
124         if ( file.getAbsolutePath().startsWith( PlexusTestCase.getBasedir() ) )
125         {
126             return file;
127         }
128         else
129         {
130             return new File( PlexusTestCase.getBasedir() + File.pathSeparator + file.getPath() );
131         }
132     }
133 }