Meta.java

  1. package org.jbehave.core.model;

  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Optional;
  6. import java.util.Properties;
  7. import java.util.Set;
  8. import java.util.TreeSet;

  9. import org.apache.commons.lang3.StringUtils;
  10. import org.apache.commons.lang3.builder.ToStringBuilder;
  11. import org.apache.commons.lang3.builder.ToStringStyle;
  12. import org.jbehave.core.configuration.Keywords;

  13. public class Meta {

  14.     public static final Meta EMPTY = new Meta();

  15.     public static final String BLANK = "";
  16.     public static final String SPACE = " ";

  17.     private final Properties properties;

  18.     public Meta() {
  19.         this(new Properties());
  20.     }

  21.     public Meta(Properties properties) {
  22.         this.properties = properties;
  23.     }

  24.     public Meta(List<String> properties) {
  25.         this.properties = new Properties();
  26.         parse(properties);
  27.     }

  28.     private void parse(List<String> propertiesAsString) {
  29.         for (String propertyAsString : new HashSet<>(propertiesAsString)) {
  30.             Property property = new Property(propertyAsString);
  31.             this.properties.setProperty(property.getName(), property.getValue());
  32.         }
  33.     }

  34.     public Set<String> getPropertyNames() {
  35.         Set<String> names = new TreeSet<>();
  36.         for (Object key : properties.keySet()) {
  37.             names.add((String) key);
  38.         }
  39.         return names;
  40.     }

  41.     public boolean hasProperty(String name) {
  42.         return properties.containsKey(name);
  43.     }

  44.     public String getProperty(String name) {
  45.         return getOptionalProperty(name).orElse(BLANK);
  46.     }

  47.     public Optional<String> getOptionalProperty(String name) {
  48.         String value = properties.getProperty(name);
  49.         return Optional.ofNullable(value);
  50.     }

  51.     public Meta inheritFrom(Meta meta) {      
  52.         return inherit(this, meta);
  53.     }

  54.     private Meta inherit(Meta child, Meta parent) {
  55.         Set<String> names = new HashSet<>(child.getPropertyNames());
  56.         // only names that are not already present in the child are added
  57.         names.addAll(parent.getPropertyNames());
  58.         Properties inherited = new Properties();
  59.         for (String name : names) {
  60.             if (child.hasProperty(name)) {
  61.                 inherited.put(name, child.getProperty(name));
  62.             } else { // if not in child, must be in parent
  63.                 inherited.put(name, parent.getProperty(name));
  64.             }
  65.         }
  66.         return new Meta(inherited);
  67.     }

  68.     public boolean isEmpty() {
  69.         return properties.isEmpty();
  70.     }
  71.    
  72.     public String asString(Keywords keywords) {
  73.         StringBuilder sb = new StringBuilder();
  74.         for (String name : getPropertyNames()) {
  75.             sb.append(keywords.metaProperty()).append(name).append(SPACE)
  76.                     .append(getProperty(name)).append(SPACE);
  77.         }
  78.         return sb.toString().trim();
  79.     }

  80.     @Override
  81.     public String toString() {
  82.         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  83.     }

  84.     public static class Property {

  85.         private String propertyAsString;
  86.         private String name;
  87.         private String value;

  88.         public Property(String propertyAsString) {
  89.             this.propertyAsString = propertyAsString.trim();
  90.             parse();
  91.         }

  92.         private void parse() {
  93.             name = StringUtils.substringBefore(propertyAsString, SPACE).trim();
  94.             value = StringUtils.substringAfter(propertyAsString, SPACE).trim();
  95.         }

  96.         public String getName() {
  97.             return name;
  98.         }

  99.         public String getValue() {
  100.             return value;
  101.         }

  102.     }

  103.     public static Meta createMeta(String meta, Keywords keywords) {
  104.         List<String> properties = new ArrayList<>();
  105.         for (String property : meta.split(keywords.metaProperty())) {
  106.             if (StringUtils.isNotBlank(property)) {
  107.                 String beforeIgnorable = StringUtils.substringBefore(property,keywords.ignorable());
  108.                 if (StringUtils.isNotBlank(beforeIgnorable)) {
  109.                     properties.add(beforeIgnorable);
  110.                 }
  111.             }
  112.         }
  113.         return new Meta(properties);
  114.     }

  115. }