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