1 | |
package org.jbehave.core.parsers; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
import java.util.regex.Matcher; |
6 | |
import java.util.regex.Pattern; |
7 | |
|
8 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
9 | |
import org.apache.commons.lang.builder.ToStringStyle; |
10 | |
import org.jbehave.core.steps.StepType; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | 120 | public class RegexPrefixCapturingPatternParser implements StepPatternParser { |
20 | |
|
21 | |
private final String prefix; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public RegexPrefixCapturingPatternParser() { |
28 | 484 | this("$"); |
29 | 484 | } |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 485 | public RegexPrefixCapturingPatternParser(String prefix) { |
38 | 485 | this.prefix = prefix; |
39 | 485 | } |
40 | |
|
41 | |
public String getPrefix(){ |
42 | 1 | return prefix; |
43 | |
} |
44 | |
|
45 | |
public StepMatcher parseStep(StepType stepType, String stepPattern) { |
46 | 101 | return new RegexStepMatcher(stepType, stepPattern, |
47 | |
buildPattern(stepPattern), extractParameterNames(stepPattern)); |
48 | |
} |
49 | |
|
50 | |
private Pattern buildPattern(String stepPattern) { |
51 | 101 | String matchThisButLeaveBrackets = escapeRegexPunctuation(stepPattern); |
52 | 101 | String patternToMatchAgainst = replaceParametersWithCapture( |
53 | |
matchThisButLeaveBrackets, findParametersToReplace(matchThisButLeaveBrackets)); |
54 | 101 | String matchThisButIgnoreWhitespace = anyWhitespaceWillDo(patternToMatchAgainst); |
55 | 101 | return Pattern.compile(matchThisButIgnoreWhitespace, Pattern.DOTALL); |
56 | |
} |
57 | |
|
58 | |
private String escapeRegexPunctuation(String matchThis) { |
59 | 101 | return matchThis.replaceAll("([\\[\\]\\{\\}\\?\\^\\.\\*\\(\\)\\+\\\\])", "\\\\$1"); |
60 | |
} |
61 | |
|
62 | |
private String anyWhitespaceWillDo(String matchThis) { |
63 | 101 | return matchThis.replaceAll("\\s+", "\\\\s+"); |
64 | |
} |
65 | |
|
66 | |
private String[] extractParameterNames(String stepPattern) { |
67 | 101 | List<String> names = new ArrayList<String>(); |
68 | 101 | for (Parameter parameter : findParametersToReplace(stepPattern)) { |
69 | 60 | names.add(parameter.name); |
70 | |
} |
71 | 101 | return names.toArray(new String[names.size()]); |
72 | |
} |
73 | |
|
74 | |
private List<Parameter> findParametersToReplace(String matchThisButLeaveBrackets) { |
75 | 202 | List<Parameter> parameters = new ArrayList<Parameter>(); |
76 | 202 | Matcher findingAllPrefixedWords = findAllPrefixedWords().matcher(matchThisButLeaveBrackets); |
77 | 322 | while (findingAllPrefixedWords.find()) { |
78 | 120 | parameters.add(new Parameter(matchThisButLeaveBrackets, findingAllPrefixedWords.start(), |
79 | |
findingAllPrefixedWords.end(), findingAllPrefixedWords.group(2))); |
80 | |
} |
81 | 202 | return parameters; |
82 | |
} |
83 | |
|
84 | |
private Pattern findAllPrefixedWords() { |
85 | 202 | return Pattern.compile("(\\" + prefix + "\\w*)(\\W|\\Z)", Pattern.DOTALL); |
86 | |
} |
87 | |
|
88 | |
private String replaceParametersWithCapture(String escapedMatch, |
89 | |
List<Parameter> parameters) { |
90 | 101 | String replaced = escapedMatch; |
91 | 161 | for (int i = parameters.size(); i > 0; i--) { |
92 | 60 | String start = replaced.substring(0, parameters.get(i - 1).start); |
93 | 60 | String end = replaced.substring(parameters.get(i - 1).end); |
94 | 60 | String whitespaceIfAny = parameters.get(i - 1).whitespaceIfAny; |
95 | 60 | replaced = start + "(.*)" + whitespaceIfAny + end; |
96 | |
} |
97 | 101 | return replaced; |
98 | |
} |
99 | |
|
100 | 240 | private class Parameter { |
101 | |
private final int start; |
102 | |
private final int end; |
103 | |
private final String whitespaceIfAny; |
104 | |
private final String name; |
105 | |
|
106 | |
public Parameter(String pattern, int start, int end, |
107 | 120 | String whitespaceIfAny) { |
108 | 120 | this.start = start; |
109 | 120 | this.end = end; |
110 | 120 | this.whitespaceIfAny = whitespaceIfAny; |
111 | 120 | this.name = pattern.substring(start + prefix.length(), end).trim(); |
112 | 120 | } |
113 | |
|
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public String toString() { |
118 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
119 | |
} |
120 | |
} |