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            public static String formatKB(double size, Locale locale) {
151                    NumberFormat numberFormat = NumberFormat.getInstance(locale);
152    
153                    numberFormat.setMaximumFractionDigits(1);
154                    numberFormat.setMinimumFractionDigits(1);
155    
156                    return numberFormat.format(size / 1024.0);
157            }
158    
159            public static String formatKB(int size, Locale locale) {
160                    return formatKB((double)size, locale);
161            }
162    
163            public static String formatName(String name) {
164                    if (Validator.isNull(name)) {
165                            return name;
166                    }
167    
168                    char[] chars = name.toLowerCase().trim().toCharArray();
169    
170                    if (chars.length > 0) {
171                            chars[0] = Character.toUpperCase(chars[0]);
172                    }
173    
174                    for (int i = 0; i < chars.length; i++) {
175                            if (chars[i] == ' ') {
176                                    chars[i + 1] = Character.toUpperCase(chars[i + 1]);
177                            }
178                    }
179    
180                    return new String(chars);
181            }
182    
183            public static String formatPlural(String s) {
184                    if (Validator.isNull(s)) {
185                            return s;
186                    }
187    
188                    if (s.endsWith("s")) {
189                            s = s.substring(0, s.length() -1) + "ses";
190                    }
191                    else if (s.endsWith("y")) {
192                            s = s.substring(0, s.length() -1) + "ies";
193                    }
194                    else {
195                            s = s + "s";
196                    }
197    
198                    return s;
199            }
200    
201            private static String _formatA(String s) {
202                    return StringUtil.replace(
203                            s.toUpperCase(), CharPool.SPACE, CharPool.UNDERLINE);
204            }
205    
206            private static String _formatB(String s) {
207                    return StringUtil.strip(s.toLowerCase(), CharPool.SPACE);
208            }
209    
210            private static String _formatC(String s) {
211                    return StringUtil.replace(
212                            s.toLowerCase(), CharPool.SPACE, CharPool.UNDERLINE);
213            }
214    
215            private static String _formatD(String s) {
216                    return StringUtil.strip(s, CharPool.SPACE);
217            }
218    
219            private static String _formatE(String s) {
220                    return s.toLowerCase();
221            }
222    
223            private static String _formatF(String s) {
224                    s = StringUtil.strip(s, CharPool.SPACE);
225    
226                    if (Character.isUpperCase(s.charAt(0))) {
227                            s = s.substring(0, 1).toLowerCase().concat(s.substring(1));
228                    }
229    
230                    return s;
231            }
232    
233            private static String _formatG(String s) {
234                    if (Character.isLowerCase(s.charAt(0))) {
235                            s = s.substring(0, 1).toUpperCase().concat(s.substring(1));
236                    }
237    
238                    return s;
239            }
240    
241            private static String _formatH(String s) {
242                    StringBuilder sb = new StringBuilder(s.length() * 2);
243    
244                    for (int i = 0; i < s.length(); i++) {
245                            char c = s.charAt(i);
246    
247                            if (Character.isUpperCase(c)) {
248                                    sb.append(CharPool.SPACE);
249                                    sb.append(Character.toLowerCase(c));
250                            }
251                            else {
252                                    sb.append(c);
253                            }
254                    }
255    
256                    return sb.toString().trim();
257            }
258    
259            private static String _formatI(String s) {
260                    if (s.length() == 1) {
261                            return s.toLowerCase();
262                    }
263    
264                    if (Character.isLowerCase(s.charAt(0))) {
265                            return s;
266                    }
267    
268                    if (Character.isUpperCase(s.charAt(0)) &&
269                            Character.isLowerCase(s.charAt(1))) {
270    
271                            return s = s.substring(0, 1).toLowerCase().concat(s.substring(1));
272                    }
273    
274                    StringBuilder sb = new StringBuilder(s);
275    
276                    for (int i = 0; i < s.length(); i++) {
277                            if (((i + 1) != s.length()) &&
278                                    Character.isLowerCase(s.charAt(i + 1))) {
279    
280                                    break;
281                            }
282                            else {
283                                    char c = Character.toLowerCase(s.charAt(i));
284    
285                                    sb.setCharAt(i, c);
286                            }
287                    }
288    
289                    return sb.toString();
290            }
291    
292            private static String _formatJ(String s) {
293                    s = StringUtil.replace(s, CharPool.DASH, CharPool.SPACE);
294                    s = StringUtil.replace(s, CharPool.UNDERLINE, CharPool.SPACE);
295    
296                    StringBuilder sb = new StringBuilder(s.toLowerCase());
297    
298                    for (int i = 0; i < s.length(); i++) {
299                            if ((i == 0) || (s.charAt(i - 1) == ' ')) {
300                                    sb.setCharAt(i, Character.toUpperCase(s.charAt(i)));
301                            }
302                    }
303    
304                    return sb.toString();
305            }
306    
307            private static String _formatK(String s) {
308                    s = _formatH(s);
309                    s = StringUtil.replace(s, CharPool.SPACE, CharPool.DASH);
310    
311                    return s;
312            }
313    
314            private static String _formatL(String s) {
315                    if (s.length() == 1) {
316                            return s.toLowerCase();
317                    }
318                    else if (Character.isLowerCase(s.charAt(0)) ||
319                                     (Character.isUpperCase(s.charAt(0)) &&
320                                      Character.isUpperCase(s.charAt(1)))) {
321    
322                            return s;
323                    }
324                    else {
325                            return s = s.substring(0, 1).toLowerCase().concat(s.substring(1));
326                    }
327            }
328    
329            private static String _formatM(String s) {
330                    StringBuilder sb = new StringBuilder(s.length());
331    
332                    for (int i = 0; i < s.length(); i++) {
333                            char c = s.charAt(i);
334    
335                            if (c == '-') {
336                            }
337                            else if ((i > 0) && (s.charAt(i - 1) == '-')) {
338                                    sb.append(Character.toUpperCase(c));
339                            }
340                            else {
341                                    sb.append(c);
342                            }
343                    }
344    
345                    return sb.toString();
346            }
347    
348            private static String _formatN(String s) {
349                    return StringUtil.replace(s, CharPool.DASH, CharPool.UNDERLINE);
350            }
351    
352            private static String _formatO(String s) {
353                    return StringUtil.replace(s, CharPool.UNDERLINE, CharPool.DASH);
354            }
355    
356            private static String _formatP(String s) {
357                    StringBuilder sb = new StringBuilder(s.toLowerCase());
358    
359                    for (int i = 0; i < s.length(); i++) {
360                            char c = s.charAt(i);
361    
362                            if (Character.isUpperCase(c) && (i > 0) && ((i + 1) < s.length())) {
363                                    int delta = sb.length() - s.length();
364    
365                                    if (Character.isLowerCase(s.charAt(i + 1))) {
366                                            sb.insert(i + delta, CharPool.DASH);
367                                    }
368                                    else if (Character.isLowerCase(s.charAt(i - 1))) {
369                                            sb.insert(i + delta, CharPool.DASH);
370                                    }
371                            }
372                    }
373    
374                    return sb.toString();
375            }
376    
377    }