| 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 | 3 | this(new Properties()); |
| 23 | 3 | } |
| 24 | |
|
| 25 | 1241 | public Meta(Properties properties) { |
| 26 | 1241 | this.properties = properties; |
| 27 | 1241 | } |
| 28 | |
|
| 29 | 30 | public Meta(List<String> properties) { |
| 30 | 30 | this.properties = new Properties(); |
| 31 | 30 | parse(properties); |
| 32 | 30 | } |
| 33 | |
|
| 34 | |
private void parse(List<String> propertiesAsString) { |
| 35 | 30 | for (String propertyAsString : new HashSet<String>(propertiesAsString)) { |
| 36 | 35 | Property property = new Property(propertyAsString); |
| 37 | 35 | this.properties.setProperty(property.getName(), property.getValue()); |
| 38 | 35 | } |
| 39 | 30 | } |
| 40 | |
|
| 41 | |
public Set<String> getPropertyNames() { |
| 42 | 337 | Set<String> names = new TreeSet<String>(); |
| 43 | 337 | for (Object key : properties.keySet()) { |
| 44 | 567 | names.add((String) key); |
| 45 | |
} |
| 46 | 337 | return names; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public boolean hasProperty(String name) { |
| 50 | 1482 | return properties.containsKey(name); |
| 51 | |
} |
| 52 | |
|
| 53 | |
public String getProperty(String name) { |
| 54 | 1588 | String value = properties.getProperty(name); |
| 55 | 1588 | if (value == null) { |
| 56 | 3 | return BLANK; |
| 57 | |
} |
| 58 | 1585 | return value; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public Meta inheritFrom(Meta meta) { |
| 62 | 129 | return inherit(this, meta); |
| 63 | |
} |
| 64 | |
|
| 65 | |
private Meta inherit(Meta child, Meta parent) { |
| 66 | 129 | Set<String> names = new HashSet<String>(child.getPropertyNames()); |
| 67 | |
|
| 68 | 129 | names.addAll(parent.getPropertyNames()); |
| 69 | 129 | Properties inherited = new Properties(); |
| 70 | 129 | for (String name : names) { |
| 71 | 454 | if (child.hasProperty(name)) { |
| 72 | 96 | inherited.put(name, child.getProperty(name)); |
| 73 | |
} else { |
| 74 | 358 | inherited.put(name, parent.getProperty(name)); |
| 75 | |
} |
| 76 | |
} |
| 77 | 129 | return new Meta(inherited); |
| 78 | |
} |
| 79 | |
|
| 80 | |
public boolean isEmpty() { |
| 81 | 42 | return properties.isEmpty(); |
| 82 | |
} |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public String toString() { |
| 86 | 1044 | 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 | 66 | public Property(String propertyAsString) { |
| 98 | 66 | this.propertyAsString = propertyAsString.trim(); |
| 99 | 66 | parse(); |
| 100 | 66 | } |
| 101 | |
|
| 102 | |
private void parse() { |
| 103 | 66 | name = StringUtils.substringBefore(propertyAsString, SPACE).trim(); |
| 104 | 66 | value = StringUtils.substringAfter(propertyAsString, SPACE).trim(); |
| 105 | 66 | } |
| 106 | |
|
| 107 | |
public String getName() { |
| 108 | 66 | return name; |
| 109 | |
} |
| 110 | |
|
| 111 | |
public String getValue() { |
| 112 | 66 | return value; |
| 113 | |
} |
| 114 | |
|
| 115 | |
} |
| 116 | |
|
| 117 | |
} |