1 | |
package org.jbehave.core.io; |
2 | |
|
3 | |
import java.util.Locale; |
4 | |
import java.util.regex.Matcher; |
5 | |
|
6 | |
import org.jbehave.core.Embeddable; |
7 | |
|
8 | |
import static java.util.regex.Pattern.compile; |
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 | |
|
34 | |
public class UnderscoredCamelCaseResolver extends AbstractStoryPathResolver { |
35 | |
|
36 | |
public static final String NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN = "([A-Z].*?)([A-Z]|\\z)"; |
37 | |
public static final String NUMBERS_AS_UPPER_CASE_LETTERS_PATTERN = "([A-Z0-9].*?)([A-Z0-9]|\\z)"; |
38 | |
private static final String UNDERSCORE = "_"; |
39 | |
private final String resolutionPattern; |
40 | |
private final Locale locale; |
41 | 252 | private String wordToRemove = ""; |
42 | |
|
43 | |
public UnderscoredCamelCaseResolver() { |
44 | 234 | this(DEFAULT_EXTENSION); |
45 | 234 | } |
46 | |
|
47 | |
public UnderscoredCamelCaseResolver(String extension) { |
48 | 251 | this(extension, NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN); |
49 | 251 | } |
50 | |
|
51 | |
public UnderscoredCamelCaseResolver(String extension, |
52 | |
String resolutionPattern) { |
53 | 252 | this(extension, resolutionPattern, Locale.getDefault()); |
54 | 252 | } |
55 | |
|
56 | |
public UnderscoredCamelCaseResolver(String extension, |
57 | |
String resolutionPattern, Locale locale) { |
58 | 252 | super(extension); |
59 | 252 | this.resolutionPattern = resolutionPattern; |
60 | 252 | this.locale = locale; |
61 | 252 | } |
62 | |
|
63 | |
@Override |
64 | |
protected String resolveName( |
65 | |
Class<? extends Embeddable> embeddableClass) { |
66 | 51 | String simpleName = embeddableClass.getSimpleName(); |
67 | 51 | simpleName = simpleName.replace(wordToRemove, ""); |
68 | 51 | Matcher matcher = compile(resolutionPattern).matcher( |
69 | |
simpleName); |
70 | 51 | int startAt = 0; |
71 | 51 | StringBuilder builder = new StringBuilder(); |
72 | 185 | while (matcher.find(startAt)) { |
73 | 134 | builder.append(matcher.group(1).toLowerCase(locale)); |
74 | 134 | builder.append(UNDERSCORE); |
75 | 134 | startAt = matcher.start(2); |
76 | |
} |
77 | 51 | return builder.substring(0, builder.length() - 1); |
78 | |
} |
79 | |
|
80 | |
public StoryPathResolver removeFromClassName(String wordToRemove) { |
81 | 1 | this.wordToRemove = wordToRemove; |
82 | 1 | return this; |
83 | |
} |
84 | |
} |