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            /**
158             * @deprecated As of 6.2.0, replaced by {@link #formatStorageSize(double,
159             *             Locale)}
160             */
161            @Deprecated
162            public static String formatKB(double size, Locale locale) {
163                    NumberFormat numberFormat = NumberFormat.getInstance(locale);
164    
165                    numberFormat.setMaximumFractionDigits(1);
166                    numberFormat.setMinimumFractionDigits(1);
167    
168                    return numberFormat.format(size / _STORAGE_SIZE_DENOMINATOR);
169            }
170    
171            /**
172             * @deprecated As of 6.2.0, replaced by {@link #formatStorageSize(int,
173             *             Locale)}
174             */
175            @Deprecated
176            public static String formatKB(int size, Locale locale) {
177                    return formatKB((double)size, locale);
178            }
179    
180            public static String formatName(String name) {
181                    if (Validator.isNull(name)) {
182                            return name;
183                    }
184    
185                    char[] chars = StringUtil.toLowerCase(name).trim().toCharArray();
186    
187                    if (chars.length > 0) {
188                            chars[0] = Character.toUpperCase(chars[0]);
189                    }
190    
191                    for (int i = 0; i < chars.length; i++) {
192                            if (chars[i] == ' ') {
193                                    chars[i + 1] = Character.toUpperCase(chars[i + 1]);
194                            }
195                    }
196    
197                    return new String(chars);
198            }
199    
200            public static String formatPlural(String s) {
201                    if (Validator.isNull(s)) {
202                            return s;
203                    }
204    
205                    if (s.endsWith("s")) {
206                            s = s.substring(0, s.length() -1) + "ses";
207                    }
208                    else if (s.endsWith("y")) {
209                            s = s.substring(0, s.length() -1) + "ies";
210                    }
211                    else {
212                            s = s + "s";
213                    }
214    
215                    return s;
216            }
217    
218            public static String formatStorageSize(double size, Locale locale) {
219                    String suffix = _STORAGE_SIZE_SUFFIX_KB;
220    
221                    size = size / _STORAGE_SIZE_DENOMINATOR;
222    
223                    if (size >= _STORAGE_SIZE_DENOMINATOR) {
224                            suffix = _STORAGE_SIZE_SUFFIX_MB;
225    
226                            size = size / _STORAGE_SIZE_DENOMINATOR;
227                    }
228    
229                    if (size >= _STORAGE_SIZE_DENOMINATOR) {
230                            suffix = _STORAGE_SIZE_SUFFIX_GB;
231    
232                            size = size / _STORAGE_SIZE_DENOMINATOR;
233                    }
234    
235                    NumberFormat numberFormat = NumberFormat.getInstance(locale);
236    
237                    if (suffix.equals(_STORAGE_SIZE_SUFFIX_KB)) {
238                            numberFormat.setMaximumFractionDigits(0);
239                    }
240                    else {
241                            numberFormat.setMaximumFractionDigits(1);
242                    }
243    
244                    numberFormat.setMinimumFractionDigits(0);
245    
246                    return numberFormat.format(size) + suffix;
247            }
248    
249            public static String formatStorageSize(int size, Locale locale) {
250                    return formatStorageSize((double)size, locale);
251            }
252    
253            private static String _formatA(String s) {
254                    return StringUtil.replace(
255                            StringUtil.toUpperCase(s), CharPool.SPACE, CharPool.UNDERLINE);
256            }
257    
258            private static String _formatB(String s) {
259                    return StringUtil.strip(StringUtil.toLowerCase(s), CharPool.SPACE);
260            }
261    
262            private static String _formatC(String s) {
263                    return StringUtil.replace(
264                            StringUtil.toLowerCase(s), CharPool.SPACE, CharPool.UNDERLINE);
265            }
266    
267            private static String _formatD(String s) {
268                    return StringUtil.strip(s, CharPool.SPACE);
269            }
270    
271            private static String _formatE(String s) {
272                    return StringUtil.toLowerCase(s);
273            }
274    
275            private static String _formatF(String s) {
276                    s = StringUtil.strip(s, CharPool.SPACE);
277    
278                    if (Character.isUpperCase(s.charAt(0))) {
279                            s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
280                                    s.substring(1));
281                    }
282    
283                    return s;
284            }
285    
286            private static String _formatG(String s) {
287                    if (Character.isLowerCase(s.charAt(0))) {
288                            s = StringUtil.toUpperCase(s.substring(0, 1)).concat(
289                                    s.substring(1));
290                    }
291    
292                    return s;
293            }
294    
295            private static String _formatH(String s) {
296                    StringBuilder sb = new StringBuilder(s.length() * 2);
297    
298                    for (int i = 0; i < s.length(); i++) {
299                            char c = s.charAt(i);
300    
301                            if (Character.isUpperCase(c)) {
302                                    sb.append(CharPool.SPACE);
303                                    sb.append(Character.toLowerCase(c));
304                            }
305                            else {
306                                    sb.append(c);
307                            }
308                    }
309    
310                    return sb.toString().trim();
311            }
312    
313            private static String _formatI(String s) {
314                    if (s.length() == 1) {
315                            return StringUtil.toLowerCase(s);
316                    }
317    
318                    if (Character.isLowerCase(s.charAt(0))) {
319                            return s;
320                    }
321    
322                    if (Character.isUpperCase(s.charAt(0)) &&
323                            Character.isLowerCase(s.charAt(1))) {
324    
325                            return s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
326                                    s.substring(1));
327                    }
328    
329                    StringBuilder sb = new StringBuilder(s);
330    
331                    for (int i = 0; i < s.length(); i++) {
332                            if (((i + 1) != s.length()) &&
333                                    Character.isLowerCase(s.charAt(i + 1))) {
334    
335                                    break;
336                            }
337                            else {
338                                    char c = Character.toLowerCase(s.charAt(i));
339    
340                                    sb.setCharAt(i, c);
341                            }
342                    }
343    
344                    return sb.toString();
345            }
346    
347            private static String _formatJ(String s) {
348                    s = StringUtil.replace(s, CharPool.DASH, CharPool.SPACE);
349                    s = StringUtil.replace(s, CharPool.UNDERLINE, CharPool.SPACE);
350    
351                    StringBuilder sb = new StringBuilder(StringUtil.toLowerCase(s));
352    
353                    for (int i = 0; i < s.length(); i++) {
354                            if ((i == 0) || (s.charAt(i - 1) == ' ')) {
355                                    sb.setCharAt(i, Character.toUpperCase(s.charAt(i)));
356                            }
357                    }
358    
359                    return sb.toString();
360            }
361    
362            private static String _formatK(String s) {
363                    s = _formatH(s);
364                    s = StringUtil.replace(s, CharPool.SPACE, CharPool.DASH);
365    
366                    return s;
367            }
368    
369            private static String _formatL(String s) {
370                    if (s.length() == 1) {
371                            return StringUtil.toLowerCase(s);
372                    }
373                    else if (Character.isLowerCase(s.charAt(0)) ||
374                                     (Character.isUpperCase(s.charAt(0)) &&
375                                      Character.isUpperCase(s.charAt(1)))) {
376    
377                            return s;
378                    }
379                    else {
380                            return s = StringUtil.toLowerCase(s.substring(0, 1)).concat(
381                                    s.substring(1));
382                    }
383            }
384    
385            private static String _formatM(String s) {
386                    StringBuilder sb = new StringBuilder(s.length());
387    
388                    for (int i = 0; i < s.length(); i++) {
389                            char c = s.charAt(i);
390    
391                            if (c == '-') {
392                            }
393                            else if ((i > 0) && (s.charAt(i - 1) == '-')) {
394                                    sb.append(Character.toUpperCase(c));
395                            }
396                            else {
397                                    sb.append(c);
398                            }
399                    }
400    
401                    return sb.toString();
402            }
403    
404            private static String _formatN(String s) {
405                    return StringUtil.replace(s, CharPool.DASH, CharPool.UNDERLINE);
406            }
407    
408            private static String _formatO(String s) {
409                    return StringUtil.replace(s, CharPool.UNDERLINE, CharPool.DASH);
410            }
411    
412            private static String _formatP(String s) {
413                    StringBuilder sb = new StringBuilder(s.length() + s.length() / 2);
414    
415                    for (int i = 0; i < s.length() - 1; i++) {
416                            char c = s.charAt(i);
417    
418                            if (Character.isUpperCase(c)) {
419                                    sb.append(Character.toLowerCase(c));
420                            }
421                            else {
422                                    sb.append(c);
423    
424                                    if (Character.isUpperCase(s.charAt(i + 1))) {
425                                            sb.append(CharPool.DASH);
426                                    }
427                            }
428                    }
429    
430                    sb.append(Character.toLowerCase(s.charAt(s.length() - 1)));
431    
432                    return sb.toString();
433            }
434    
435            private static String _formatQ(String s) {
436                    StringBuilder sb = new StringBuilder(StringUtil.toLowerCase(s));
437    
438                    for (int i = 0; i < s.length(); i++) {
439                            char c = s.charAt(i);
440    
441                            if (Character.isUpperCase(c) && (i > 0) && ((i + 1) < s.length())) {
442                                    int delta = sb.length() - s.length();
443    
444                                    if (Character.isLowerCase(s.charAt(i + 1))) {
445                                            sb.insert(i + delta, CharPool.DASH);
446                                    }
447                                    else if (Character.isLowerCase(s.charAt(i - 1))) {
448                                            sb.insert(i + delta, CharPool.DASH);
449                                    }
450                            }
451                    }
452    
453                    return sb.toString();
454            }
455    
456            private static final double _STORAGE_SIZE_DENOMINATOR = 1024.0;
457    
458            private static final String _STORAGE_SIZE_SUFFIX_GB = "GB";
459    
460            private static final String _STORAGE_SIZE_SUFFIX_KB = "KB";
461    
462            private static final String _STORAGE_SIZE_SUFFIX_MB = "MB";
463    
464    }