| 1 | |
package org.jbehave.core.parsers; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.regex.Matcher; |
| 7 | |
import java.util.regex.Pattern; |
| 8 | |
|
| 9 | |
import org.apache.commons.lang.StringUtils; |
| 10 | |
import org.jbehave.core.configuration.Keywords; |
| 11 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
| 12 | |
import org.jbehave.core.model.Description; |
| 13 | |
import org.jbehave.core.model.ExamplesTable; |
| 14 | |
import org.jbehave.core.model.ExamplesTableFactory; |
| 15 | |
import org.jbehave.core.model.GivenStories; |
| 16 | |
import org.jbehave.core.model.Lifecycle; |
| 17 | |
import org.jbehave.core.model.Meta; |
| 18 | |
import org.jbehave.core.model.Narrative; |
| 19 | |
import org.jbehave.core.model.Scenario; |
| 20 | |
import org.jbehave.core.model.Story; |
| 21 | |
|
| 22 | |
import static java.util.regex.Pattern.DOTALL; |
| 23 | |
import static java.util.regex.Pattern.compile; |
| 24 | |
import static org.apache.commons.lang.StringUtils.removeStart; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public class RegexStoryParser implements StoryParser { |
| 31 | |
|
| 32 | |
private static final String NONE = ""; |
| 33 | |
private final Keywords keywords; |
| 34 | |
private final ExamplesTableFactory tableFactory; |
| 35 | |
|
| 36 | |
public RegexStoryParser() { |
| 37 | 34 | this(new LocalizedKeywords()); |
| 38 | 34 | } |
| 39 | |
|
| 40 | |
public RegexStoryParser(Keywords keywords) { |
| 41 | 616 | this(keywords, new ExamplesTableFactory()); |
| 42 | 616 | } |
| 43 | |
|
| 44 | |
public RegexStoryParser(ExamplesTableFactory tableFactory) { |
| 45 | 0 | this(new LocalizedKeywords(), tableFactory); |
| 46 | 0 | } |
| 47 | |
|
| 48 | 616 | public RegexStoryParser(Keywords keywords, ExamplesTableFactory tableFactory) { |
| 49 | 616 | this.keywords = keywords; |
| 50 | 616 | this.tableFactory = tableFactory; |
| 51 | 616 | } |
| 52 | |
|
| 53 | |
public Story parseStory(String storyAsText) { |
| 54 | 4 | return parseStory(storyAsText, null); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public Story parseStory(String storyAsText, String storyPath) { |
| 58 | 34 | Description description = parseDescriptionFrom(storyAsText); |
| 59 | 34 | Meta meta = parseStoryMetaFrom(storyAsText); |
| 60 | 34 | Narrative narrative = parseNarrativeFrom(storyAsText); |
| 61 | 34 | GivenStories givenStories = parseGivenStories(storyAsText); |
| 62 | 34 | Lifecycle lifecycle = parseLifecycle(storyAsText); |
| 63 | 34 | List<Scenario> scenarios = parseScenariosFrom(storyAsText); |
| 64 | 34 | Story story = new Story(storyPath, description, meta, narrative, givenStories, lifecycle, scenarios); |
| 65 | 34 | if (storyPath != null) { |
| 66 | 27 | story.namedAs(new File(storyPath).getName()); |
| 67 | |
} |
| 68 | 34 | return story; |
| 69 | |
} |
| 70 | |
|
| 71 | |
private Description parseDescriptionFrom(String storyAsText) { |
| 72 | 34 | Matcher findingDescription = patternToPullDescriptionIntoGroupOne().matcher(storyAsText); |
| 73 | 34 | if (findingDescription.matches()) { |
| 74 | 23 | return new Description(findingDescription.group(1).trim()); |
| 75 | |
} |
| 76 | 11 | return Description.EMPTY; |
| 77 | |
} |
| 78 | |
|
| 79 | |
private Meta parseStoryMetaFrom(String storyAsText) { |
| 80 | 34 | Matcher findingMeta = patternToPullStoryMetaIntoGroupOne().matcher(preScenarioText(storyAsText)); |
| 81 | 34 | if (findingMeta.matches()) { |
| 82 | 4 | String meta = findingMeta.group(1).trim(); |
| 83 | 4 | return Meta.createMeta(meta, keywords); |
| 84 | |
} |
| 85 | 30 | return Meta.EMPTY; |
| 86 | |
} |
| 87 | |
|
| 88 | |
private String preScenarioText(String storyAsText) { |
| 89 | 34 | String[] split = storyAsText.split(keywords.scenario()); |
| 90 | 34 | return split.length > 0 ? split[0] : storyAsText; |
| 91 | |
} |
| 92 | |
|
| 93 | |
private Narrative parseNarrativeFrom(String storyAsText) { |
| 94 | 34 | Matcher findingNarrative = patternToPullNarrativeIntoGroupOne().matcher(storyAsText); |
| 95 | 34 | if (findingNarrative.matches()) { |
| 96 | 4 | String narrative = findingNarrative.group(1).trim(); |
| 97 | 4 | return createNarrative(narrative); |
| 98 | |
} |
| 99 | 30 | return Narrative.EMPTY; |
| 100 | |
} |
| 101 | |
|
| 102 | |
private Narrative createNarrative(String narrative) { |
| 103 | 4 | Matcher findingElements = patternToPullNarrativeElementsIntoGroups().matcher(narrative); |
| 104 | 4 | if (findingElements.matches()) { |
| 105 | 2 | String inOrderTo = findingElements.group(1).trim(); |
| 106 | 2 | String asA = findingElements.group(2).trim(); |
| 107 | 2 | String iWantTo = findingElements.group(3).trim(); |
| 108 | 2 | return new Narrative(inOrderTo, asA, iWantTo); |
| 109 | |
} |
| 110 | 2 | Matcher findingAlternativeElements = patternToPullAlternativeNarrativeElementsIntoGroups().matcher(narrative); |
| 111 | 2 | if (findingAlternativeElements.matches()) { |
| 112 | 1 | String asA = findingAlternativeElements.group(1).trim(); |
| 113 | 1 | String iWantTo = findingAlternativeElements.group(2).trim(); |
| 114 | 1 | String soThat = findingAlternativeElements.group(3).trim(); |
| 115 | 1 | return new Narrative("", asA, iWantTo, soThat); |
| 116 | |
} |
| 117 | 1 | return Narrative.EMPTY; |
| 118 | |
} |
| 119 | |
|
| 120 | |
private GivenStories parseGivenStories(String storyAsText) { |
| 121 | 34 | String scenarioKeyword = keywords.scenario(); |
| 122 | |
|
| 123 | 34 | String beforeScenario = ""; |
| 124 | 34 | if (StringUtils.contains(storyAsText, scenarioKeyword)) { |
| 125 | 22 | beforeScenario = StringUtils.substringBefore(storyAsText, scenarioKeyword); |
| 126 | |
} |
| 127 | 34 | Matcher findingGivenStories = patternToPullStoryGivenStoriesIntoGroupOne().matcher(beforeScenario); |
| 128 | 34 | String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE; |
| 129 | 34 | return new GivenStories(givenStories); |
| 130 | |
} |
| 131 | |
|
| 132 | |
private Lifecycle parseLifecycle(String storyAsText) { |
| 133 | 34 | String scenarioKeyword = keywords.scenario(); |
| 134 | |
|
| 135 | 34 | String beforeScenario = ""; |
| 136 | 34 | if (StringUtils.contains(storyAsText, scenarioKeyword)) { |
| 137 | 22 | beforeScenario = StringUtils.substringBefore(storyAsText, scenarioKeyword); |
| 138 | |
} |
| 139 | 34 | Matcher findingLifecycle = patternToPullLifecycleIntoGroupOne().matcher(beforeScenario); |
| 140 | 34 | String lifecycle = findingLifecycle.find() ? findingLifecycle.group(1).trim() : NONE; |
| 141 | 34 | Matcher findingBeforeAndAfter = compile(".*" + keywords.before() + "(.*)\\s*" + keywords.after() + "(.*)\\s*" , DOTALL).matcher(lifecycle); |
| 142 | 34 | if ( findingBeforeAndAfter.matches() ){ |
| 143 | 2 | List<String> beforeSteps = findSteps(startingWithNL(findingBeforeAndAfter.group(1).trim())); |
| 144 | 2 | List<String> afterSteps = findSteps(startingWithNL(findingBeforeAndAfter.group(2).trim())); |
| 145 | 2 | return new Lifecycle(beforeSteps, afterSteps); |
| 146 | |
} |
| 147 | 32 | Matcher findingBefore = compile(".*" + keywords.before() + "(.*)\\s*" , DOTALL).matcher(lifecycle); |
| 148 | 32 | if ( findingBefore.matches() ){ |
| 149 | 1 | List<String> beforeSteps = findSteps(startingWithNL(findingBefore.group(1).trim())); |
| 150 | 1 | List<String> afterSteps = new ArrayList<String>(); |
| 151 | 1 | return new Lifecycle(beforeSteps, afterSteps); |
| 152 | |
} |
| 153 | 31 | Matcher findingAfter = compile(".*" + keywords.after() + "(.*)\\s*" , DOTALL).matcher(lifecycle); |
| 154 | 31 | if ( findingAfter.matches() ){ |
| 155 | 1 | List<String> beforeSteps = new ArrayList<String>(); |
| 156 | 1 | List<String> afterSteps = findSteps(startingWithNL(findingAfter.group(1).trim())); |
| 157 | 1 | return new Lifecycle(beforeSteps, afterSteps); |
| 158 | |
} |
| 159 | 30 | return Lifecycle.EMPTY; |
| 160 | |
} |
| 161 | |
|
| 162 | |
private List<Scenario> parseScenariosFrom(String storyAsText) { |
| 163 | 34 | List<Scenario> parsed = new ArrayList<Scenario>(); |
| 164 | 34 | for (String scenarioAsText : splitScenarios(storyAsText)) { |
| 165 | 135 | parsed.add(parseScenario(scenarioAsText)); |
| 166 | 135 | } |
| 167 | 34 | return parsed; |
| 168 | |
} |
| 169 | |
|
| 170 | |
private List<String> splitScenarios(String storyAsText) { |
| 171 | 34 | List<String> scenarios = new ArrayList<String>(); |
| 172 | 34 | String scenarioKeyword = keywords.scenario(); |
| 173 | |
|
| 174 | |
|
| 175 | 34 | if (StringUtils.contains(storyAsText, scenarioKeyword)) { |
| 176 | 22 | storyAsText = StringUtils.substringAfter(storyAsText, scenarioKeyword); |
| 177 | |
} |
| 178 | |
|
| 179 | 171 | for (String scenarioAsText : storyAsText.split(scenarioKeyword)) { |
| 180 | 137 | if (scenarioAsText.trim().length() > 0) { |
| 181 | 135 | scenarios.add(scenarioKeyword + "\n" + scenarioAsText); |
| 182 | |
} |
| 183 | |
} |
| 184 | |
|
| 185 | 34 | return scenarios; |
| 186 | |
} |
| 187 | |
|
| 188 | |
private Scenario parseScenario(String scenarioAsText) { |
| 189 | 135 | String title = findScenarioTitle(scenarioAsText); |
| 190 | 135 | String scenarioWithoutKeyword = removeStart(scenarioAsText, keywords.scenario()).trim(); |
| 191 | 135 | String scenarioWithoutTitle = removeStart(scenarioWithoutKeyword, title); |
| 192 | 135 | scenarioWithoutTitle = startingWithNL(scenarioWithoutTitle); |
| 193 | 135 | Meta meta = findScenarioMeta(scenarioWithoutTitle); |
| 194 | 135 | ExamplesTable examplesTable = findExamplesTable(scenarioWithoutTitle); |
| 195 | 135 | GivenStories givenStories = findScenarioGivenStories(scenarioWithoutTitle); |
| 196 | 135 | if (givenStories.requireParameters()) { |
| 197 | 1 | givenStories.useExamplesTable(examplesTable); |
| 198 | |
} |
| 199 | 135 | List<String> steps = findSteps(scenarioWithoutTitle); |
| 200 | 135 | return new Scenario(title, meta, givenStories, examplesTable, steps); |
| 201 | |
} |
| 202 | |
|
| 203 | |
private String startingWithNL(String text) { |
| 204 | 141 | if ( !text.startsWith("\n") ){ |
| 205 | 28 | return "\n" + text; |
| 206 | |
} |
| 207 | 113 | return text; |
| 208 | |
} |
| 209 | |
|
| 210 | |
private String findScenarioTitle(String scenarioAsText) { |
| 211 | 135 | Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne().matcher(scenarioAsText); |
| 212 | 135 | return findingTitle.find() ? findingTitle.group(1).trim() : NONE; |
| 213 | |
} |
| 214 | |
|
| 215 | |
private Meta findScenarioMeta(String scenarioAsText) { |
| 216 | 135 | Matcher findingMeta = patternToPullScenarioMetaIntoGroupOne().matcher(scenarioAsText); |
| 217 | 135 | if (findingMeta.matches()) { |
| 218 | 5 | String meta = findingMeta.group(1).trim(); |
| 219 | 5 | return Meta.createMeta(meta, keywords); |
| 220 | |
} |
| 221 | 130 | return Meta.EMPTY; |
| 222 | |
} |
| 223 | |
|
| 224 | |
private ExamplesTable findExamplesTable(String scenarioAsText) { |
| 225 | 135 | Matcher findingTable = patternToPullExamplesTableIntoGroupOne().matcher(scenarioAsText); |
| 226 | 135 | String tableInput = findingTable.find() ? findingTable.group(1).trim() : NONE; |
| 227 | 135 | return tableFactory.createExamplesTable(tableInput); |
| 228 | |
} |
| 229 | |
|
| 230 | |
private GivenStories findScenarioGivenStories(String scenarioAsText) { |
| 231 | 135 | Matcher findingGivenStories = patternToPullScenarioGivenStoriesIntoGroupOne().matcher(scenarioAsText); |
| 232 | 135 | String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE; |
| 233 | 135 | return new GivenStories(givenStories); |
| 234 | |
} |
| 235 | |
|
| 236 | |
private List<String> findSteps(String scenarioAsText) { |
| 237 | 141 | Matcher matcher = patternToPullStepsIntoGroupOne().matcher(scenarioAsText); |
| 238 | 141 | List<String> steps = new ArrayList<String>(); |
| 239 | 141 | int startAt = 0; |
| 240 | 15230 | while (matcher.find(startAt)) { |
| 241 | 15089 | steps.add(StringUtils.substringAfter(matcher.group(1), "\n")); |
| 242 | 15089 | startAt = matcher.start(4); |
| 243 | |
} |
| 244 | 141 | return steps; |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
private Pattern patternToPullDescriptionIntoGroupOne() { |
| 250 | 34 | String metaOrNarrativeOrLifecycleOrScenario = concatenateWithOr(keywords.meta(), keywords.narrative(), keywords.lifecycle(), keywords.scenario()); |
| 251 | 34 | return compile("(.*?)(" + metaOrNarrativeOrLifecycleOrScenario + ").*", DOTALL); |
| 252 | |
} |
| 253 | |
|
| 254 | |
private Pattern patternToPullStoryMetaIntoGroupOne() { |
| 255 | 34 | String narrativeOrGivenStories = concatenateWithOr(keywords.narrative(), keywords.givenStories()); |
| 256 | 34 | return compile(".*" + keywords.meta() + "(.*?)\\s*(\\Z|" + narrativeOrGivenStories + ").*", DOTALL); |
| 257 | |
} |
| 258 | |
|
| 259 | |
private Pattern patternToPullNarrativeIntoGroupOne() { |
| 260 | 34 | String givenStoriesOrLifecycleOrScenario = concatenateWithOr(keywords.givenStories(), keywords.lifecycle(), keywords.scenario()); |
| 261 | 34 | return compile(".*" + keywords.narrative() + "(.*?)\\s*(" + givenStoriesOrLifecycleOrScenario + ").*", DOTALL); |
| 262 | |
} |
| 263 | |
|
| 264 | |
private Pattern patternToPullNarrativeElementsIntoGroups() { |
| 265 | 4 | return compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo() |
| 266 | |
+ "(.*)", DOTALL); |
| 267 | |
} |
| 268 | |
|
| 269 | |
private Pattern patternToPullAlternativeNarrativeElementsIntoGroups() { |
| 270 | 2 | return compile(".*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo() + "(.*)\\s*" + keywords.soThat() |
| 271 | |
+ "(.*)", DOTALL); |
| 272 | |
} |
| 273 | |
|
| 274 | |
private Pattern patternToPullStoryGivenStoriesIntoGroupOne() { |
| 275 | 34 | String lifecycleOrScenario = concatenateWithOr(keywords.lifecycle(), keywords.scenario()); |
| 276 | 34 | return compile(".*" + keywords.givenStories() + "(.*?)\\s*(\\Z|" + lifecycleOrScenario + ").*", DOTALL); |
| 277 | |
} |
| 278 | |
|
| 279 | |
private Pattern patternToPullLifecycleIntoGroupOne() { |
| 280 | 34 | return compile(".*" + keywords.lifecycle() + "\\s*(.*)", DOTALL); |
| 281 | |
} |
| 282 | |
|
| 283 | |
private Pattern patternToPullScenarioTitleIntoGroupOne() { |
| 284 | 135 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
| 285 | 135 | return compile(keywords.scenario() + "((.)*?)\\s*(" + keywords.meta() + "|" + startingWords + ").*", DOTALL); |
| 286 | |
} |
| 287 | |
|
| 288 | |
private Pattern patternToPullScenarioMetaIntoGroupOne() { |
| 289 | 135 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
| 290 | 135 | return compile(".*" + keywords.meta() + "(.*?)\\s*(" + keywords.givenStories() + "|" + startingWords + ").*", |
| 291 | |
DOTALL); |
| 292 | |
} |
| 293 | |
|
| 294 | |
private Pattern patternToPullScenarioGivenStoriesIntoGroupOne() { |
| 295 | 135 | String startingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
| 296 | 135 | return compile("\\n" + keywords.givenStories() + "((.|\\n)*?)\\s*(" + startingWords + ").*", DOTALL); |
| 297 | |
} |
| 298 | |
|
| 299 | |
private Pattern patternToPullStepsIntoGroupOne() { |
| 300 | 141 | String initialStartingWords = concatenateWithOr("\\n", "", keywords.startingWords()); |
| 301 | 141 | String followingStartingWords = concatenateWithOr("\\n", "\\s", keywords.startingWords()); |
| 302 | 141 | return compile( |
| 303 | |
"((" + initialStartingWords + ")\\s(.)*?)\\s*(\\Z|" + followingStartingWords + "|\\n" |
| 304 | |
+ keywords.examplesTable() + ")", DOTALL); |
| 305 | |
} |
| 306 | |
|
| 307 | |
private Pattern patternToPullExamplesTableIntoGroupOne() { |
| 308 | 135 | return compile("\\n" + keywords.examplesTable() + "\\s*(.*)", DOTALL); |
| 309 | |
} |
| 310 | |
|
| 311 | |
private String concatenateWithOr(String... keywords) { |
| 312 | 136 | return concatenateWithOr(null, null, keywords); |
| 313 | |
} |
| 314 | |
|
| 315 | |
private String concatenateWithOr(String beforeKeyword, String afterKeyword, String[] keywords) { |
| 316 | 823 | StringBuilder builder = new StringBuilder(); |
| 317 | 823 | String before = beforeKeyword != null ? beforeKeyword : NONE; |
| 318 | 823 | String after = afterKeyword != null ? afterKeyword : NONE; |
| 319 | 4642 | for (String keyword : keywords) { |
| 320 | 3819 | builder.append(before).append(keyword).append(after).append("|"); |
| 321 | |
} |
| 322 | 823 | return StringUtils.chomp(builder.toString(), "|"); |
| 323 | |
|
| 324 | |
} |
| 325 | |
|
| 326 | |
} |