1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.testing;
20
21 import java.io.File;
22
23 import org.apache.maven.artifact.repository.MavenArtifactRepository;
24 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
25 import org.codehaus.plexus.PlexusTestCase;
26 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
27 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
28
29
30
31
32
33
34 public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator {
35
36 @Override
37 public Object evaluate(String expr) throws ExpressionEvaluationException {
38
39 Object value = null;
40
41 if (expr == null) {
42 return null;
43 }
44
45 String expression = stripTokens(expr);
46
47 if (expression.equals(expr)) {
48 int index = expr.indexOf("${");
49 if (index >= 0) {
50 int lastIndex = expr.indexOf("}", index);
51 if (lastIndex >= 0) {
52 String retVal = expr.substring(0, index);
53
54 if (index > 0 && expr.charAt(index - 1) == '$') {
55 retVal += expr.substring(index + 1, lastIndex + 1);
56 } else {
57 retVal += evaluate(expr.substring(index, lastIndex + 1));
58 }
59
60 retVal += evaluate(expr.substring(lastIndex + 1));
61 return retVal;
62 }
63 }
64
65
66 if (expression.indexOf("$$") > -1) {
67 return expression.replaceAll("\\$\\$", "\\$");
68 }
69 }
70
71 if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
72 return PlexusTestCase.getBasedir();
73 } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
74 int pathSeparator = expression.indexOf("/");
75
76 if (pathSeparator > 0) {
77 value = PlexusTestCase.getBasedir() + expression.substring(pathSeparator);
78 } else {
79 System.out.println("Got expression '" + expression + "' that was not recognised");
80 }
81 return value;
82 } else if ("localRepository".equals(expression)) {
83 File localRepo = new File(PlexusTestCase.getBasedir(), "target/local-repo");
84 return new MavenArtifactRepository(
85 "localRepository",
86 "file://" + localRepo.getAbsolutePath(),
87 new DefaultRepositoryLayout(),
88 null,
89 null);
90 } else {
91 return expr;
92 }
93 }
94
95 private String stripTokens(String expr) {
96 if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
97 expr = expr.substring(2, expr.length() - 1);
98 }
99
100 return expr;
101 }
102
103
104 @Override
105 public File alignToBaseDirectory(File file) {
106 if (file.getAbsolutePath().startsWith(PlexusTestCase.getBasedir())) {
107 return file;
108 } else if (file.isAbsolute()) {
109 return file;
110 } else {
111 return new File(PlexusTestCase.getBasedir(), file.getPath());
112 }
113 }
114 }