001    /**
002     * Copyright (c) 2000-2010 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.util;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.text.NumberFormat;
022    
023    import java.util.Locale;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class TextFormatter {
029    
030            // Web Search --> WEB_SEARCH
031    
032            public static final int A = 0;
033    
034            // Web Search --> websearch
035    
036            public static final int B = 1;
037    
038            // Web Search --> web_search
039    
040            public static final int C = 2;
041    
042            // Web Search --> WebSearch
043    
044            public static final int D = 3;
045    
046            // Web Search --> web search
047    
048            public static final int E = 4;
049    
050            // Web Search --> webSearch
051    
052            public static final int F = 5;
053    
054            // formatId --> FormatId
055    
056            public static final int G = 6;
057    
058            // formatId --> format id
059    
060            public static final int H = 7;
061    
062            // FormatId --> formatId
063    
064            public static final int I = 8;
065    
066            // format-id --> Format Id
067    
068            public static final int J = 9;
069    
070            // formatId --> format-id
071    
072            public static final int K = 10;
073    
074            // FormatId --> formatId, FOrmatId --> FOrmatId
075    
076            public static final int L = 11;
077    
078            // format-id --> formatId
079    
080            public static final int M = 12;
081    
082            // format-id --> format_id
083    
084            public static final int N = 13;
085    
086            // format_id --> format-id
087    
088            public static final int O = 14;
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 {
143                            return s;
144                    }
145            }
146    
147            public static String formatKB(double size, Locale locale) {
148                    NumberFormat nf = NumberFormat.getInstance(locale);
149                    nf.setMaximumFractionDigits(1);
150                    nf.setMinimumFractionDigits(1);
151    
152                    return nf.format(size / 1024.0);
153            }
154    
155            public static String formatKB(int size, Locale locale) {
156                    return formatKB((double)size, locale);
157            }
158    
159            public static String formatName(String name) {
160                    if (Validator.isNull(name)) {
161                            return name;
162                    }
163    
164                    char[] charArray = name.toLowerCase().trim().toCharArray();
165    
166                    if (charArray.length > 0) {
167                            charArray[0] = Character.toUpperCase(charArray[0]);
168                    }
169    
170                    for (int i = 0; i < charArray.length; i++) {
171                            if (charArray[i] == ' ') {
172                                    charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
173                            }
174                    }
175    
176                    return new String(charArray);
177            }
178    
179            public static String formatPlural(String s) {
180                    if (Validator.isNull(s)) {
181                            return s;
182                    }
183    
184                    if (s.endsWith("s")) {
185                            s = s.substring(0, s.length() -1) + "ses";
186                    }
187                    else if (s.endsWith("y")) {
188                            s = s.substring(0, s.length() -1) + "ies";
189                    }
190                    else {
191                            s = s + "s";
192                    }
193    
194                    return s;
195            }
196    
197            private static String _formatA(String s) {
198                    return StringUtil.replace(
199                            s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
200            }
201    
202            private static String _formatB(String s) {
203                    return StringUtil.replace(
204                            s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
205            }
206    
207            private static String _formatC(String s) {
208                    return StringUtil.replace(
209                            s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
210            }
211    
212            private static String _formatD(String s) {
213                    return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
214            }
215    
216            private static String _formatE(String s) {
217                    return s.toLowerCase();
218            }
219    
220            private static String _formatF(String s) {
221                    s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
222                    s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
223    
224                    return s;
225            }
226    
227            private static String _formatG(String s) {
228                    return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
229            }
230    
231            private static String _formatH(String s) {
232                    char[] charArray = s.toCharArray();
233    
234                    StringBuilder sb = new StringBuilder(charArray.length * 2);
235    
236                    for (int i = 0; i < charArray.length; i++) {
237                            if (Character.isUpperCase(charArray[i])) {
238                                    sb.append(StringPool.SPACE);
239                                    sb.append(Character.toLowerCase(charArray[i]));
240                            }
241                            else {
242                                    sb.append(charArray[i]);
243                            }
244                    }
245    
246                    return sb.toString();
247            }
248    
249            private static String _formatI(String s) {
250                    if (s.length() == 1) {
251                            return s.toLowerCase();
252                    }
253    
254                    if (Character.isUpperCase(s.charAt(0)) &&
255                            Character.isLowerCase(s.charAt(1))) {
256    
257                            return Character.toLowerCase(s.charAt(0)) +
258                                    s.substring(1, s.length());
259                    }
260    
261                    char[] charArray = s.toCharArray();
262    
263                    StringBuilder sb = new StringBuilder(charArray.length);
264    
265                    for (int i = 0; i < charArray.length; i++) {
266                            if ((i + 1 != charArray.length) &&
267                                    (Character.isLowerCase(charArray[i + 1]))) {
268    
269                                    sb.append(s.substring(i, charArray.length));
270    
271                                    break;
272                            }
273                            else {
274                                    sb.append(Character.toLowerCase(charArray[i]));
275                            }
276                    }
277    
278                    return sb.toString();
279            }
280    
281            private static String _formatJ(String s) {
282                    s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
283                    s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
284    
285                    char[] charArray = s.toCharArray();
286    
287                    StringBuilder sb = new StringBuilder(charArray.length);
288    
289                    for (int i = 0; i < charArray.length; i++) {
290                            if ((i == 0) || (charArray[i - 1] == ' ')) {
291                                    sb.append(Character.toUpperCase(charArray[i]));
292                            }
293                            else {
294                                    sb.append(Character.toLowerCase(charArray[i]));
295                            }
296                    }
297    
298                    return sb.toString();
299            }
300    
301            private static String _formatK(String s) {
302                    s = _formatH(s);
303                    s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
304    
305                    return s;
306            }
307    
308            private static String _formatL(String s) {
309                    if (s.length() == 1) {
310                            return s.toLowerCase();
311                    }
312                    else if (Character.isUpperCase(s.charAt(0)) &&
313                                     Character.isUpperCase(s.charAt(1))) {
314    
315                            return s;
316                    }
317                    else {
318                            return Character.toLowerCase(s.charAt(0)) + s.substring(1);
319                    }
320            }
321    
322            private static String _formatM(String s) {
323                    char[] charArray = s.toCharArray();
324    
325                    StringBuilder sb = new StringBuilder(charArray.length);
326    
327                    for (int i = 0; i < charArray.length; i++) {
328                            if (charArray[i] == '-') {
329                            }
330                            else if ((i > 0) && (charArray[i - 1] == '-')) {
331                                    sb.append(Character.toUpperCase(charArray[i]));
332                            }
333                            else {
334                                    sb.append(charArray[i]);
335                            }
336                    }
337    
338                    return sb.toString();
339            }
340    
341            private static String _formatN(String s) {
342                    return StringUtil.replace(s, StringPool.DASH, StringPool.UNDERLINE);
343            }
344    
345            private static String _formatO(String s) {
346                    return StringUtil.replace(s, StringPool.UNDERLINE, StringPool.DASH);
347            }
348    
349    }