1 | |
package org.jbehave.core.io; |
2 | |
|
3 | |
import static java.util.regex.Pattern.compile; |
4 | |
|
5 | |
import java.util.regex.Matcher; |
6 | |
|
7 | |
import org.jbehave.core.Embeddable; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class UnderscoredCamelCaseResolver extends AbstractStoryPathResolver { |
34 | |
|
35 | |
public static final String NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN = "([A-Z].*?)([A-Z]|\\z)"; |
36 | |
public static final String NUMBERS_AS_UPPER_CASE_LETTERS_PATTERN = "([A-Z0-9].*?)([A-Z0-9]|\\z)"; |
37 | |
private static final String UNDERSCORE = "_"; |
38 | |
private final String resolutionPattern; |
39 | 220 | private String wordToRemove = ""; |
40 | |
|
41 | |
public UnderscoredCamelCaseResolver() { |
42 | 203 | this(DEFAULT_EXTENSION); |
43 | 203 | } |
44 | |
|
45 | |
public UnderscoredCamelCaseResolver(String extension) { |
46 | 219 | this(extension, NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN); |
47 | 219 | } |
48 | |
|
49 | |
public UnderscoredCamelCaseResolver(String extension, |
50 | |
String resolutionPattern) { |
51 | 220 | super(extension); |
52 | 220 | this.resolutionPattern = resolutionPattern; |
53 | 220 | } |
54 | |
|
55 | |
@Override |
56 | |
protected String resolveName( |
57 | |
Class<? extends Embeddable> embeddableClass) { |
58 | 46 | String simpleName = embeddableClass.getSimpleName(); |
59 | 46 | simpleName = simpleName.replace(wordToRemove, ""); |
60 | 46 | Matcher matcher = compile(resolutionPattern).matcher( |
61 | |
simpleName); |
62 | 46 | int startAt = 0; |
63 | 46 | StringBuilder builder = new StringBuilder(); |
64 | 168 | while (matcher.find(startAt)) { |
65 | 122 | builder.append(matcher.group(1).toLowerCase()); |
66 | 122 | builder.append(UNDERSCORE); |
67 | 122 | startAt = matcher.start(2); |
68 | |
} |
69 | 46 | return builder.substring(0, builder.length() - 1); |
70 | |
} |
71 | |
|
72 | |
public StoryPathResolver removeFromClassName(String wordToRemove) { |
73 | 1 | this.wordToRemove = wordToRemove; |
74 | 1 | return this; |
75 | |
} |
76 | |
} |