1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.propertyhelper;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18 import static java.lang.String.format;
19
20 import org.basepom.mojo.propertyhelper.beans.PropertyGroup;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableList.Builder;
29 import org.apache.maven.model.Model;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.codehaus.plexus.interpolation.InterpolationException;
32
33 public class PropertyField
34 implements PropertyElement {
35
36 private final String propertyName;
37 private final String propertyValue;
38
39 PropertyField(final String propertyName, final String propertyValue) {
40 this.propertyName = checkNotNull(propertyName, "propertyName is null");
41 this.propertyValue = checkNotNull(propertyValue, "propertyValue is null");
42 }
43
44 public static List<PropertyElement> createProperties(final Model model, final Map<String, String> values, final PropertyGroup propertyGroup)
45 throws MojoExecutionException, IOException {
46 checkNotNull(model, "model is null");
47 checkNotNull(values, "values is null");
48 checkNotNull(propertyGroup, "propertyGroup is null");
49
50 final InterpolatorFactory interpolatorFactory = new InterpolatorFactory(model);
51
52 final Builder<PropertyElement> result = ImmutableList.builder();
53 final Map<String, String> properties = propertyGroup.getProperties();
54
55 for (String name : properties.keySet()) {
56 try {
57 final String value = propertyGroup.getPropertyValue(interpolatorFactory, name, values);
58 result.add(new PropertyField(name, value));
59 } catch (InterpolationException e) {
60 throw new MojoExecutionException(format("Could not interpolate '%s", model), e);
61 }
62 }
63 return result.build();
64 }
65
66 @Override
67 public String getPropertyName() {
68 return propertyName;
69 }
70
71 @Override
72 public Optional<String> getPropertyValue() {
73 return Optional.of(propertyValue);
74 }
75
76 @Override
77 public boolean isExport() {
78 return true;
79 }
80 }