| 1 | |
package org.jbehave.core.model; |
| 2 | |
|
| 3 | |
import java.util.HashSet; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Properties; |
| 6 | |
import java.util.Set; |
| 7 | |
import java.util.TreeSet; |
| 8 | |
|
| 9 | |
import org.apache.commons.lang.StringUtils; |
| 10 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 11 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 12 | |
|
| 13 | |
public class Meta { |
| 14 | |
|
| 15 | 1 | public static final Meta EMPTY = new Meta(); |
| 16 | |
|
| 17 | |
public static final String BLANK = ""; |
| 18 | |
|
| 19 | |
private final Properties properties; |
| 20 | |
|
| 21 | |
public Meta() { |
| 22 | 2 | this(new Properties()); |
| 23 | 2 | } |
| 24 | |
|
| 25 | 46 | public Meta(Properties properties) { |
| 26 | 46 | this.properties = properties; |
| 27 | 46 | } |
| 28 | |
|
| 29 | 24 | public Meta(List<String> properties) { |
| 30 | 24 | this.properties = new Properties(); |
| 31 | 24 | parse(properties); |
| 32 | 24 | } |
| 33 | |
|
| 34 | |
private void parse(List<String> propertiesAsString) { |
| 35 | 24 | for (String propertyAsString : new HashSet<String>(propertiesAsString)) { |
| 36 | 26 | Property property = new Property(propertyAsString); |
| 37 | 26 | this.properties.setProperty(property.getName(), property.getValue()); |
| 38 | 26 | } |
| 39 | 24 | } |
| 40 | |
|
| 41 | |
public Set<String> getPropertyNames() { |
| 42 | 81 | Set<String> names = new TreeSet<String>(); |
| 43 | 81 | for (Object key : properties.keySet()) { |
| 44 | 66 | names.add((String) key); |
| 45 | |
} |
| 46 | 81 | return names; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public boolean hasProperty(String name) { |
| 50 | 3 | return properties.containsKey(name); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public String getProperty(String name) { |
| 54 | 76 | String value = properties.getProperty(name); |
| 55 | 76 | if (value == null) { |
| 56 | 2 | return BLANK; |
| 57 | |
} |
| 58 | 74 | return value; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public Meta inheritFrom(Meta meta) { |
| 62 | 20 | return inherit(this, meta); |
| 63 | |
} |
| 64 | |
|
| 65 | |
private Meta inherit(Meta child, Meta parent) { |
| 66 | 20 | Set<String> names = new HashSet<String>(child.getPropertyNames()); |
| 67 | |
|
| 68 | 20 | names.addAll(parent.getPropertyNames()); |
| 69 | 20 | Properties inherited = new Properties(); |
| 70 | 20 | for (String name : names) { |
| 71 | 3 | if (child.hasProperty(name)) { |
| 72 | 1 | inherited.put(name, child.getProperty(name)); |
| 73 | |
} else { |
| 74 | 2 | inherited.put(name, parent.getProperty(name)); |
| 75 | |
} |
| 76 | |
} |
| 77 | 20 | return new Meta(inherited); |
| 78 | |
} |
| 79 | |
|
| 80 | |
public boolean isEmpty() { |
| 81 | 22 | return properties.isEmpty(); |
| 82 | |
} |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public String toString() { |
| 86 | 27 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public static class Property { |
| 90 | |
|
| 91 | |
private static final String SPACE = " "; |
| 92 | |
|
| 93 | |
private String propertyAsString; |
| 94 | |
private String name; |
| 95 | |
private String value; |
| 96 | |
|
| 97 | 50 | public Property(String propertyAsString) { |
| 98 | 50 | this.propertyAsString = propertyAsString.trim(); |
| 99 | 50 | parse(); |
| 100 | 50 | } |
| 101 | |
|
| 102 | |
private void parse() { |
| 103 | 50 | name = StringUtils.substringBefore(propertyAsString, SPACE).trim(); |
| 104 | 50 | value = StringUtils.substringAfter(propertyAsString, SPACE).trim(); |
| 105 | 50 | } |
| 106 | |
|
| 107 | |
public String getName() { |
| 108 | 50 | return name; |
| 109 | |
} |
| 110 | |
|
| 111 | |
public String getValue() { |
| 112 | 50 | return value; |
| 113 | |
} |
| 114 | |
|
| 115 | |
} |
| 116 | |
|
| 117 | |
} |