1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.jaggenerator;
19
20 import java.text.StringCharacterIterator;
21
22 /***
23 *
24 * @author hillie
25 */
26 public class Utils {
27
28 public static String toClassName(String unformatted) {
29 try {
30 if (unformatted.startsWith("T_")) {
31 unformatted = unformatted.substring(2);
32 }
33 String s = format(unformatted);
34 s = initCap(s);
35 return s;
36 } catch (Exception e) {
37 return unformatted;
38 }
39 }
40
41 /***
42 * Format the name by only allowing lowercase.
43 */
44 public static String formatLowercase(String unformatted) {
45 try {
46 if (unformatted == null) {
47 return null;
48 }
49 StringBuffer sb = new StringBuffer(unformatted.toLowerCase());
50 StringBuffer formattedString = new StringBuffer();
51
52 for (int i = 0; i < sb.length(); i++) {
53 char c = sb.charAt(i);
54 if (c >= 'a' && c <= 'z') {
55 formattedString.append(c);
56 }
57 }
58 return formattedString.toString();
59 } catch (Exception e) {
60 return unformatted;
61 }
62 }
63
64 /***
65 * Format the name by only allowing lowercase and uppercase and
66 * always start with uppercase.
67 */
68 public static String formatLowerAndUpperCase(String unformatted) {
69 try {
70 if (unformatted == null) {
71 return null;
72 }
73 StringBuffer sb = new StringBuffer(unformatted);
74 StringBuffer formattedString = new StringBuffer();
75
76 for (int i = 0; i < sb.length(); i++) {
77 char c = sb.charAt(i);
78 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
79 formattedString.append(c);
80 }
81 }
82 return firstToUpperCase(formattedString.toString());
83 } catch (Exception e) {
84 return unformatted;
85 }
86 }
87
88 public static String format(String unformatted) {
89 try {
90 StringBuffer sb = new StringBuffer(unformatted.toLowerCase());
91
92 int i = sb.toString().indexOf("_");
93 while (i >= 0) {
94 if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase());
95 i = sb.toString().indexOf("_");
96 }
97
98 i = sb.toString().indexOf("-");
99 while (i >= 0) {
100 if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase());
101 i = sb.toString().indexOf("-");
102 }
103 return sb.toString();
104 } catch (Exception e) {
105 return unformatted;
106 }
107 }
108
109 /***
110 * Undoes a format. Everything is capitalised and any uppercase character
111 * forces a '_' to be prefixed, e.g. "theColumnName" is unformatted to "THE_COLUMN_NAME", and
112 * "theATrain" unformats to "THE_A_TRAIN".
113 * @param formatted
114 * @return the unformatted String.
115 */
116 public static String unformat(String formatted) {
117 StringBuffer result = new StringBuffer();
118 for (int i = 0; i < formatted.length(); i++) {
119 if (Character.isUpperCase(formatted.charAt(i))) {
120 result.append('_');
121 }
122 result.append(Character.toUpperCase(formatted.charAt(i)));
123 }
124 return result.toString();
125 }
126
127 public static String initCap(String unformatted) {
128 StringBuffer sb = new StringBuffer(unformatted);
129
130 if ((unformatted != null) && (unformatted.length() > 0)) {
131 sb.replace(0, 1, sb.substring(0, 1).toUpperCase());
132 return sb.toString();
133 } else
134 return unformatted;
135 }
136
137 public static String formatFKName(String fkColumnName) {
138 return format(fkColumnName) + "FK";
139 }
140
141 public static String firstToLowerCase(String text) {
142 return (text == null || text.length() == 0) ? "" : Character.toLowerCase(text.charAt(0)) +
143 (text.length() > 1 ? text.substring(1) : "");
144 }
145
146 public static String firstToUpperCase(String text) {
147 return (text == null || text.length() == 0) ? "" : Character.toUpperCase(text.charAt(0)) +
148 (text.length() > 1 ? text.substring(1) : "");
149 }
150
151 }