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