1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.CharPool;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  import java.text.NumberFormat;
23  
24  import java.util.Locale;
25  
26  /**
27   * <a href="TextFormatter.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class TextFormatter {
32  
33      // Web Search --> WEB_SEARCH
34  
35      public static final int A = 0;
36  
37      // Web Search --> websearch
38  
39      public static final int B = 1;
40  
41      // Web Search --> web_search
42  
43      public static final int C = 2;
44  
45      // Web Search --> WebSearch
46  
47      public static final int D = 3;
48  
49      // Web Search --> web search
50  
51      public static final int E = 4;
52  
53      // Web Search --> webSearch
54  
55      public static final int F = 5;
56  
57      // formatId --> FormatId
58  
59      public static final int G = 6;
60  
61      // formatId --> format id
62  
63      public static final int H = 7;
64  
65      // FormatId --> formatId
66  
67      public static final int I = 8;
68  
69      // format-id --> Format Id
70  
71      public static final int J = 9;
72  
73      // formatId --> format-id
74  
75      public static final int K = 10;
76  
77      // FormatId --> formatId, FOrmatId --> FOrmatId
78  
79      public static final int L = 11;
80  
81      // format-id --> formatId
82  
83      public static final int M = 12;
84  
85      // format-id --> format_id
86  
87      public static final int N = 13;
88  
89      // format_id --> format-id
90  
91      public static final int O = 14;
92  
93      // formatID --> format-id
94  
95      public static final int P = 15;
96  
97      public static String format(String s, int style) {
98          if (Validator.isNull(s)) {
99              return null;
100         }
101 
102         s = s.trim();
103 
104         if (style == A) {
105             return _formatA(s);
106         }
107         else if (style == B) {
108             return _formatB(s);
109         }
110         else if (style == C) {
111             return _formatC(s);
112         }
113         else if (style == D) {
114             return _formatD(s);
115         }
116         else if (style == E) {
117             return _formatE(s);
118         }
119         else if (style == F) {
120             return _formatF(s);
121         }
122         else if (style == G) {
123             return _formatG(s);
124         }
125         else if (style == H) {
126             return _formatH(s);
127         }
128         else if (style == I) {
129             return _formatI(s);
130         }
131         else if (style == J) {
132             return _formatJ(s);
133         }
134         else if (style == K) {
135             return _formatK(s);
136         }
137         else if (style == L) {
138             return _formatL(s);
139         }
140         else if (style == M) {
141             return _formatM(s);
142         }
143         else if (style == N) {
144             return _formatN(s);
145         }
146         else if (style == O) {
147             return _formatO(s);
148         }
149         else if (style == P) {
150             return _formatP(s);
151         }
152         else {
153             return s;
154         }
155     }
156 
157     public static String formatKB(double size, Locale locale) {
158         NumberFormat numberFormat = NumberFormat.getInstance(locale);
159 
160         numberFormat.setMaximumFractionDigits(1);
161         numberFormat.setMinimumFractionDigits(1);
162 
163         return numberFormat.format(size / 1024.0);
164     }
165 
166     public static String formatKB(int size, Locale locale) {
167         return formatKB((double)size, locale);
168     }
169 
170     public static String formatName(String name) {
171         if (Validator.isNull(name)) {
172             return name;
173         }
174 
175         char[] charArray = name.toLowerCase().trim().toCharArray();
176 
177         if (charArray.length > 0) {
178             charArray[0] = Character.toUpperCase(charArray[0]);
179         }
180 
181         for (int i = 0; i < charArray.length; i++) {
182             if (charArray[i] == ' ') {
183                 charArray[i + 1] = Character.toUpperCase(charArray[i + 1]);
184             }
185         }
186 
187         return new String(charArray);
188     }
189 
190     public static String formatPlural(String s) {
191         if (Validator.isNull(s)) {
192             return s;
193         }
194 
195         if (s.endsWith("s")) {
196             s = s.substring(0, s.length() -1) + "ses";
197         }
198         else if (s.endsWith("y")) {
199             s = s.substring(0, s.length() -1) + "ies";
200         }
201         else {
202             s = s + "s";
203         }
204 
205         return s;
206     }
207 
208     private static String _formatA(String s) {
209         return StringUtil.replace(
210             s.toUpperCase(), CharPool.SPACE, CharPool.UNDERLINE);
211     }
212 
213     private static String _formatB(String s) {
214         return StringUtil.replace(
215             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
216     }
217 
218     private static String _formatC(String s) {
219         return StringUtil.replace(
220             s.toLowerCase(), CharPool.SPACE, CharPool.UNDERLINE);
221     }
222 
223     private static String _formatD(String s) {
224         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
225     }
226 
227     private static String _formatE(String s) {
228         return s.toLowerCase();
229     }
230 
231     private static String _formatF(String s) {
232         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
233         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
234 
235         return s;
236     }
237 
238     private static String _formatG(String s) {
239         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
240     }
241 
242     private static String _formatH(String s) {
243         char[] charArray = s.toCharArray();
244 
245         StringBuilder sb = new StringBuilder(charArray.length * 2);
246 
247         for (int i = 0; i < charArray.length; i++) {
248             if (Character.isUpperCase(charArray[i])) {
249                 sb.append(CharPool.SPACE);
250                 sb.append(Character.toLowerCase(charArray[i]));
251             }
252             else {
253                 sb.append(charArray[i]);
254             }
255         }
256 
257         return sb.toString().trim();
258     }
259 
260     private static String _formatI(String s) {
261         if (s.length() == 1) {
262             return s.toLowerCase();
263         }
264 
265         if (Character.isUpperCase(s.charAt(0)) &&
266             Character.isLowerCase(s.charAt(1))) {
267 
268             return Character.toLowerCase(s.charAt(0)) +
269                 s.substring(1, s.length());
270         }
271 
272         char[] charArray = s.toCharArray();
273 
274         StringBuilder sb = new StringBuilder(charArray.length);
275 
276         for (int i = 0; i < charArray.length; i++) {
277             if ((i + 1 != charArray.length) &&
278                 (Character.isLowerCase(charArray[i + 1]))) {
279 
280                 sb.append(s.substring(i, charArray.length));
281 
282                 break;
283             }
284             else {
285                 sb.append(Character.toLowerCase(charArray[i]));
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         char[] charArray = s.toCharArray();
297 
298         StringBuilder sb = new StringBuilder(charArray.length);
299 
300         for (int i = 0; i < charArray.length; i++) {
301             if ((i == 0) || (charArray[i - 1] == ' ')) {
302                 sb.append(Character.toUpperCase(charArray[i]));
303             }
304             else {
305                 sb.append(Character.toLowerCase(charArray[i]));
306             }
307         }
308 
309         return sb.toString();
310     }
311 
312     private static String _formatK(String s) {
313         s = _formatH(s);
314         s = StringUtil.replace(s, CharPool.SPACE, CharPool.DASH);
315 
316         return s;
317     }
318 
319     private static String _formatL(String s) {
320         if (s.length() == 1) {
321             return s.toLowerCase();
322         }
323         else if (Character.isUpperCase(s.charAt(0)) &&
324                  Character.isUpperCase(s.charAt(1))) {
325 
326             return s;
327         }
328         else {
329             return Character.toLowerCase(s.charAt(0)) + s.substring(1);
330         }
331     }
332 
333     private static String _formatM(String s) {
334         char[] charArray = s.toCharArray();
335 
336         StringBuilder sb = new StringBuilder(charArray.length);
337 
338         for (int i = 0; i < charArray.length; i++) {
339             if (charArray[i] == '-') {
340             }
341             else if ((i > 0) && (charArray[i - 1] == '-')) {
342                 sb.append(Character.toUpperCase(charArray[i]));
343             }
344             else {
345                 sb.append(charArray[i]);
346             }
347         }
348 
349         return sb.toString();
350     }
351 
352     private static String _formatN(String s) {
353         return StringUtil.replace(s, CharPool.DASH, CharPool.UNDERLINE);
354     }
355 
356     private static String _formatO(String s) {
357         return StringUtil.replace(s, CharPool.UNDERLINE, CharPool.DASH);
358     }
359 
360     private static String _formatP(String s) {
361         char[] charArray = s.toCharArray();
362 
363         StringBuilder sb = new StringBuilder(charArray.length * 2);
364 
365         for (int i = 0; i < charArray.length; i++) {
366             if (Character.isUpperCase(charArray[i]) && (i > 0) &&
367                 ((i + 1) < charArray.length)) {
368 
369                 if (Character.isUpperCase(charArray[i]) &&
370                     Character.isLowerCase(charArray[i + 1])) {
371 
372                     sb.append(CharPool.DASH);
373                 }
374                 else if (Character.isLowerCase(charArray[i - 1]) &&
375                          Character.isUpperCase(charArray[i])) {
376 
377                     sb.append(CharPool.DASH);
378                 }
379             }
380 
381             sb.append(Character.toLowerCase(charArray[i]));
382         }
383 
384         return sb.toString();
385     }
386 
387 }