001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.text.NumberFormat;
018    
019    import java.util.Locale;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class TextFormatter {
025    
026            // Web Search --> WEB_SEARCH
027    
028            public static final int A = 0;
029    
030            // Web Search --> websearch
031    
032            public static final int B = 1;
033    
034            // Web Search --> web_search
035    
036            public static final int C = 2;
037    
038            // Web Search --> WebSearch
039    
040            public static final int D = 3;
041    
042            // Web Search --> web search
043    
044            public static final int E = 4;
045    
046            // Web Search --> webSearch
047    
048            public static final int F = 5;
049    
050            // formatId --> FormatId
051    
052            public static final int G = 6;
053    
054            // formatId --> format id
055    
056            public static final int H = 7;
057    
058            // FormatId --> formatId
059    
060            public static final int I = 8;
061    
062            // format-id --> Format Id
063    
064            public static final int J = 9;
065    
066            // formatId --> format-id, formatID --> format-i-d
067    
068            public static final int K = 10;
069    
070            // FormatId --> formatId, FOrmatId --> FOrmatId
071    
072            public static final int L = 11;
073    
074            // format-id --> formatId
075    
076            public static final int M = 12;
077    
078            // format-id --> format_id
079    
080            public static final int N = 13;
081    
082            // format_id --> format-id
083    
084            public static final int O = 14;
085    
086            // FormatID --> format-id
087    
088            public static final int P = 15;
089    
090            // FORMATId --> format-id
091    
092            public static final int Q = 16;
093    
094            public static String format(String s, int style) {
095                    if (Validator.isNull(s)) {
096                            return null;
097                    }
098    
099                    s = s.trim();
100    
101                    if (style == A) {
102                            return _formatA(s);
103                    }
104                    else if (style == B) {
105                            return _formatB(s);
106                    }
107                    else if (style == C) {
108                            return _formatC(s);
109                    }
110                    else if (style == D) {
111                            return _formatD(s);
112                    }
113                    else if (style == E) {
114                            return _formatE(s);
115                    }
116                    else if (style == F) {
117                            return _formatF(s);
118                    }
119                    else if (style == G) {
120                            return _formatG(s);
121                    }
122                    else if (style == H) {
123                            return _formatH(s);
124                    }
125                    else if (style == I) {
126                            return _formatI(s);
127                    }
128                    else if (style == J) {
129                            return _formatJ(s);
130                    }
131                    else if (style == K) {
132                            return _formatK(s);
133                    }
134                    else if (style == L) {
135                            return _formatL(s);
136                    }
137                    else if (style == M) {
138                            return _formatM(s);
139                    }
140                    else if (style == N) {
141                            return _formatN(s);
142                    }
143                    else if (style == O) {
144                            return _formatO(s);
145                    }
146                    else if (style == P) {
147                            return _formatP(s);
148                    }
149                    else if (style == Q) {
150                            return _formatQ(s);
151                    }
152                    else {
153                            return s;
154                    }
155            }
156    
157            public static String formatName(String name) {
158                    if (Validator.isNull(name)) {
159                            return name;
160                    }
161    
162                    char[] chars = StringUtil.toLowerCase(name).trim().toCharArray();
163    
164                    if (chars.length > 0) {
165                            chars[0] = Character.toUpperCase(chars[0]);
166                    }
167    
168                    for (int i = 0; i < chars.length; i++) {
169                            if (chars[i] == ' ') {
170                                    chars[i + 1] = Character.toUpperCase(chars[i + 1]);
171                            }
172                    }
173    
174                    return new String(chars);
175            }
176    
177            public static String formatPlural(String s) {
178                    if (Validator.isNull(s)) {
179                            return s;
180                    }
181    
182                    if (s.endsWith("s")) {
183                            s = s.substring(0, s.length() -1) + "ses";
184                    }
185                    else if (s.endsWith("y")) {
186                            s = s.substring(0, s.length() -1) + "ies";
187                    }
188                    else {
189                            s = s + "s";
190                    }
191    
192                    return s;
193            }
194    
195            public static String formatStorageSize(double size, Locale locale) {
196                    String suffix = _STORAGE_SIZE_SUFFIX_KB;
197    
198                    size = size / _STORAGE_SIZE_DENOMINATOR;
199    
200                    if (size >= _STORAGE_SIZE_DENOMINATOR) {
201                            suffix = _STORAGE_SIZE_SUFFIX_MB;
202    
203                            size = size / _STORAGE_SIZE_DENOMINATOR;
204                    }
205    
206                    if (size >= _STORAGE_SIZE_DENOMINATOR) {
207                            suffix = _STORAGE_SIZE_SUFFIX_GB;
208    
209                            size = size / _STORAGE_SIZE_DENOMINATOR;
210                    }
211    
212                    NumberFormat numberFormat = NumberFormat.getInstance(locale);
213    
214                    if (suffix.equals(_STORAGE_SIZE_SUFFIX_KB)) {
215                            numberFormat.setMaximumFractionDigits(0);
216                    }
217                    else {
218                            numberFormat.setMaximumFractionDigits(1);
219                    }
220    
221                    numberFormat.setMinimumFractionDigits(0);
222    
223                    return numberFormat.format(size) + suffix;
224            }
225    
226            public static String formatStorageSize(int size, Locale locale) {
227                    return formatStorageSize((double)size, locale);
228            }
229    
230            private static String _formatA(String s) {
231                    return StringUtil.replace(
232                            StringUtil.toUpperCase(s), CharPool.SPACE, CharPool.UNDERLINE);
233            }
234    
235            private static String _formatB(String s) {
236                    return StringUtil.strip(StringUtil.toLowerCase(s), CharPool.SPACE);
237            }
238    
239            private static String _formatC(String s) {
240                    return StringUtil.replace(
241                            StringUtil.toLowerCase(s), CharPool.SPACE, CharPool.UNDERLINE);
242            }
243    
244            private static String _formatD(String s) {
245                    return StringUtil.strip(s, CharPool.SPACE);
246            }
247    
248            private static String _formatE(String s) {
249                    return StringUtil.toLowerCase(s);
250            }
251    
252            private static String _formatF(String s) {
253                    s = StringUtil.strip(s, CharPool.SPACE);
254    
255                    if (Character.isUpperCase(s.charAt(0))) {
256                            s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
257                                    s.substring(1));
258                    }
259    
260                    return s;
261            }
262    
263            private static String _formatG(String s) {
264                    if (Character.isLowerCase(s.charAt(0))) {
265                            s = StringUtil.toUpperCase(s.substring(0, 1)).concat(
266                                    s.substring(1));
267                    }
268    
269                    return s;
270            }
271    
272            private static String _formatH(String s) {
273                    StringBuilder sb = new StringBuilder(s.length() * 2);
274    
275                    for (int i = 0; i < s.length(); i++) {
276                            char c = s.charAt(i);
277    
278                            if (Character.isUpperCase(c)) {
279                                    sb.append(CharPool.SPACE);
280                                    sb.append(Character.toLowerCase(c));
281                            }
282                            else {
283                                    sb.append(c);
284                            }
285                    }
286    
287                    return sb.toString().trim();
288            }
289    
290            private static String _formatI(String s) {
291                    if (s.length() == 1) {
292                            return StringUtil.toLowerCase(s);
293                    }
294    
295                    if (Character.isLowerCase(s.charAt(0))) {
296                            return s;
297                    }
298    
299                    if (Character.isUpperCase(s.charAt(0)) &&
300                            Character.isLowerCase(s.charAt(1))) {
301    
302                            return s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
303                                    s.substring(1));
304                    }
305    
306                    StringBuilder sb = new StringBuilder(s);
307    
308                    for (int i = 0; i < s.length(); i++) {
309                            if (((i + 1) != s.length()) &&
310                                    Character.isLowerCase(s.charAt(i + 1))) {
311    
312                                    break;
313                            }
314                            else {
315                                    char c = Character.toLowerCase(s.charAt(i));
316    
317                                    sb.setCharAt(i, c);
318                            }
319                    }
320    
321                    return sb.toString();
322            }
323    
324            private static String _formatJ(String s) {
325                    s = StringUtil.replace(s, CharPool.DASH, CharPool.SPACE);
326                    s = StringUtil.replace(s, CharPool.UNDERLINE, CharPool.SPACE);
327    
328                    StringBuilder sb = new StringBuilder(StringUtil.toLowerCase(s));
329    
330                    for (int i = 0; i < s.length(); i++) {
331                            if ((i == 0) || (s.charAt(i - 1) == ' ')) {
332                                    sb.setCharAt(i, Character.toUpperCase(s.charAt(i)));
333                            }
334                    }
335    
336                    return sb.toString();
337            }
338    
339            private static String _formatK(String s) {
340                    s = _formatH(s);
341                    s = StringUtil.replace(s, CharPool.SPACE, CharPool.DASH);
342    
343                    return s;
344            }
345    
346            private static String _formatL(String s) {
347                    if (s.length() == 1) {
348                            return StringUtil.toLowerCase(s);
349                    }
350                    else if (Character.isLowerCase(s.charAt(0)) ||
351                                     (Character.isUpperCase(s.charAt(0)) &&
352                                      Character.isUpperCase(s.charAt(1)))) {
353    
354                            return s;
355                    }
356                    else {
357                            return s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
358                                    s.substring(1));
359                    }
360            }
361    
362            private static String _formatM(String s) {
363                    StringBuilder sb = new StringBuilder(s.length());
364    
365                    for (int i = 0; i < s.length(); i++) {
366                            char c = s.charAt(i);
367    
368                            if (c == '-') {
369                            }
370                            else if ((i > 0) && (s.charAt(i - 1) == '-')) {
371                                    sb.append(Character.toUpperCase(c));
372                            }
373                            else {
374                                    sb.append(c);
375                            }
376                    }
377    
378                    return sb.toString();
379            }
380    
381            private static String _formatN(String s) {
382                    return StringUtil.replace(s, CharPool.DASH, CharPool.UNDERLINE);
383            }
384    
385            private static String _formatO(String s) {
386                    return StringUtil.replace(s, CharPool.UNDERLINE, CharPool.DASH);
387            }
388    
389            private static String _formatP(String s) {
390                    StringBuilder sb = new StringBuilder(s.length() + s.length() / 2);
391    
392                    for (int i = 0; i < s.length() - 1; i++) {
393                            char c = s.charAt(i);
394    
395                            if (Character.isUpperCase(c)) {
396                                    sb.append(Character.toLowerCase(c));
397                            }
398                            else {
399                                    sb.append(c);
400    
401                                    if (Character.isUpperCase(s.charAt(i + 1))) {
402                                            sb.append(CharPool.DASH);
403                                    }
404                            }
405                    }
406    
407                    sb.append(Character.toLowerCase(s.charAt(s.length() - 1)));
408    
409                    return sb.toString();
410            }
411    
412            private static String _formatQ(String s) {
413                    StringBuilder sb = new StringBuilder(StringUtil.toLowerCase(s));
414    
415                    for (int i = 0; i < s.length(); i++) {
416                            char c = s.charAt(i);
417    
418                            if (Character.isUpperCase(c) && (i > 0) && ((i + 1) < s.length())) {
419                                    int delta = sb.length() - s.length();
420    
421                                    if (Character.isLowerCase(s.charAt(i + 1))) {
422                                            sb.insert(i + delta, CharPool.DASH);
423                                    }
424                                    else if (Character.isLowerCase(s.charAt(i - 1))) {
425                                            sb.insert(i + delta, CharPool.DASH);
426                                    }
427                            }
428                    }
429    
430                    return sb.toString();
431            }
432    
433            private static final double _STORAGE_SIZE_DENOMINATOR = 1024.0;
434    
435            private static final String _STORAGE_SIZE_SUFFIX_GB = "GB";
436    
437            private static final String _STORAGE_SIZE_SUFFIX_KB = "KB";
438    
439            private static final String _STORAGE_SIZE_SUFFIX_MB = "MB";
440    
441    }