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