1 | |
package org.jbehave.core.parsers; |
2 | |
|
3 | |
import static java.util.regex.Pattern.DOTALL; |
4 | |
import static java.util.regex.Pattern.compile; |
5 | |
|
6 | |
import java.io.File; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
import java.util.regex.Matcher; |
10 | |
import java.util.regex.Pattern; |
11 | |
|
12 | |
import org.apache.commons.lang.StringUtils; |
13 | |
import org.jbehave.core.configuration.Keywords; |
14 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
15 | |
import org.jbehave.core.model.Description; |
16 | |
import org.jbehave.core.model.ExamplesTable; |
17 | |
import org.jbehave.core.model.GivenStories; |
18 | |
import org.jbehave.core.model.Meta; |
19 | |
import org.jbehave.core.model.Narrative; |
20 | |
import org.jbehave.core.model.Scenario; |
21 | |
import org.jbehave.core.model.Story; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class RegexStoryParser implements StoryParser { |
28 | |
|
29 | |
private static final String NONE = ""; |
30 | |
private final Keywords keywords; |
31 | |
|
32 | |
public RegexStoryParser() { |
33 | 22 | this(new LocalizedKeywords()); |
34 | 22 | } |
35 | |
|
36 | 442 | public RegexStoryParser(Keywords keywords) { |
37 | 442 | this.keywords = keywords; |
38 | 442 | } |
39 | |
|
40 | |
public Story parseStory(String storyAsText) { |
41 | 1 | return parseStory(storyAsText, null); |
42 | |
} |
43 | |
|
44 | |
public Story parseStory(String storyAsText, String storyPath) { |
45 | 20 | Description description = parseDescriptionFrom(storyAsText); |
46 | 20 | Meta meta = parseStoryMetaFrom(storyAsText); |
47 | 20 | Narrative narrative = parseNarrativeFrom(storyAsText); |
48 | 20 | List<Scenario> scenarios = parseScenariosFrom(storyAsText); |
49 | 20 | Story story = new Story(storyPath, description, meta, narrative, scenarios); |
50 | 20 | if (storyPath != null) { |
51 | 16 | story.namedAs(new File(storyPath).getName()); |
52 | |
} |
53 | 20 | return story; |
54 | |
} |
55 | |
|
56 | |
private Description parseDescriptionFrom(String storyAsText) { |
57 | 20 | Matcher findingDescription = patternToPullDescriptionIntoGroupOne().matcher(storyAsText); |
58 | 20 | if (findingDescription.matches()) { |
59 | 11 | return new Description(findingDescription.group(1).trim()); |
60 | |
} |
61 | 9 | return Description.EMPTY; |
62 | |
} |
63 | |
|
64 | |
private Meta parseStoryMetaFrom(String storyAsText) { |
65 | 20 | Matcher findingMeta = patternToPullStoryMetaIntoGroupOne().matcher(preScenarioText(storyAsText)); |
66 | 20 | if (findingMeta.matches()) { |
67 | 2 | String meta = findingMeta.group(1).trim(); |
68 | 2 | return createMeta(meta); |
69 | |
} |
70 | 18 | return Meta.EMPTY; |
71 | |
} |
72 | |
|
73 | |
private String preScenarioText(String storyAsText) { |
74 | 20 | String[] split = storyAsText.split(keywords.scenario()); |
75 | 20 | return split.length > 0 ? split[0] : storyAsText; |
76 | |
} |
77 | |
|
78 | |
private Meta createMeta(String meta) { |
79 | 5 | List<String> properties = new ArrayList<String>(); |
80 | 17 | for (String property : meta.split(keywords.metaProperty())) { |
81 | 12 | if (!StringUtils.isBlank(property)) { |
82 | 7 | properties.add(property); |
83 | |
} |
84 | |
} |
85 | 5 | return new Meta(properties); |
86 | |
} |
87 | |
|
88 | |
private Narrative parseNarrativeFrom(String storyAsText) { |
89 | 20 | Matcher findingNarrative = patternToPullNarrativeIntoGroupOne().matcher(storyAsText); |
90 | 20 | if (findingNarrative.matches()) { |
91 | 3 | String narrative = findingNarrative.group(1).trim(); |
92 | 3 | return createNarrative(narrative); |
93 | |
} |
94 | 17 | return Narrative.EMPTY; |
95 | |
} |
96 | |
|
97 | |
private Narrative createNarrative(String narrative) { |
98 | 3 | Pattern findElements = patternToPullNarrativeElementsIntoGroups(); |
99 | 3 | Matcher findingElements = findElements.matcher(narrative); |
100 | 3 | if (findingElements.matches()) { |
101 | 2 | String inOrderTo = findingElements.group(1).trim(); |
102 | 2 | String asA = findingElements.group(2).trim(); |
103 | 2 | String iWantTo = findingElements.group(3).trim(); |
104 | 2 | return new Narrative(inOrderTo, asA, iWantTo); |
105 | |
} |
106 | 1 | return Narrative.EMPTY; |
107 | |
} |
108 | |
|
109 | |
private List<Scenario> parseScenariosFrom(String storyAsText) { |
110 | 20 | List<Scenario> parsed = new ArrayList<Scenario>(); |
111 | 20 | for (String scenarioAsText : splitScenarios(storyAsText)) { |
112 | 121 | parsed.add(parseScenario(scenarioAsText)); |
113 | |
} |
114 | 20 | return parsed; |
115 | |
} |
116 | |
|
117 | |
private List<String> splitScenarios(String storyAsText) { |
118 | 20 | List<String> scenarios = new ArrayList<String>(); |
119 | 20 | String scenarioKeyword = keywords.scenario(); |
120 | |
|
121 | |
|
122 | 20 | if (StringUtils.contains(storyAsText, scenarioKeyword)) { |
123 | 11 | storyAsText = StringUtils.substringAfter(storyAsText, scenarioKeyword); |
124 | |
} |
125 | |
|
126 | 143 | for (String scenarioAsText : storyAsText.split(scenarioKeyword)) { |
127 | 123 | if (scenarioAsText.trim().length() > 0) { |
128 | 121 | scenarios.add(scenarioKeyword + "\n" + scenarioAsText); |
129 | |
} |
130 | |
} |
131 | 20 | return scenarios; |
132 | |
} |
133 | |
|
134 | |
private Scenario parseScenario(String scenarioAsText) { |
135 | 121 | String title = findScenarioTitle(scenarioAsText); |
136 | 121 | Meta meta = findScenarioMeta(scenarioAsText); |
137 | 121 | ExamplesTable examplesTable = findExamplesTable(scenarioAsText); |
138 | 121 | GivenStories givenStories = findGivenStories(scenarioAsText); |
139 | 121 | if (givenStories.requireParameters()) { |
140 | 1 | givenStories.useExamplesTable(examplesTable); |
141 | |
} |
142 | 121 | List<String> steps = findSteps(scenarioAsText); |
143 | 121 | return new Scenario(title, meta, givenStories, examplesTable, steps); |
144 | |
} |
145 | |
|
146 | |
private String findScenarioTitle(String scenarioAsText) { |
147 | 121 | Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne().matcher(scenarioAsText); |
148 | 121 | return findingTitle.find() ? findingTitle.group(1).trim() : NONE; |
149 | |
} |
150 | |
|
151 | |
private Meta findScenarioMeta(String scenarioAsText) { |
152 | 121 | Matcher findingMeta = patternToPullScenarioMetaIntoGroupOne().matcher(scenarioAsText); |
153 | 121 | if (findingMeta.matches()) { |
154 | 3 | String meta = findingMeta.group(1).trim(); |
155 | 3 | return createMeta(meta); |
156 | |
} |
157 | 118 | return Meta.EMPTY; |
158 | |
} |
159 | |
|
160 | |
private ExamplesTable findExamplesTable(String scenarioAsText) { |
161 | 121 | Matcher findingTable = patternToPullExamplesTableIntoGroupOne().matcher(scenarioAsText); |
162 | 121 | String table = findingTable.find() ? findingTable.group(1).trim() : NONE; |
163 | 121 | return new ExamplesTable(table, keywords.examplesTableHeaderSeparator(), keywords.examplesTableValueSeparator()); |
164 | |
} |
165 | |
|
166 | |
private GivenStories findGivenStories(String scenarioAsText) { |
167 | 121 | Matcher findingGivenStories = patternToPullGivenStoriesIntoGroupOne().matcher(scenarioAsText); |
168 | 121 | String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE; |
169 | 121 | return new GivenStories(givenStories); |
170 | |
} |
171 | |
|
172 | |
private List<String> findSteps(String scenarioAsText) { |
173 | 121 | Matcher matcher = patternToPullStepsIntoGroupOne().matcher(scenarioAsText); |
174 | 121 | List<String> steps = new ArrayList<String>(); |
175 | 121 | int startAt = 0; |
176 | 15175 | while (matcher.find(startAt)) { |
177 | 15054 | steps.add(StringUtils.substringAfter(matcher.group(1), "\n")); |
178 | 15054 | startAt = matcher.start(4); |
179 | |
} |
180 | 121 | return steps; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
private Pattern patternToPullDescriptionIntoGroupOne() { |
186 | 20 | String metaOrNarrativeOrScenario = concatenateWithOr(keywords.meta(), keywords.narrative(), keywords.scenario()); |
187 | 20 | return compile("(.*?)(" + metaOrNarrativeOrScenario + ").*", DOTALL); |
188 | |
} |
189 | |
|
190 | |
private Pattern patternToPullStoryMetaIntoGroupOne() { |
191 | 20 | return compile(".*" + keywords.meta() + "(.*?)\\s*(\\Z|" + keywords.narrative() + ").*", DOTALL); |
192 | |
} |
193 | |
|
194 | |
private Pattern patternToPullNarrativeIntoGroupOne() { |
195 | 20 | return compile(".*" + keywords.narrative() + "(.*?)\\s*(" + keywords.scenario() + ").*", DOTALL); |
196 | |
} |
197 | |
|
198 | |
private Pattern patternToPullNarrativeElementsIntoGroups() { |
199 | 3 | return compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo() |
200 | |
+ "(.*)", DOTALL); |
201 | |
} |
202 | |
|
203 | |
private Pattern patternToPullScenarioTitleIntoGroupOne() { |
204 | 121 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
205 | 121 | return compile(keywords.scenario() + "((.|\\n)*?)\\s*(" + keywords.meta() + "|" + startingWords + ").*"); |
206 | |
} |
207 | |
|
208 | |
private Pattern patternToPullScenarioMetaIntoGroupOne() { |
209 | 121 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
210 | 121 | return compile(".*" + keywords.meta() + "(.*?)\\s*(" + keywords.givenStories() + "|" + startingWords + ").*", |
211 | |
DOTALL); |
212 | |
} |
213 | |
|
214 | |
private Pattern patternToPullGivenStoriesIntoGroupOne() { |
215 | 121 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
216 | 121 | return compile(".*\\n" + keywords.givenStories() + "((.|\\n)*?)\\s*(" + startingWords + ").*"); |
217 | |
} |
218 | |
|
219 | |
private Pattern patternToPullStepsIntoGroupOne() { |
220 | 121 | String initialStartingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
221 | 121 | String followingStartingWords = concatenateWithOr("\\n", "\\s", keywords.startingWords()); |
222 | 121 | return compile( |
223 | |
"((" + initialStartingWords + ") (.)*?)\\s*(\\Z|" + followingStartingWords + "|\\n" |
224 | |
+ keywords.examplesTable() + ")", DOTALL); |
225 | |
} |
226 | |
|
227 | |
private Pattern patternToPullExamplesTableIntoGroupOne() { |
228 | 121 | return compile(".*\\n" + keywords.examplesTable() + "\\s*(.*)", DOTALL); |
229 | |
} |
230 | |
|
231 | |
private String concatenateWithOr(String... keywords) { |
232 | 20 | return concatenateWithOr(null, null, keywords); |
233 | |
} |
234 | |
|
235 | |
private String concatenateWithOr(String beforeKeyword, String afterKeyword, String[] keywords) { |
236 | 625 | StringBuilder builder = new StringBuilder(); |
237 | 625 | String before = beforeKeyword != null ? beforeKeyword : NONE; |
238 | 625 | String after = afterKeyword != null ? afterKeyword : NONE; |
239 | 3710 | for (String keyword : keywords) { |
240 | 3085 | builder.append(before).append(keyword).append(after).append("|"); |
241 | |
} |
242 | 625 | return StringUtils.chomp(builder.toString(), "|"); |
243 | |
|
244 | |
} |
245 | |
|
246 | |
} |