01: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
02: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
03: // Released under the terms of the GNU General Public License version 2 or later.
04: // Copyright (C) 2003,2004 by Object Mentor, Inc. All rights reserved.
05: // Released under the terms of the GNU General Public License version 2 or
06: // later.
07: package fit;
08:
09: import java.util.regex.*;
10:
11: public class GracefulNamer {
12: private static Pattern disgracefulNamePattern = Pattern
13: .compile("\\w(?:[.]|\\w)*[^.]");
14:
15: static boolean isGracefulName(String fixtureName) {
16: Matcher matcher = disgracefulNamePattern.matcher(fixtureName);
17: return !matcher.matches();
18: }
19:
20: static String disgrace(String fixtureName) {
21: GracefulNamer namer = new GracefulNamer();
22:
23: for (int i = 0; i < fixtureName.length(); i++) {
24: char c = fixtureName.charAt(i);
25: if (Character.isLetter(c))
26: namer.currentState.letter(c);
27: else if (Character.isDigit(c))
28: namer.currentState.digit(c);
29: else
30: namer.currentState.other(c);
31: }
32:
33: return namer.finalName.toString();
34: }
35:
36: private StringBuffer finalName = new StringBuffer();
37:
38: private GracefulNameState currentState = new OutOfWordState();
39:
40: private GracefulNamer() {
41: }
42:
43: private interface GracefulNameState {
44: public void letter(char c);
45:
46: public void digit(char c);
47:
48: public void other(char c);
49: }
50:
51: private class InWordState implements GracefulNameState {
52: public void letter(char c) {
53: finalName.append(c);
54: }
55:
56: public void digit(char c) {
57: finalName.append(c);
58: currentState = new InNumberState();
59: }
60:
61: public void other(char c) {
62: currentState = new OutOfWordState();
63: }
64: }
65:
66: private class InNumberState implements GracefulNameState {
67: public void letter(char c) {
68: finalName.append(Character.toUpperCase(c));
69: currentState = new InWordState();
70: }
71:
72: public void digit(char c) {
73: finalName.append(c);
74: }
75:
76: public void other(char c) {
77: currentState = new OutOfWordState();
78: }
79: }
80:
81: private class OutOfWordState implements GracefulNameState {
82: public void letter(char c) {
83: finalName.append(Character.toUpperCase(c));
84: currentState = new InWordState();
85: }
86:
87: public void digit(char c) {
88: finalName.append(c);
89: currentState = new InNumberState();
90: }
91:
92: public void other(char c) {
93: }
94: }
95: }
|