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