1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.text.NumberFormat;
30  
31  import java.util.Locale;
32  
33  /**
34   * <a href="TextFormatter.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class TextFormatter {
40  
41      // Web Search --> WEB_SEARCH
42  
43      public static final int A = 0;
44  
45      // Web Search --> websearch
46  
47      public static final int B = 1;
48  
49      // Web Search --> web_search
50  
51      public static final int C = 2;
52  
53      // Web Search --> WebSearch
54  
55      public static final int D = 3;
56  
57      // Web Search --> web search
58  
59      public static final int E = 4;
60  
61      // Web Search --> webSearch
62  
63      public static final int F = 5;
64  
65      // formatId --> FormatId
66  
67      public static final int G = 6;
68  
69      // formatId --> format id
70  
71      public static final int H = 7;
72  
73      // FormatId --> formatId
74  
75      public static final int I = 8;
76  
77      // format-id --> Format Id
78  
79      public static final int J = 9;
80  
81      // formatId --> format-id
82  
83      public static final int K = 10;
84  
85      // FormatId --> formatId, FOrmatId --> FOrmatId
86  
87      public static final int L = 11;
88  
89      // format-id --> formatId
90  
91      public static final int M = 12;
92  
93      // format-id --> format_id
94  
95      public static final int N = 13;
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 {
147             return s;
148         }
149     }
150 
151     public static String formatKB(double size, Locale locale) {
152         NumberFormat nf = NumberFormat.getInstance(locale);
153         nf.setMaximumFractionDigits(1);
154         nf.setMinimumFractionDigits(1);
155 
156         return nf.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[] c = name.toLowerCase().trim().toCharArray();
169 
170         if (c.length > 0) {
171             c[0] = Character.toUpperCase(c[0]);
172         }
173 
174         for (int i = 0; i < c.length; i++) {
175             if (c[i] == ' ') {
176                 c[i + 1] = Character.toUpperCase(c[i + 1]);
177             }
178         }
179 
180         return new String(c);
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(), StringPool.SPACE, StringPool.UNDERLINE);
204     }
205 
206     private static String _formatB(String s) {
207         return StringUtil.replace(
208             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
209     }
210 
211     private static String _formatC(String s) {
212         return StringUtil.replace(
213             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
214     }
215 
216     private static String _formatD(String s) {
217         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
218     }
219 
220     private static String _formatE(String s) {
221         return s.toLowerCase();
222     }
223 
224     private static String _formatF(String s) {
225         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
226         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
227 
228         return s;
229     }
230 
231     private static String _formatG(String s) {
232         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
233     }
234 
235     private static String _formatH(String s) {
236         StringBuilder sb = new StringBuilder();
237 
238         char[] c = s.toCharArray();
239 
240         for (int i = 0; i < c.length; i++) {
241             if (Character.isUpperCase(c[i])) {
242                 sb.append(StringPool.SPACE);
243                 sb.append(Character.toLowerCase(c[i]));
244             }
245             else {
246                 sb.append(c[i]);
247             }
248         }
249 
250         return sb.toString();
251     }
252 
253     private static String _formatI(String s) {
254         if (s.length() == 1) {
255             return s.toLowerCase();
256         }
257 
258         if (Character.isUpperCase(s.charAt(0)) &&
259             Character.isLowerCase(s.charAt(1))) {
260 
261             return Character.toLowerCase(s.charAt(0)) +
262                 s.substring(1, s.length());
263         }
264 
265         StringBuilder sb = new StringBuilder();
266 
267         char[] c = s.toCharArray();
268 
269         for (int i = 0; i < c.length; i++) {
270             if ((i + 1 != c.length) &&
271                 (Character.isLowerCase(c[i + 1]))) {
272 
273                 sb.append(s.substring(i, c.length));
274 
275                 break;
276             }
277             else {
278                 sb.append(Character.toLowerCase(c[i]));
279             }
280         }
281 
282         return sb.toString();
283     }
284 
285     private static String _formatJ(String s) {
286         StringBuilder sb = new StringBuilder();
287 
288         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
289         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
290 
291         char[] c = s.toCharArray();
292 
293         for (int i = 0; i < c.length; i++) {
294             if ((i == 0) || (c[i - 1] == ' ')) {
295                 sb.append(Character.toUpperCase(c[i]));
296             }
297             else {
298                 sb.append(Character.toLowerCase(c[i]));
299             }
300         }
301 
302         return sb.toString();
303     }
304 
305     private static String _formatK(String s) {
306         s = _formatH(s);
307         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
308 
309         return s;
310     }
311 
312     private static String _formatL(String s) {
313         if (s.length() == 1) {
314             return s.toLowerCase();
315         }
316         else if (Character.isUpperCase(s.charAt(0)) &&
317                  Character.isUpperCase(s.charAt(1))) {
318 
319             return s;
320         }
321         else {
322             return Character.toLowerCase(s.charAt(0)) + s.substring(1);
323         }
324     }
325 
326     private static String _formatM(String s) {
327         StringBuilder sb = new StringBuilder();
328 
329         char[] c = s.toCharArray();
330 
331         for (int i = 0; i < c.length; i++) {
332             if (c[i] == '-') {
333             }
334             else if ((i > 0) && (c[i - 1] == '-')) {
335                 sb.append(Character.toUpperCase(c[i]));
336             }
337             else {
338                 sb.append(c[i]);
339             }
340         }
341 
342         return sb.toString();
343     }
344 
345     private static String _formatN(String s) {
346         return StringUtil.replace(s, StringPool.DASH, StringPool.UNDERLINE);
347     }
348 
349 }