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 com.google.common.base.Preconditions.checkState;
19 import static java.lang.String.format;
20
21 import org.basepom.mojo.propertyhelper.beans.MacroDefinition;
22 import org.basepom.mojo.propertyhelper.macros.MacroType;
23
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Optional;
27
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableList.Builder;
30 import org.apache.maven.plugin.MojoExecutionException;
31
32 public class MacroField
33 implements PropertyElement {
34
35 private final MacroDefinition macroDefinition;
36 private final ValueProvider valueProvider;
37 private final AbstractPropertyHelperMojo mojo;
38
39 public MacroField(final MacroDefinition macroDefinition,
40 final ValueProvider valueProvider,
41 final AbstractPropertyHelperMojo mojo) {
42 this.macroDefinition = macroDefinition;
43 this.valueProvider = valueProvider;
44 this.mojo = mojo;
45 }
46
47 public static List<MacroField> createMacros(final ValueCache valueCache,
48 final MacroDefinition[] macroDefinitions,
49 final AbstractPropertyHelperMojo mojo)
50 throws IOException {
51 checkNotNull(valueCache, "valueCache is null");
52 checkNotNull(macroDefinitions, "macroDefinitions is null");
53 checkNotNull(mojo, "mojo is null");
54
55 final Builder<MacroField> result = ImmutableList.builder();
56
57 for (MacroDefinition macroDefinition : macroDefinitions) {
58 macroDefinition.check();
59 final ValueProvider macroValue = valueCache.getValueProvider(macroDefinition);
60 final MacroField macroField = new MacroField(macroDefinition, macroValue, mojo);
61 result.add(macroField);
62 }
63
64 return result.build();
65 }
66
67 @Override
68 public String getPropertyName() {
69 return macroDefinition.getId();
70 }
71
72 @Override
73 public Optional<String> getPropertyValue() throws MojoExecutionException {
74 final Optional<String> type = macroDefinition.getMacroType();
75 final MacroType macroType;
76
77 try {
78 if (type.isPresent()) {
79 macroType = mojo.getMacros().get(type.get());
80 checkState(macroType != null, "Could not locate macro '%s'", type.get());
81 } else {
82 final Optional<String> macroClassName = macroDefinition.getMacroClass();
83 checkState(macroClassName.isPresent(), "No definition for macro '%s' found!", macroDefinition.getId());
84 final Class<?> macroClass = Class.forName(macroClassName.get());
85 macroType = (MacroType) macroClass.getDeclaredConstructor().newInstance();
86 }
87
88 Optional<String> result = macroType.getValue(macroDefinition, valueProvider, mojo);
89 if (result.isPresent()) {
90 return macroDefinition.formatResult(result.get());
91 }
92 return result;
93 } catch (ReflectiveOperationException e) {
94 throw new MojoExecutionException(format("Could not instantiate '%s'", macroDefinition), e);
95 }
96 }
97
98 @Override
99 public boolean isExport() {
100 return macroDefinition.isExport();
101 }
102
103 @Override
104 public String toString() {
105 try {
106 return getPropertyValue().orElse("");
107 } catch (Exception e) {
108 return "<unset>";
109 }
110 }
111 }