1 package org.alfresco.maven.plugin.amp.util;
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.artifact.Artifact;
23 import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
24 import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
25 import org.codehaus.plexus.util.interpolation.ValueSource;
26
27 import java.util.Properties;
28
29 /***
30 * Utilities used to evaluate expression.
31 * <p/>
32 * TODO: this comes from the assembly plugin; refactor when it's shared.
33 * <p/>
34 * The expression might use any fied of the {@link Artifact} interface. Some
35 * examples might be:
36 * <ul>
37 * <li>${artifactId}-${version}.${extension}</li>
38 * <li>${artifactId}.${extension}</li>
39 * </ul>
40 *
41 * @author Stephane Nicoll
42 */
43 public class MappingUtils
44 {
45
46 /***
47 * Evaluates the specified expression for the given artifact.
48 *
49 * @param expression the expression to evaluate
50 * @param artifact the artifact to use as value object for tokens
51 * @return expression the evaluated expression
52 */
53 public static String evaluateFileNameMapping( String expression, Artifact artifact )
54
55 {
56 String value = expression;
57
58
59 artifact.isSnapshot();
60
61 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
62
63 interpolator.addValueSource( new ObjectBasedValueSource( artifact ) );
64 interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) );
65
66 Properties classifierMask = new Properties();
67 classifierMask.setProperty( "classifier", "" );
68
69 interpolator.addValueSource( new PropertiesInterpolationValueSource( classifierMask ) );
70
71 value = interpolator.interpolate( value, "__artifact" );
72
73 return value;
74 }
75
76
77 static class PropertiesInterpolationValueSource
78 implements ValueSource
79 {
80
81 private final Properties properties;
82
83 public PropertiesInterpolationValueSource( Properties properties )
84 {
85 this.properties = properties;
86 }
87
88 public Object getValue( String key )
89 {
90 return properties.getProperty( key );
91 }
92
93 }
94
95 }