View Javadoc
1   package org.apache.maven.plugin.testing;
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 java.io.File;
23  
24  import org.codehaus.plexus.PlexusTestCase;
25  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
26  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
27  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
28  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
29  
30  /**
31   * Stub for {@link ExpressionEvaluator}
32   *
33   * @author jesse
34   * @version $Id$
35   */
36  public class ResolverExpressionEvaluatorStub
37      implements ExpressionEvaluator
38  {
39      /** {@inheritDoc} */
40      public Object evaluate( String expr )
41          throws ExpressionEvaluationException
42      {
43  
44          Object value = null;
45  
46          if ( expr == null )
47          {
48              return null;
49          }
50  
51          String expression = stripTokens( expr );
52  
53          if ( expression.equals( expr ) )
54          {
55              int index = expr.indexOf( "${" );
56              if ( index >= 0 )
57              {
58                  int lastIndex = expr.indexOf( "}", index );
59                  if ( lastIndex >= 0 )
60                  {
61                      String retVal = expr.substring( 0, index );
62  
63                      if ( index > 0 && expr.charAt( index - 1 ) == '$' )
64                      {
65                          retVal += expr.substring( index + 1, lastIndex + 1 );
66                      }
67                      else
68                      {
69                          retVal += evaluate( expr.substring( index, lastIndex + 1 ) );
70                      }
71  
72                      retVal += evaluate( expr.substring( lastIndex + 1 ) );
73                      return retVal;
74                  }
75              }
76  
77              // Was not an expression
78              if ( expression.indexOf( "$$" ) > -1 )
79              {
80                  return expression.replaceAll( "\\$\\$", "\\$" );
81              }
82          }
83  
84          if ( "basedir".equals( expression ) )
85          {
86              return PlexusTestCase.getBasedir();
87          }
88          else if ( expression.startsWith( "basedir" ) )
89          {
90              int pathSeparator = expression.indexOf( "/" );
91  
92              if ( pathSeparator > 0 )
93              {
94                  value = PlexusTestCase.getBasedir() + expression.substring( pathSeparator );
95              }
96              else
97              {
98                  System.out.println( "Got expression '" + expression + "' that was not recognised" );
99              }
100             return value;
101         }
102         else if ( "localRepository".equals( expression ) )
103         {
104             File localRepo = new File( PlexusTestCase.getBasedir(), "target/local-repo" );
105             return new DefaultArtifactRepository( "localRepository", "file://" + localRepo.getAbsolutePath(),
106                                                   new DefaultRepositoryLayout() );
107         }
108         else
109         {
110             return expr;
111         }
112     }
113 
114     private String stripTokens( String expr )
115     {
116         if ( expr.startsWith( "${" ) && expr.indexOf( "}" ) == expr.length() - 1 )
117         {
118             expr = expr.substring( 2, expr.length() - 1 );
119         }
120 
121         return expr;
122     }
123 
124     /** {@inheritDoc} */
125     public File alignToBaseDirectory( File file )
126     {
127         if ( file.getAbsolutePath().startsWith( PlexusTestCase.getBasedir() ) )
128         {
129             return file;
130         }
131         else if ( file.isAbsolute() )
132         {
133             return file;
134         }
135         else
136         {
137             return new File( PlexusTestCase.getBasedir(), file.getPath() );
138         }
139     }
140 }