|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.api.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.component.configurator.expression.ExpressionEvaluationException; |
| 26 | +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; |
| 27 | + |
| 28 | +/** |
| 29 | + * Stub for {@link ExpressionEvaluator} |
| 30 | + * |
| 31 | + * @author jesse |
| 32 | + */ |
| 33 | +public class ResolverExpressionEvaluatorStub implements ExpressionEvaluator { |
| 34 | + /** {@inheritDoc} */ |
| 35 | + @Override |
| 36 | + public Object evaluate(String expr) throws ExpressionEvaluationException { |
| 37 | + |
| 38 | + Object value = null; |
| 39 | + |
| 40 | + if (expr == null) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + String expression = stripTokens(expr); |
| 45 | + |
| 46 | + if (expression.equals(expr)) { |
| 47 | + int index = expr.indexOf("${"); |
| 48 | + if (index >= 0) { |
| 49 | + int lastIndex = expr.indexOf("}", index); |
| 50 | + if (lastIndex >= 0) { |
| 51 | + String retVal = expr.substring(0, index); |
| 52 | + |
| 53 | + if (index > 0 && expr.charAt(index - 1) == '$') { |
| 54 | + retVal += expr.substring(index + 1, lastIndex + 1); |
| 55 | + } else { |
| 56 | + retVal += evaluate(expr.substring(index, lastIndex + 1)); |
| 57 | + } |
| 58 | + |
| 59 | + retVal += evaluate(expr.substring(lastIndex + 1)); |
| 60 | + return retVal; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Was not an expression |
| 65 | + if (expression.indexOf("$$") > -1) { |
| 66 | + return expression.replaceAll("\\$\\$", "\\$"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if ("basedir".equals(expression) || "project.basedir".equals(expression)) { |
| 71 | + return MojoExtension.getBasedir(); |
| 72 | + } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) { |
| 73 | + int pathSeparator = expression.indexOf("/"); |
| 74 | + |
| 75 | + if (pathSeparator > 0) { |
| 76 | + value = MojoExtension.getBasedir() + expression.substring(pathSeparator); |
| 77 | + } else { |
| 78 | + System.out.println("Got expression '" + expression + "' that was not recognised"); |
| 79 | + } |
| 80 | + return value; |
| 81 | + } else if ("localRepository".equals(expression)) { |
| 82 | + File localRepo = new File(MojoExtension.getBasedir(), "target/local-repo"); |
| 83 | + return new MavenArtifactRepository( |
| 84 | + "localRepository", |
| 85 | + "file://" + localRepo.getAbsolutePath(), |
| 86 | + new DefaultRepositoryLayout(), |
| 87 | + null, |
| 88 | + null); |
| 89 | + } else { |
| 90 | + return expr; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private String stripTokens(String expr) { |
| 95 | + if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) { |
| 96 | + expr = expr.substring(2, expr.length() - 1); |
| 97 | + } |
| 98 | + |
| 99 | + return expr; |
| 100 | + } |
| 101 | + |
| 102 | + /** {@inheritDoc} */ |
| 103 | + @Override |
| 104 | + public File alignToBaseDirectory(File file) { |
| 105 | + if (file.getAbsolutePath().startsWith(MojoExtension.getBasedir())) { |
| 106 | + return file; |
| 107 | + } else if (file.isAbsolute()) { |
| 108 | + return file; |
| 109 | + } else { |
| 110 | + return new File(MojoExtension.getBasedir(), file.getPath()); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments