1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  
21  import java.text.NumberFormat;
22  
23  import java.util.Locale;
24  
25  /**
26   * <a href="TextFormatter.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class TextFormatter {
31  
32      // Web Search --> WEB_SEARCH
33  
34      public static final int A = 0;
35  
36      // Web Search --> websearch
37  
38      public static final int B = 1;
39  
40      // Web Search --> web_search
41  
42      public static final int C = 2;
43  
44      // Web Search --> WebSearch
45  
46      public static final int D = 3;
47  
48      // Web Search --> web search
49  
50      public static final int E = 4;
51  
52      // Web Search --> webSearch
53  
54      public static final int F = 5;
55  
56      // formatId --> FormatId
57  
58      public static final int G = 6;
59  
60      // formatId --> format id
61  
62      public static final int H = 7;
63  
64      // FormatId --> formatId
65  
66      public static final int I = 8;
67  
68      // format-id --> Format Id
69  
70      public static final int J = 9;
71  
72      // formatId --> format-id
73  
74      public static final int K = 10;
75  
76      // FormatId --> formatId, FOrmatId --> FOrmatId
77  
78      public static final int L = 11;
79  
80      // format-id --> formatId
81  
82      public static final int M = 12;
83  
84      // format-id --> format_id
85  
86      public static final int N = 13;
87  
88      // format_id --> format-id
89  
90      public static final int O = 14;
91  
92      public static String format(String s, int style) {
93          if (Validator.isNull(s)) {
94              return null;
95          }
96  
97          s = s.trim();
98  
99          if (style == A) {
100             return _formatA(s);
101         }
102         else if (style == B) {
103             return _formatB(s);
104         }
105         else if (style == C) {
106             return _formatC(s);
107         }
108         else if (style == D) {
109             return _formatD(s);
110         }
111         else if (style == E) {
112             return _formatE(s);
113         }
114         else if (style == F) {
115             return _formatF(s);
116         }
117         else if (style == G) {
118             return _formatG(s);
119         }
120         else if (style == H) {
121             return _formatH(s);
122         }
123         else if (style == I) {
124             return _formatI(s);
125         }
126         else if (style == J) {
127             return _formatJ(s);
128         }
129         else if (style == K) {
130             return _formatK(s);
131         }
132         else if (style == L) {
133             return _formatL(s);
134         }
135         else if (style == M) {
136             return _formatM(s);
137         }
138         else if (style == N) {
139             return _formatN(s);
140         }
141         else if (style == O) {
142             return _formatO(s);
143         }
144         else {
145             return s;
146         }
147     }
148 
149     public static String formatKB(double size, Locale locale) {
150         NumberFormat nf = NumberFormat.getInstance(locale);
151         nf.setMaximumFractionDigits(1);
152         nf.setMinimumFractionDigits(1);
153 
154         return nf.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(), StringPool.SPACE, StringPool.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(), StringPool.SPACE, StringPool.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(StringPool.SPACE);
241                 sb.append(Character.toLowerCase(charArray[i]));
242             }
243             else {
244                 sb.append(charArray[i]);
245             }
246         }
247 
248         return sb.toString();
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, StringPool.DASH, StringPool.SPACE);
285         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.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, StringPool.SPACE, StringPool.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, StringPool.DASH, StringPool.UNDERLINE);
345     }
346 
347     private static String _formatO(String s) {
348         return StringUtil.replace(s, StringPool.UNDERLINE, StringPool.DASH);
349     }
350 
351 }