- Revision
- 1020
- Author
- mauro
- Date
- 2008-12-26 12:18:57 -0600 (Fri, 26 Dec 2008)
Log Message
Better readability of regex patterns.
Modified Paths
Diff
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java (1019 => 1020)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2008-12-26 16:50:19 UTC (rev 1019) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2008-12-26 18:18:57 UTC (rev 1020) @@ -1,5 +1,9 @@ package org.jbehave.scenario.parser; +import static java.util.regex.Pattern.DOTALL; +import static java.util.regex.Pattern.compile; +import static org.jbehave.scenario.definition.Blurb.EMPTY; + import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -8,6 +12,7 @@ import org.jbehave.scenario.Configuration; import org.jbehave.scenario.PropertyBasedConfiguration; import org.jbehave.scenario.definition.Blurb; +import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.definition.ScenarioDefinition; import org.jbehave.scenario.definition.StoryDefinition; @@ -17,123 +22,136 @@ */ public class PatternScenarioParser implements ScenarioParser { - private final Configuration configuration; + private final Configuration configuration; - public PatternScenarioParser() { - this(new PropertyBasedConfiguration()); - } + public PatternScenarioParser() { + this(new PropertyBasedConfiguration()); + } - public PatternScenarioParser(Configuration configuration) { - this.configuration = configuration; - } + public PatternScenarioParser(Configuration configuration) { + this.configuration = configuration; + } - public StoryDefinition defineStoryFrom(String wholeStoryAsString) { - Blurb blurb = parseBlurbFrom(wholeStoryAsString); - List<ScenarioDefinition> scenarioDefinitions = parseScenariosFrom(wholeStoryAsString); - return new StoryDefinition(blurb, scenarioDefinitions); - } + public StoryDefinition defineStoryFrom(String wholeStoryAsString) { + Blurb blurb = parseBlurbFrom(wholeStoryAsString); + List<ScenarioDefinition> scenarioDefinitions = parseScenariosFrom(wholeStoryAsString); + return new StoryDefinition(blurb, scenarioDefinitions); + } - private List<ScenarioDefinition> parseScenariosFrom(String wholeStoryAsString) { - List<ScenarioDefinition> scenarioDefinitions = new ArrayList<ScenarioDefinition>(); - List<String> scenarios = splitScenarios(wholeStoryAsString); - for (String scenario : scenarios) { - Matcher findingTitle = patternToPullScenarioTitlesIntoGroupOne().matcher(scenario); - scenarioDefinitions.add(new ScenarioDefinition(findingTitle.find() ? findingTitle.group(1).trim() : "", - findSteps(scenario))); - } - return scenarioDefinitions; - } + private List<ScenarioDefinition> parseScenariosFrom( + String wholeStoryAsString) { + List<ScenarioDefinition> scenarioDefinitions = new ArrayList<ScenarioDefinition>(); + List<String> scenarios = splitScenarios(wholeStoryAsString); + for (String scenario : scenarios) { + Matcher findingTitle = patternToPullScenarioTitlesIntoGroupOne() + .matcher(scenario); + scenarioDefinitions.add(new ScenarioDefinition( + findingTitle.find() ? findingTitle.group(1).trim() : "", + findSteps(scenario))); + } + return scenarioDefinitions; + } - private List<String> findSteps(String scenarioAsString) { - Matcher matcher = patternToPullOutSteps().matcher(scenarioAsString); - List<String> steps = new ArrayList<String>(); - int startAt = 0; - while (matcher.find(startAt)) { - steps.add(matcher.group(1)); - startAt = matcher.start(4); - } + private List<String> findSteps(String scenarioAsString) { + Matcher matcher = patternToPullOutSteps().matcher(scenarioAsString); + List<String> steps = new ArrayList<String>(); + int startAt = 0; + while (matcher.find(startAt)) { + steps.add(matcher.group(1)); + startAt = matcher.start(4); + } - return steps; - } + return steps; + } - private Blurb parseBlurbFrom(String wholeStoryAsString) { - Pattern findStoryBlurb = Pattern.compile("(.*?)(" + configuration.keywords().scenario() + ").*", - Pattern.DOTALL); - Matcher matcher = findStoryBlurb.matcher(wholeStoryAsString); - if (matcher.find()) { - return new Blurb(matcher.group(1).trim()); - } else { - return Blurb.EMPTY; - } - } + private Blurb parseBlurbFrom(String wholeStoryAsString) { + String scenario = configuration.keywords().scenario(); + Pattern findStoryBlurb = compile("(.*?)(" + scenario + ").*", DOTALL); + Matcher matcher = findStoryBlurb.matcher(wholeStoryAsString); + if (matcher.find()) { + return new Blurb(matcher.group(1).trim()); + } else { + return EMPTY; + } + } - private List<String> splitScenarios(String allScenariosInFile) { - Pattern scenarioSplitter = patternToPullScenariosIntoGroupFour(); - Matcher matcher = scenarioSplitter.matcher(allScenariosInFile); - int startAt = 0; - List<String> scenarios = new ArrayList<String>(); - if (matcher.matches()) { - while (matcher.find(startAt)) { - scenarios.add(matcher.group(1)); - startAt = matcher.start(4); - } - } else { - String loneScenario = allScenariosInFile; - scenarios.add(loneScenario); - } - return scenarios; - } + private List<String> splitScenarios(String allScenariosInFile) { + Pattern scenarioSplitter = patternToPullScenariosIntoGroupFour(); + Matcher matcher = scenarioSplitter.matcher(allScenariosInFile); + int startAt = 0; + List<String> scenarios = new ArrayList<String>(); + if (matcher.matches()) { + while (matcher.find(startAt)) { + scenarios.add(matcher.group(1)); + startAt = matcher.start(4); + } + } else { + String loneScenario = allScenariosInFile; + scenarios.add(loneScenario); + } + return scenarios; + } - private Pattern patternToPullScenariosIntoGroupFour() { - return Pattern.compile(".*?((Scenario) (.|\\s)*?)\\s*(\\Z|Scenario).*".replace("Scenario", configuration - .keywords().scenario()), Pattern.DOTALL); - } + private Pattern patternToPullScenariosIntoGroupFour() { + String scenario = configuration.keywords().scenario(); + return compile(".*?((" + scenario + ") (.|\\s)*?)\\s*(\\Z|" + scenario + + ").*", DOTALL); + } - private Pattern patternToPullScenarioTitlesIntoGroupOne() { - String concatenatedKeywords = concatenateWithOr(configuration.keywords().given(), configuration.keywords() - .when(), configuration.keywords().then(), configuration.keywords().others()); - return Pattern.compile(configuration.keywords().scenario() + "(.*?)\\s*(" + concatenatedKeywords + ").*"); - } + private Pattern patternToPullScenarioTitlesIntoGroupOne() { + KeyWords keywords = configuration.keywords(); + String concatenatedKeywords = concatenateWithOr(keywords.given(), + keywords.when(), keywords.then(), keywords.others()); + String scenario = keywords.scenario(); + return compile(scenario + "(.*?)\\s*(" + concatenatedKeywords + ").*"); + } - private String concatenateWithOr(String given, String when, String then, String[] others) { - return concatenateWithOr(false, given, when, then, others); - } - - private String concatenateWithSpaceOr(String given, String when, String then, String[] others) { - return concatenateWithOr(true, given, when, then, others); - } - - private String concatenateWithOr(boolean usingSpace, String given, String when, String then, String[] others) { - StringBuilder builder = new StringBuilder(); - builder.append(given).append(usingSpace ? "\\s|" : "|"); - builder.append(when).append(usingSpace ? "\\s|" : "|"); - builder.append(then).append(usingSpace ? "\\s|" : "|"); - builder.append(usingSpace ? concatenateWithSpaceOr(others) : concatenateWithOr(others)); - return builder.toString(); - } + private String concatenateWithOr(String given, String when, String then, + String[] others) { + return concatenateWithOr(false, given, when, then, others); + } - private String concatenateWithOr(String... keywords) { - return concatenateWithOr(false, new StringBuilder(), keywords); - } - - private String concatenateWithSpaceOr(String... keywords) { - return concatenateWithOr(true, new StringBuilder(), keywords); - } + private String concatenateWithSpaceOr(String given, String when, + String then, String[] others) { + return concatenateWithOr(true, given, when, then, others); + } - private String concatenateWithOr(boolean usingSpace, StringBuilder builder, String[] keywords) { - for (String other : keywords) { - builder.append(other).append(usingSpace ? "\\s|" : "|"); - } - String result = builder.toString(); - return result.substring(0, result.length() - 1); // chop off the last | - } + private String concatenateWithOr(boolean usingSpace, String given, + String when, String then, String[] others) { + StringBuilder builder = new StringBuilder(); + builder.append(given).append(usingSpace ? "\\s|" : "|"); + builder.append(when).append(usingSpace ? "\\s|" : "|"); + builder.append(then).append(usingSpace ? "\\s|" : "|"); + builder.append(usingSpace ? concatenateWithSpaceOr(others) + : concatenateWithOr(others)); + return builder.toString(); + } - private Pattern patternToPullOutSteps() { - String givenWhenThen = concatenateWithOr(configuration.keywords().given(), configuration.keywords().when(), - configuration.keywords().then(), configuration.keywords().others()); - String givenWhenThenSpaced = concatenateWithSpaceOr(configuration.keywords().given(), configuration.keywords().when(), - configuration.keywords().then(), configuration.keywords().others()); - return Pattern.compile("((" + givenWhenThen + ") (.|\\s)*?)\\s*(\\Z|" + givenWhenThenSpaced + "|" - + configuration.keywords().scenario() + ")"); - } + private String concatenateWithOr(String... keywords) { + return concatenateWithOr(false, new StringBuilder(), keywords); + } + + private String concatenateWithSpaceOr(String... keywords) { + return concatenateWithOr(true, new StringBuilder(), keywords); + } + + private String concatenateWithOr(boolean usingSpace, StringBuilder builder, + String[] keywords) { + for (String other : keywords) { + builder.append(other).append(usingSpace ? "\\s|" : "|"); + } + String result = builder.toString(); + return result.substring(0, result.length() - 1); // chop off the last | + } + + private Pattern patternToPullOutSteps() { + KeyWords keywords = configuration.keywords(); + String givenWhenThen = concatenateWithOr(keywords.given(), keywords + .when(), keywords.then(), keywords.others()); + String givenWhenThenSpaced = concatenateWithSpaceOr(keywords.given(), + keywords.when(), keywords.then(), keywords.others()); + String scenario = keywords.scenario(); + return compile("((" + givenWhenThen + ") (.|\\s)*?)\\s*(\\Z|" + + givenWhenThenSpaced + "|" + scenario + ")"); + } }
To unsubscribe from this list please visit:
