View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.beans;
16  
17  import static com.google.common.base.Functions.identity;
18  import static com.google.common.base.Preconditions.checkNotNull;
19  import static com.google.common.collect.ImmutableMap.toImmutableMap;
20  import static com.google.common.collect.ImmutableSet.toImmutableSet;
21  import static org.basepom.mojo.propertyhelper.beans.PropertyDefinition.getNameFunction;
22  import static org.basepom.mojo.propertyhelper.beans.PropertyDefinition.getValueFunction;
23  
24  import org.basepom.mojo.propertyhelper.InterpolatorFactory;
25  import org.basepom.mojo.propertyhelper.TransformerRegistry;
26  
27  import java.io.IOException;
28  import java.util.Arrays;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  import java.util.Set;
32  
33  import com.google.common.collect.ImmutableMap;
34  import com.google.common.collect.ImmutableSet;
35  import org.codehaus.plexus.interpolation.InterpolationException;
36  
37  public class PropertyGroup {
38  
39      /**
40       * Property group id.
41       */
42      String id;
43  
44      /**
45       * Activate the group if the current project version does not contain SNAPSHOT. Field injected by Maven.
46       */
47      boolean activeOnRelease = true;
48  
49      /**
50       * Activate the group if the current project version contains SNAPSHOT. Field injected by Maven.
51       */
52      boolean activeOnSnapshot = true;
53  
54      /**
55       * Action if this property group defines a duplicate property. Field injected by Maven.
56       */
57      String onDuplicateProperty = "fail";
58  
59      /**
60       * Action if any property from that group could not be defined. Field injected by Maven.
61       */
62      String onMissingProperty = "fail";
63  
64      /**
65       * Property definitions in this group. Field injected by Maven.
66       */
67      PropertyDefinition[] properties = new PropertyDefinition[0];
68  
69      // Must be noargs c'tor for maven property injection */
70      public PropertyGroup() {
71      }
72  
73      public String getId() {
74          return id;
75      }
76  
77      public PropertyGroup setId(String id) {
78          this.id = id;
79          return this;
80      }
81  
82      public boolean isActiveOnRelease() {
83          return activeOnRelease;
84      }
85  
86      public PropertyGroup setActiveOnRelease(final boolean activeOnRelease) {
87          this.activeOnRelease = activeOnRelease;
88          return this;
89      }
90  
91      public boolean isActiveOnSnapshot() {
92          return activeOnSnapshot;
93      }
94  
95      public PropertyGroup setActiveOnSnapshot(final boolean activeOnSnapshot) {
96          this.activeOnSnapshot = activeOnSnapshot;
97          return this;
98      }
99  
100     public IgnoreWarnFail getOnDuplicateProperty() {
101         return IgnoreWarnFail.forString(onDuplicateProperty);
102     }
103 
104     public PropertyGroup setOnDuplicateProperty(final String onDuplicateProperty) {
105         IgnoreWarnFail.forString(onDuplicateProperty);
106         this.onDuplicateProperty = onDuplicateProperty;
107         return this;
108     }
109 
110     public IgnoreWarnFail getOnMissingProperty() {
111         return IgnoreWarnFail.forString(onMissingProperty);
112     }
113 
114     public PropertyGroup setOnMissingProperty(final String onMissingProperty) {
115         IgnoreWarnFail.forString(onMissingProperty);
116         this.onMissingProperty = onMissingProperty;
117         return this;
118     }
119 
120     public Map<String, String> getProperties() {
121         return ImmutableMap.copyOf(Arrays.stream(properties).collect(toImmutableMap(getNameFunction(), getValueFunction())));
122     }
123 
124     public PropertyGroup setProperties(final Map<String, String> properties) {
125         checkNotNull(properties, "properties is null");
126         this.properties = new PropertyDefinition[properties.size()];
127 
128         int i = 0;
129         for (Entry<String, String> entry : properties.entrySet()) {
130             this.properties[i] = new PropertyDefinition(entry.getKey(), entry.getValue());
131         }
132         return this;
133     }
134 
135     public Set<String> getPropertyNames() {
136         return ImmutableSet.copyOf(Arrays.stream(properties).map(getNameFunction()).collect(toImmutableSet()));
137     }
138 
139     public String getPropertyValue(final InterpolatorFactory interpolatorFactory, final String propertyName, final Map<String, String> propElements)
140             throws IOException, InterpolationException {
141 
142         ImmutableMap<String, PropertyDefinition> definitionMap = ImmutableMap.copyOf(
143                 Arrays.stream(properties).collect(toImmutableMap(getNameFunction(), identity())));
144 
145         if (!definitionMap.containsKey(propertyName)) {
146             return "";
147         }
148 
149         final PropertyDefinition propertyDefinition = definitionMap.get(propertyName);
150 
151         final String result = interpolatorFactory.interpolate(propertyDefinition.getValue(), getOnMissingProperty(), propElements);
152         return TransformerRegistry.applyTransformers(propertyDefinition.getTransformers(), result);
153     }
154 }