PropertyField.java

  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. package org.basepom.mojo.propertyhelper;

  15. import static com.google.common.base.Preconditions.checkNotNull;
  16. import static java.lang.String.format;

  17. import org.basepom.mojo.propertyhelper.beans.PropertyGroup;

  18. import java.io.IOException;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Optional;

  22. import com.google.common.collect.ImmutableList;
  23. import com.google.common.collect.ImmutableList.Builder;
  24. import org.apache.maven.model.Model;
  25. import org.apache.maven.plugin.MojoExecutionException;
  26. import org.codehaus.plexus.interpolation.InterpolationException;

  27. public class PropertyField
  28.         implements PropertyElement {

  29.     private final String propertyName;
  30.     private final String propertyValue;

  31.     PropertyField(final String propertyName, final String propertyValue) {
  32.         this.propertyName = checkNotNull(propertyName, "propertyName is null");
  33.         this.propertyValue = checkNotNull(propertyValue, "propertyValue is null");
  34.     }

  35.     public static List<PropertyElement> createProperties(final Model model, final Map<String, String> values, final PropertyGroup propertyGroup)
  36.             throws MojoExecutionException, IOException {
  37.         checkNotNull(model, "model is null");
  38.         checkNotNull(values, "values is null");
  39.         checkNotNull(propertyGroup, "propertyGroup is null");

  40.         final InterpolatorFactory interpolatorFactory = new InterpolatorFactory(model);

  41.         final Builder<PropertyElement> result = ImmutableList.builder();
  42.         final Map<String, String> properties = propertyGroup.getProperties();

  43.         for (String name : properties.keySet()) {
  44.             try {
  45.                 final String value = propertyGroup.getPropertyValue(interpolatorFactory, name, values);
  46.                 result.add(new PropertyField(name, value));
  47.             } catch (InterpolationException e) {
  48.                 throw new MojoExecutionException(format("Could not interpolate '%s", model), e);
  49.             }
  50.         }
  51.         return result.build();
  52.     }

  53.     @Override
  54.     public String getPropertyName() {
  55.         return propertyName;
  56.     }

  57.     @Override
  58.     public Optional<String> getPropertyValue() {
  59.         return Optional.of(propertyValue);
  60.     }

  61.     @Override
  62.     public boolean isExport() {
  63.         return true;
  64.     }
  65. }