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