01: /*
02: * Author: Chris Seguin
03: *
04: * This software has been developed under the copyleft
05: * rules of the GNU General Public License. Please
06: * consult the GNU General Public License for more
07: * details about use and distribution of this software.
08: */
09: package org.acm.seguin.pretty.ai;
10:
11: /**
12: * Parses a variable name into different words
13: *
14: *@author Chris Seguin
15: */
16: public class ParseVariableName {
17: private final static int INITIAL = 0;
18: private final static int NORMAL = 1;
19: private final static int ACRONYM = 2;
20:
21: /**
22: * This breaks words around capitalization except when there are
23: * capitalization occurs two or more times in a row.
24: *
25: *@param value the string to break
26: *@return the broken string
27: */
28: public String parse(String value) {
29: StringBuffer buffer = new StringBuffer();
30: int state = INITIAL;
31:
32: int last = value.length();
33: for (int ndx = 0; ndx < last; ndx++) {
34: char ch = value.charAt(ndx);
35: if (Character.isUpperCase(ch)) {
36: if (state == NORMAL) {
37: buffer.append(" ");
38: }
39:
40: if (ndx + 1 == last) {
41: // Don't adjust state
42: } else if (Character.isUpperCase(value.charAt(ndx + 1))) {
43: state = ACRONYM;
44: } else {
45: if (state == ACRONYM)
46: buffer.append(" ");
47: state = NORMAL;
48: }
49:
50: if (state == ACRONYM) {
51: buffer.append(ch);
52: } else {
53: buffer.append(Character.toLowerCase(ch));
54: }
55: } else {
56: buffer.append(ch);
57: state = NORMAL;
58: }
59: }
60:
61: return buffer.toString();
62: }
63: }
|