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