1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant.types;
20
21 import org.apache.maven.model.Model;
22 import org.apache.tools.ant.Project;
23 import org.codehaus.plexus.interpolation.reflection.ReflectionValueExtractor;
24
25
26
27 class ModelValueExtractor {
28
29 private static final String PREFIX_PROPERTIES = "properties.";
30
31 private final String prefix;
32
33 private final Project project;
34
35 private final Model model;
36
37 ModelValueExtractor(String prefix, Model model, Project project) {
38 if (model == null) {
39 throw new IllegalArgumentException("reference to Maven POM has not been specified");
40 }
41 if (project == null) {
42 throw new IllegalArgumentException("reference to Ant project has not been specified");
43 }
44 if (prefix == null || prefix.length() <= 0) {
45 prefix = "pom.";
46 } else if (!prefix.endsWith(".")) {
47 prefix += '.';
48 }
49 this.prefix = prefix;
50 this.model = model;
51 this.project = project;
52 }
53
54 public Project getProject() {
55 return project;
56 }
57
58 public boolean isApplicable(String expression) {
59 return expression.startsWith(prefix);
60 }
61
62 public Object getValue(String expression) {
63 if (expression.startsWith(prefix)) {
64 String expr = expression.substring(prefix.length());
65 try {
66 if (expr.startsWith(PREFIX_PROPERTIES)) {
67 String key = expr.substring(PREFIX_PROPERTIES.length());
68 return model.getProperties().getProperty(key);
69 }
70
71 return ReflectionValueExtractor.evaluate(expr, model, false);
72 } catch (Exception e) {
73 project.log("Could not retrieve '" + expression + "' from POM: " + e.getMessage(), e, Project.MSG_WARN);
74 return null;
75 }
76 } else {
77 return null;
78 }
79 }
80 }