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 | |
public static final String SPACE = " "; |
21 | |
|
22 | |
private final Properties properties; |
23 | |
|
24 | |
public Meta() { |
25 | 3 | this(new Properties()); |
26 | 3 | } |
27 | |
|
28 | 288 | public Meta(Properties properties) { |
29 | 288 | this.properties = properties; |
30 | 288 | } |
31 | |
|
32 | 41 | public Meta(List<String> properties) { |
33 | 41 | this.properties = new Properties(); |
34 | 41 | parse(properties); |
35 | 41 | } |
36 | |
|
37 | |
private void parse(List<String> propertiesAsString) { |
38 | 41 | for (String propertyAsString : new HashSet<String>(propertiesAsString)) { |
39 | 46 | Property property = new Property(propertyAsString); |
40 | 46 | this.properties.setProperty(property.getName(), property.getValue()); |
41 | 46 | } |
42 | 41 | } |
43 | |
|
44 | |
public Set<String> getPropertyNames() { |
45 | 433 | Set<String> names = new TreeSet<String>(); |
46 | 433 | for (Object key : properties.keySet()) { |
47 | 703 | names.add((String) key); |
48 | 703 | } |
49 | 433 | return names; |
50 | |
} |
51 | |
|
52 | |
public boolean hasProperty(String name) { |
53 | 606 | return properties.containsKey(name); |
54 | |
} |
55 | |
|
56 | |
public String getProperty(String name) { |
57 | 715 | String value = properties.getProperty(name); |
58 | 715 | if (value == null) { |
59 | 3 | return BLANK; |
60 | |
} |
61 | 712 | return value; |
62 | |
} |
63 | |
|
64 | |
public Meta inheritFrom(Meta meta) { |
65 | 158 | return inherit(this, meta); |
66 | |
} |
67 | |
|
68 | |
private Meta inherit(Meta child, Meta parent) { |
69 | 158 | Set<String> names = new HashSet<String>(child.getPropertyNames()); |
70 | |
|
71 | 158 | names.addAll(parent.getPropertyNames()); |
72 | 158 | Properties inherited = new Properties(); |
73 | 158 | for (String name : names) { |
74 | 577 | if (child.hasProperty(name)) { |
75 | 122 | inherited.put(name, child.getProperty(name)); |
76 | |
} else { |
77 | 455 | inherited.put(name, parent.getProperty(name)); |
78 | |
} |
79 | 577 | } |
80 | 158 | return new Meta(inherited); |
81 | |
} |
82 | |
|
83 | |
public boolean isEmpty() { |
84 | 49 | return properties.isEmpty(); |
85 | |
} |
86 | |
|
87 | |
public String asString(Keywords keywords) { |
88 | 0 | StringBuffer sb = new StringBuffer(); |
89 | 0 | for (String name : getPropertyNames()) { |
90 | 0 | sb.append(keywords.metaProperty()).append(name).append(SPACE) |
91 | |
.append(getProperty(name)).append(SPACE); |
92 | 0 | } |
93 | 0 | return sb.toString().trim(); |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public String toString() { |
98 | 51 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
99 | |
} |
100 | |
|
101 | |
public static class Property { |
102 | |
|
103 | |
private String propertyAsString; |
104 | |
private String name; |
105 | |
private String value; |
106 | |
|
107 | 85 | public Property(String propertyAsString) { |
108 | 85 | this.propertyAsString = propertyAsString.trim(); |
109 | 85 | parse(); |
110 | 85 | } |
111 | |
|
112 | |
private void parse() { |
113 | 85 | name = StringUtils.substringBefore(propertyAsString, SPACE).trim(); |
114 | 85 | value = StringUtils.substringAfter(propertyAsString, SPACE).trim(); |
115 | 85 | } |
116 | |
|
117 | |
public String getName() { |
118 | 85 | return name; |
119 | |
} |
120 | |
|
121 | |
public String getValue() { |
122 | 85 | return value; |
123 | |
} |
124 | |
|
125 | |
} |
126 | |
|
127 | |
public static Meta createMeta(String meta, Keywords keywords) { |
128 | 9 | List<String> properties = new ArrayList<String>(); |
129 | 30 | for (String property : meta.split(keywords.metaProperty())) { |
130 | 21 | if (StringUtils.isNotBlank(property)) { |
131 | 14 | String beforeIgnorable = StringUtils.substringBefore(property,keywords.ignorable()); |
132 | 14 | if ( StringUtils.isNotBlank(beforeIgnorable)){ |
133 | 12 | properties.add(beforeIgnorable); |
134 | |
} |
135 | |
} |
136 | |
} |
137 | 9 | return new Meta(properties); |
138 | |
} |
139 | |
|
140 | |
} |