1 | |
package org.jbehave.core.model; |
2 | |
|
3 | |
import static java.lang.Boolean.parseBoolean; |
4 | |
import static java.util.regex.Pattern.DOTALL; |
5 | |
import static java.util.regex.Pattern.compile; |
6 | |
|
7 | |
import java.io.ByteArrayInputStream; |
8 | |
import java.io.IOException; |
9 | |
import java.util.ArrayList; |
10 | |
import java.util.LinkedHashMap; |
11 | |
import java.util.List; |
12 | |
import java.util.Map; |
13 | |
import java.util.Properties; |
14 | |
import java.util.regex.Matcher; |
15 | |
import java.util.regex.Pattern; |
16 | |
|
17 | |
import org.apache.commons.lang.StringUtils; |
18 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
19 | |
import org.apache.commons.lang.builder.ToStringStyle; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public class ExamplesTable { |
71 | 1 | public static final ExamplesTable EMPTY = new ExamplesTable(""); |
72 | |
|
73 | |
private static final String HEADER_SEPARATOR = "|"; |
74 | |
private static final String VALUE_SEPARATOR = "|"; |
75 | |
private static final String IGNORABLE_SEPARATOR = "|--"; |
76 | 150 | private final List<Map<String, String>> data = new ArrayList<Map<String, String>>(); |
77 | |
private final String tableAsString; |
78 | |
private final String headerSeparator; |
79 | |
private final String valueSeparator; |
80 | |
private final String ignorableSeparator; |
81 | 150 | private final List<String> headers = new ArrayList<String>(); |
82 | 150 | private final Properties properties = new Properties(); |
83 | 150 | private boolean trim = true; |
84 | |
|
85 | |
public ExamplesTable(String tableAsString) { |
86 | 26 | this(tableAsString, HEADER_SEPARATOR, VALUE_SEPARATOR); |
87 | 26 | } |
88 | |
|
89 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator) { |
90 | 150 | this(tableAsString, headerSeparator, valueSeparator, IGNORABLE_SEPARATOR); |
91 | 150 | } |
92 | |
|
93 | 150 | public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, String ignorableSeparator) { |
94 | 150 | this.tableAsString = tableAsString; |
95 | 150 | this.headerSeparator = headerSeparator; |
96 | 150 | this.valueSeparator = valueSeparator; |
97 | 150 | this.ignorableSeparator = ignorableSeparator; |
98 | 150 | parse(); |
99 | 150 | } |
100 | |
|
101 | |
private void parse() { |
102 | 150 | data.clear(); |
103 | 150 | headers.clear(); |
104 | 150 | String[] rows = splitInRows(stripProperties(tableAsString.trim())); |
105 | 359 | for (int row = 0; row < rows.length; row++) { |
106 | 209 | String rowAsString = rows[row]; |
107 | 209 | if (rowAsString.startsWith(ignorableSeparator)) { |
108 | |
|
109 | 2 | continue; |
110 | 207 | } else if (row == 0) { |
111 | 150 | List<String> columns = columnsFor(rowAsString, headerSeparator); |
112 | 150 | headers.addAll(columns); |
113 | 150 | } else { |
114 | 57 | List<String> columns = columnsFor(rowAsString, valueSeparator); |
115 | 57 | Map<String, String> map = createRowMap(); |
116 | 179 | for (int column = 0; column < columns.size(); column++) { |
117 | 122 | map.put(headers.get(column), columns.get(column)); |
118 | |
} |
119 | 57 | data.add(map); |
120 | |
} |
121 | |
} |
122 | 150 | } |
123 | |
|
124 | |
private String stripProperties(String tableAsString) { |
125 | 150 | Pattern pattern = compile("\\{(.*?)\\}\\s*(.*)", DOTALL); |
126 | 150 | Matcher matcher = pattern.matcher(tableAsString); |
127 | 150 | if (matcher.matches()) { |
128 | 1 | parseProperties(matcher.group(1)); |
129 | 1 | return matcher.group(2); |
130 | |
} |
131 | 149 | return tableAsString; |
132 | |
} |
133 | |
|
134 | |
private void parseProperties(String propertiesAsString) { |
135 | 1 | properties.clear(); |
136 | |
try { |
137 | 1 | properties.load(new ByteArrayInputStream(propertiesAsString.replace(",", "\n").getBytes())); |
138 | 0 | } catch (IOException e) { |
139 | |
|
140 | 1 | } |
141 | 1 | trim = parseBoolean(properties.getProperty("trim", "true")); |
142 | 1 | } |
143 | |
|
144 | |
private String[] splitInRows(String table) { |
145 | 150 | return table.split("\n"); |
146 | |
} |
147 | |
|
148 | |
private List<String> columnsFor(String row, String separator) { |
149 | 207 | List<String> columns = new ArrayList<String>(); |
150 | |
|
151 | 675 | for (String column : row.split(buildRegex(separator), -1)) { |
152 | 468 | columns.add(valueOf(column)); |
153 | |
} |
154 | |
|
155 | 207 | if (StringUtils.isBlank(columns.get(0))) { |
156 | 201 | columns.remove(0); |
157 | |
} |
158 | 207 | int lastIndex = columns.size() - 1; |
159 | 207 | if (lastIndex != -1 && StringUtils.isBlank(columns.get(lastIndex))) { |
160 | 81 | columns.remove(lastIndex); |
161 | |
} |
162 | 207 | return columns; |
163 | |
} |
164 | |
|
165 | |
private String valueOf(String column) { |
166 | 468 | return trim ? column.trim() : column; |
167 | |
} |
168 | |
|
169 | |
private String buildRegex(String separator) { |
170 | 207 | char[] chars = separator.toCharArray(); |
171 | 207 | StringBuffer sb = new StringBuffer(); |
172 | 416 | for (char c : chars) { |
173 | 209 | sb.append("\\").append(c); |
174 | |
} |
175 | 207 | return sb.toString(); |
176 | |
} |
177 | |
|
178 | |
protected Map<String, String> createRowMap() { |
179 | 57 | return new LinkedHashMap<String, String>(); |
180 | |
} |
181 | |
|
182 | |
public Properties getProperties() { |
183 | 1 | return properties; |
184 | |
} |
185 | |
|
186 | |
public List<String> getHeaders() { |
187 | 22 | return headers; |
188 | |
} |
189 | |
|
190 | |
public Map<String, String> getRow(int row) { |
191 | 84 | return data.get(row); |
192 | |
} |
193 | |
|
194 | |
public int getRowCount() { |
195 | 31 | return data.size(); |
196 | |
} |
197 | |
|
198 | |
public List<Map<String, String>> getRows() { |
199 | 24 | return data; |
200 | |
} |
201 | |
|
202 | |
public String getHeaderSeparator() { |
203 | 2 | return headerSeparator; |
204 | |
} |
205 | |
|
206 | |
public String getValueSeparator() { |
207 | 2 | return valueSeparator; |
208 | |
} |
209 | |
|
210 | |
public String asString() { |
211 | 14 | return tableAsString; |
212 | |
} |
213 | |
|
214 | |
@Override |
215 | |
public String toString() { |
216 | 30 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
217 | |
} |
218 | |
|
219 | |
} |