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