View Javadoc
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  import java.util.Map;
23  
24  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
25  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
26  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
27  import org.eclipse.aether.repository.LocalRepository;
28  
29  /**
30   * Stub for {@link ExpressionEvaluator}
31   *
32   * @author jesse
33   */
34  public class ResolverExpressionEvaluatorStub implements TypeAwareExpressionEvaluator {
35  
36      private final Map<String, Object> properties;
37  
38      public ResolverExpressionEvaluatorStub(Map<String, Object> properties) {
39          this.properties = properties;
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public Object evaluate(String expr) throws ExpressionEvaluationException {
45          return evaluate(expr, null);
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
51  
52          Object value = null;
53  
54          if (expr == null) {
55              return null;
56          }
57  
58          String expression = stripTokens(expr);
59  
60          if (expression.equals(expr)) {
61              int index = expr.indexOf("${");
62              if (index >= 0) {
63                  int lastIndex = expr.indexOf("}", index);
64                  if (lastIndex >= 0) {
65                      String retVal = expr.substring(0, index);
66  
67                      if (index > 0 && expr.charAt(index - 1) == '$') {
68                          retVal += expr.substring(index + 1, lastIndex + 1);
69                      } else {
70                          retVal += evaluate(expr.substring(index, lastIndex + 1));
71                      }
72  
73                      retVal += evaluate(expr.substring(lastIndex + 1));
74                      return retVal;
75                  }
76              }
77  
78              // Was not an expression
79              return expression.contains("$$") ? expression.replaceAll("\\$\\$", "\\$") : expression;
80          } else {
81              if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
82                  value = MojoExtension.getBasedir();
83              } else if (expression.startsWith("basedir") || expression.startsWith("project.basedir")) {
84                  int pathSeparator = expression.indexOf("/");
85                  if (pathSeparator > 0) {
86                      value = MojoExtension.getBasedir() + expression.substring(pathSeparator);
87                  }
88              } else if ("localRepository".equals(expression)) {
89                  File localRepo = new File(MojoExtension.getBasedir(), "target/local-repo");
90                  return new LocalRepository("file://" + localRepo.getAbsolutePath());
91              }
92              if (value == null && properties != null && properties.containsKey(expression)) {
93                  value = properties.get(expression);
94              }
95              return value;
96          }
97      }
98  
99      private String stripTokens(String expr) {
100         if (expr.startsWith("${") && expr.indexOf("}") == expr.length() - 1) {
101             expr = expr.substring(2, expr.length() - 1);
102         }
103 
104         return expr;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public File alignToBaseDirectory(File file) {
110         if (file.getAbsolutePath().startsWith(MojoExtension.getBasedir())) {
111             return file;
112         } else if (file.isAbsolute()) {
113             return file;
114         } else {
115             return new File(MojoExtension.getBasedir(), file.getPath());
116         }
117     }
118 }