1 package org.apache.maven.plugin.testing;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }