| 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 | 235 | private String wordToRemove = ""; |
| 40 | |
|
| 41 | |
public UnderscoredCamelCaseResolver() { |
| 42 | 218 | this(DEFAULT_EXTENSION); |
| 43 | 218 | } |
| 44 | |
|
| 45 | |
public UnderscoredCamelCaseResolver(String extension) { |
| 46 | 234 | this(extension, NUMBERS_AS_LOWER_CASE_LETTERS_PATTERN); |
| 47 | 234 | } |
| 48 | |
|
| 49 | |
public UnderscoredCamelCaseResolver(String extension, |
| 50 | |
String resolutionPattern) { |
| 51 | 235 | super(extension); |
| 52 | 235 | this.resolutionPattern = resolutionPattern; |
| 53 | 235 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
protected String resolveName( |
| 57 | |
Class<? extends Embeddable> embeddableClass) { |
| 58 | 50 | String simpleName = embeddableClass.getSimpleName(); |
| 59 | 50 | simpleName = simpleName.replace(wordToRemove, ""); |
| 60 | 50 | Matcher matcher = compile(resolutionPattern).matcher( |
| 61 | |
simpleName); |
| 62 | 50 | int startAt = 0; |
| 63 | 50 | StringBuilder builder = new StringBuilder(); |
| 64 | 182 | while (matcher.find(startAt)) { |
| 65 | 132 | builder.append(matcher.group(1).toLowerCase()); |
| 66 | 132 | builder.append(UNDERSCORE); |
| 67 | 132 | startAt = matcher.start(2); |
| 68 | |
} |
| 69 | 50 | 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 | |
} |