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