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;
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  }