001
014
015 package com.liferay.util;
016
017 import com.liferay.portal.kernel.util.StringPool;
018 import com.liferay.portal.kernel.util.StringUtil;
019 import com.liferay.portal.kernel.util.Validator;
020
021 import java.text.NumberFormat;
022
023 import java.util.Locale;
024
025
028 public class TextFormatter {
029
030
031
032 public static final int A = 0;
033
034
035
036 public static final int B = 1;
037
038
039
040 public static final int C = 2;
041
042
043
044 public static final int D = 3;
045
046
047
048 public static final int E = 4;
049
050
051
052 public static final int F = 5;
053
054
055
056 public static final int G = 6;
057
058
059
060 public static final int H = 7;
061
062
063
064 public static final int I = 8;
065
066
067
068 public static final int J = 9;
069
070
071
072 public static final int K = 10;
073
074
075
076 public static final int L = 11;
077
078
079
080 public static final int M = 12;
081
082
083
084 public static final int N = 13;
085
086
087
088 public static final int O = 14;
089
090 public static String format(String s, int style) {
091 if (Validator.isNull(s)) {
092 return null;
093 }
094
095 s = s.trim();
096
097 if (style == A) {
098 return _formatA(s);
099 }
100 else if (style == B) {
101 return _formatB(s);
102 }
103 else if (style == C) {
104 return _formatC(s);
105 }
106 else if (style == D) {
107 return _formatD(s);
108 }
109 else if (style == E) {
110 return _formatE(s);
111 }
112 else if (style == F) {
113 return _formatF(s);
114 }
115 else if (style == G) {
116 return _formatG(s);
117 }
118 else if (style == H) {
119 return _formatH(s);
120 }
121 else if (style == I) {
122 return _formatI(s);
123 }
124 else if (style == J) {
125 return _formatJ(s);
126 }
127 else if (style == K) {
128 return _formatK(s);
129 }
130 else if (style == L) {
131 return _formatL(s);
132 }
133 else if (style == M) {
134 return _formatM(s);
135 }
136 else if (style == N) {
137 return _formatN(s);
138 }
139 else if (style == O) {
140 return _formatO(s);
141 }
142 else {
143 return s;
144 }
145 }
146
147 public static String formatKB(double size, Locale locale) {
148 NumberFormat numberFormat = NumberFormat.getInstance(locale);
149
150 numberFormat.setMaximumFractionDigits(1);
151 numberFormat.setMinimumFractionDigits(1);
152
153 return numberFormat.format(size / 1024.0);
154 }
155
156 public static String formatKB(int size, Locale locale) {
157 return formatKB((double)size, locale);
158 }
159
160 public static String formatName(String name) {
161 if (Validator.isNull(name)) {
162 return name;
163 }
164
165 char[] charArray = name.toLowerCase().trim().toCharArray();
166
167 if (charArray.length > 0) {
168 charArray[0] = Character.toUpperCase(charArray[0]);
169 }
170
171 for (int i = 0; i < charArray.length; i++) {
172 if (charArray[i] == ' ') {
173 charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
174 }
175 }
176
177 return new String(charArray);
178 }
179
180 public static String formatPlural(String s) {
181 if (Validator.isNull(s)) {
182 return s;
183 }
184
185 if (s.endsWith("s")) {
186 s = s.substring(0, s.length() -1) + "ses";
187 }
188 else if (s.endsWith("y")) {
189 s = s.substring(0, s.length() -1) + "ies";
190 }
191 else {
192 s = s + "s";
193 }
194
195 return s;
196 }
197
198 private static String _formatA(String s) {
199 return StringUtil.replace(
200 s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
201 }
202
203 private static String _formatB(String s) {
204 return StringUtil.replace(
205 s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
206 }
207
208 private static String _formatC(String s) {
209 return StringUtil.replace(
210 s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
211 }
212
213 private static String _formatD(String s) {
214 return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
215 }
216
217 private static String _formatE(String s) {
218 return s.toLowerCase();
219 }
220
221 private static String _formatF(String s) {
222 s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
223 s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
224
225 return s;
226 }
227
228 private static String _formatG(String s) {
229 return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
230 }
231
232 private static String _formatH(String s) {
233 char[] charArray = s.toCharArray();
234
235 StringBuilder sb = new StringBuilder(charArray.length * 2);
236
237 for (int i = 0; i < charArray.length; i++) {
238 if (Character.isUpperCase(charArray[i])) {
239 sb.append(StringPool.SPACE);
240 sb.append(Character.toLowerCase(charArray[i]));
241 }
242 else {
243 sb.append(charArray[i]);
244 }
245 }
246
247 return sb.toString().trim();
248 }
249
250 private static String _formatI(String s) {
251 if (s.length() == 1) {
252 return s.toLowerCase();
253 }
254
255 if (Character.isUpperCase(s.charAt(0)) &&
256 Character.isLowerCase(s.charAt(1))) {
257
258 return Character.toLowerCase(s.charAt(0)) +
259 s.substring(1, s.length());
260 }
261
262 char[] charArray = s.toCharArray();
263
264 StringBuilder sb = new StringBuilder(charArray.length);
265
266 for (int i = 0; i < charArray.length; i++) {
267 if ((i + 1 != charArray.length) &&
268 (Character.isLowerCase(charArray[i + 1]))) {
269
270 sb.append(s.substring(i, charArray.length));
271
272 break;
273 }
274 else {
275 sb.append(Character.toLowerCase(charArray[i]));
276 }
277 }
278
279 return sb.toString();
280 }
281
282 private static String _formatJ(String s) {
283 s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
284 s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
285
286 char[] charArray = s.toCharArray();
287
288 StringBuilder sb = new StringBuilder(charArray.length);
289
290 for (int i = 0; i < charArray.length; i++) {
291 if ((i == 0) || (charArray[i - 1] == ' ')) {
292 sb.append(Character.toUpperCase(charArray[i]));
293 }
294 else {
295 sb.append(Character.toLowerCase(charArray[i]));
296 }
297 }
298
299 return sb.toString();
300 }
301
302 private static String _formatK(String s) {
303 s = _formatH(s);
304 s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
305
306 return s;
307 }
308
309 private static String _formatL(String s) {
310 if (s.length() == 1) {
311 return s.toLowerCase();
312 }
313 else if (Character.isUpperCase(s.charAt(0)) &&
314 Character.isUpperCase(s.charAt(1))) {
315
316 return s;
317 }
318 else {
319 return Character.toLowerCase(s.charAt(0)) + s.substring(1);
320 }
321 }
322
323 private static String _formatM(String s) {
324 char[] charArray = s.toCharArray();
325
326 StringBuilder sb = new StringBuilder(charArray.length);
327
328 for (int i = 0; i < charArray.length; i++) {
329 if (charArray[i] == '-') {
330 }
331 else if ((i > 0) && (charArray[i - 1] == '-')) {
332 sb.append(Character.toUpperCase(charArray[i]));
333 }
334 else {
335 sb.append(charArray[i]);
336 }
337 }
338
339 return sb.toString();
340 }
341
342 private static String _formatN(String s) {
343 return StringUtil.replace(s, StringPool.DASH, StringPool.UNDERLINE);
344 }
345
346 private static String _formatO(String s) {
347 return StringUtil.replace(s, StringPool.UNDERLINE, StringPool.DASH);
348 }
349
350 }