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