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