1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.plugin;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.maven.api.MojoExecution;
28  import org.apache.maven.api.Project;
29  import org.apache.maven.api.Session;
30  import org.apache.maven.internal.impl.model.reflection.ReflectionValueExtractor;
31  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
32  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  public class PluginParameterExpressionEvaluatorV4 implements TypeAwareExpressionEvaluator {
55      private final Session session;
56  
57      private final MojoExecution mojoExecution;
58  
59      private final Project project;
60  
61      private final Path basedir;
62  
63      private final Map<String, String> properties;
64  
65      public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
66          this(session, project, null);
67      }
68  
69      public PluginParameterExpressionEvaluatorV4(Session session, Project project, MojoExecution mojoExecution) {
70          this.session = session;
71          this.mojoExecution = mojoExecution;
72          this.properties = session.getEffectiveProperties(project);
73          this.project = project;
74  
75          Path basedir = null;
76  
77          if (project != null) {
78              Path projectFile = project.getBasedir();
79              basedir = projectFile.toAbsolutePath();
80          }
81  
82          if (basedir == null) {
83              basedir = session.getTopDirectory();
84          }
85  
86          if (basedir == null) {
87              basedir = Paths.get(System.getProperty("user.dir"));
88          }
89  
90          this.basedir = basedir;
91      }
92  
93      @Override
94      public Object evaluate(String expr) throws ExpressionEvaluationException {
95          return evaluate(expr, null);
96      }
97  
98      @Override
99      @SuppressWarnings("checkstyle:methodlength")
100     public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException {
101         Object value = null;
102 
103         if (expr == null) {
104             return null;
105         }
106 
107         String expression = stripTokens(expr);
108         if (expression.equals(expr)) {
109             int index = expr.indexOf("${");
110             if (index >= 0) {
111                 int lastIndex = expr.indexOf('}', index);
112                 if (lastIndex >= 0) {
113                     String retVal = expr.substring(0, index);
114 
115                     if ((index > 0) && (expr.charAt(index - 1) == '$')) {
116                         retVal += expr.substring(index + 1, lastIndex + 1);
117                     } else {
118                         Object subResult = evaluate(expr.substring(index, lastIndex + 1));
119 
120                         if (subResult != null) {
121                             retVal += subResult;
122                         } else {
123                             retVal += "$" + expr.substring(index + 1, lastIndex + 1);
124                         }
125                     }
126 
127                     retVal += evaluate(expr.substring(lastIndex + 1));
128                     return retVal;
129                 }
130             }
131 
132             
133             return expression.replace("$$", "$");
134         }
135 
136         Map<String, Object> objects = new HashMap<>();
137         objects.put("session.", session);
138         objects.put("project.", project);
139         objects.put("mojo.", mojoExecution);
140         objects.put("settings.", session.getSettings());
141         for (Map.Entry<String, Object> ctx : objects.entrySet()) {
142             if (expression.startsWith(ctx.getKey())) {
143                 try {
144                     int pathSeparator = expression.indexOf('/');
145                     if (pathSeparator > 0) {
146                         String pathExpression = expression.substring(0, pathSeparator);
147                         value = ReflectionValueExtractor.evaluate(pathExpression, ctx.getValue());
148                         if (pathSeparator < expression.length() - 1) {
149                             if (value instanceof Path) {
150                                 value = ((Path) value).resolve(expression.substring(pathSeparator + 1));
151                             } else {
152                                 value = value + expression.substring(pathSeparator);
153                             }
154                         }
155                     } else {
156                         value = ReflectionValueExtractor.evaluate(expression, ctx.getValue());
157                     }
158                     break;
159                 } catch (Exception e) {
160                     
161                     throw new ExpressionEvaluationException(
162                             "Error evaluating plugin parameter expression: " + expression, e);
163                 }
164             }
165         }
166 
167         
168 
169 
170 
171 
172 
173 
174 
175         if (value != null && type != null && !(value instanceof String) && !isTypeCompatible(type, value)) {
176             value = null;
177         }
178 
179         if (value == null) {
180             
181 
182             if (properties != null) {
183                 
184                 
185                 
186                 
187 
188                 value = properties.get(expression);
189             }
190         }
191 
192         if (value instanceof String) {
193             
194 
195             String val = (String) value;
196 
197             int exprStartDelimiter = val.indexOf("${");
198 
199             if (exprStartDelimiter >= 0) {
200                 if (exprStartDelimiter > 0) {
201                     value = val.substring(0, exprStartDelimiter) + evaluate(val.substring(exprStartDelimiter));
202                 } else {
203                     value = evaluate(val.substring(exprStartDelimiter));
204                 }
205             }
206         }
207 
208         return value;
209     }
210 
211     private static boolean isTypeCompatible(Class<?> type, Object value) {
212         if (type.isInstance(value)) {
213             return true;
214         }
215         
216         return ((type.isPrimitive() || type.getName().startsWith("java.lang."))
217                 && value.getClass().getName().startsWith("java.lang."));
218     }
219 
220     private String stripTokens(String expr) {
221         if (expr.startsWith("${") && (expr.indexOf('}') == expr.length() - 1)) {
222             expr = expr.substring(2, expr.length() - 1);
223         }
224         return expr;
225     }
226 
227     @Override
228     public File alignToBaseDirectory(File file) {
229         
230         
231         if (file != null) {
232             if (file.isAbsolute()) {
233                 
234             } else if (file.getPath().startsWith(File.separator)) {
235                 
236                 file = file.getAbsoluteFile();
237             } else {
238                 
239                 file = basedir.resolve(file.getPath())
240                         .normalize()
241                         .toAbsolutePath()
242                         .toFile();
243             }
244         }
245         return file;
246     }
247 }