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      public static String format(String s, int style) {
94          if (Validator.isNull(s)) {
95              return null;
96          }
97  
98          s = s.trim();
99  
100         if (style == A) {
101             return _formatA(s);
102         }
103         else if (style == B) {
104             return _formatB(s);
105         }
106         else if (style == C) {
107             return _formatC(s);
108         }
109         else if (style == D) {
110             return _formatD(s);
111         }
112         else if (style == E) {
113             return _formatE(s);
114         }
115         else if (style == F) {
116             return _formatF(s);
117         }
118         else if (style == G) {
119             return _formatG(s);
120         }
121         else if (style == H) {
122             return _formatH(s);
123         }
124         else if (style == I) {
125             return _formatI(s);
126         }
127         else if (style == J) {
128             return _formatJ(s);
129         }
130         else if (style == K) {
131             return _formatK(s);
132         }
133         else if (style == L) {
134             return _formatL(s);
135         }
136         else if (style == M) {
137             return _formatM(s);
138         }
139         else {
140             return s;
141         }
142     }
143 
144     public static String formatKB(double size, Locale locale) {
145         NumberFormat nf = NumberFormat.getInstance(locale);
146         nf.setMaximumFractionDigits(1);
147         nf.setMinimumFractionDigits(1);
148 
149         return nf.format(size / 1024.0);
150     }
151 
152     public static String formatKB(int size, Locale locale) {
153         return formatKB((double)size, locale);
154     }
155 
156     public static String formatName(String name) {
157         if (Validator.isNull(name)) {
158             return name;
159         }
160 
161         char[] c = name.toLowerCase().trim().toCharArray();
162 
163         if (c.length > 0) {
164             c[0] = Character.toUpperCase(c[0]);
165         }
166 
167         for (int i = 0; i < c.length; i++) {
168             if (c[i] == ' ') {
169                 c[i + 1] = Character.toUpperCase(c[i + 1]);
170             }
171         }
172 
173         return new String(c);
174     }
175 
176     public static String formatPlural(String s) {
177         if (Validator.isNull(s)) {
178             return s;
179         }
180 
181         if (s.endsWith("s")) {
182             s = s.substring(0, s.length() -1) + "ses";
183         }
184         else if (s.endsWith("y")) {
185             s = s.substring(0, s.length() -1) + "ies";
186         }
187         else {
188             s = s + "s";
189         }
190 
191         return s;
192     }
193 
194     private static String _formatA(String s) {
195         return StringUtil.replace(
196             s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
197     }
198 
199     private static String _formatB(String s) {
200         return StringUtil.replace(
201             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
202     }
203 
204     private static String _formatC(String s) {
205         return StringUtil.replace(
206             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
207     }
208 
209     private static String _formatD(String s) {
210         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
211     }
212 
213     private static String _formatE(String s) {
214         return s.toLowerCase();
215     }
216 
217     private static String _formatF(String s) {
218         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
219         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
220 
221         return s;
222     }
223 
224     private static String _formatG(String s) {
225         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
226     }
227 
228     private static String _formatH(String s) {
229         StringBuilder sb = new StringBuilder();
230 
231         char[] c = s.toCharArray();
232 
233         for (int i = 0; i < c.length; i++) {
234             if (Character.isUpperCase(c[i])) {
235                 sb.append(StringPool.SPACE);
236                 sb.append(Character.toLowerCase(c[i]));
237             }
238             else {
239                 sb.append(c[i]);
240             }
241         }
242 
243         return sb.toString();
244     }
245 
246     private static String _formatI(String s) {
247         if (s.length() == 1) {
248             return s.toLowerCase();
249         }
250 
251         if (Character.isUpperCase(s.charAt(0)) &&
252             Character.isLowerCase(s.charAt(1))) {
253 
254             return Character.toLowerCase(s.charAt(0)) +
255                 s.substring(1, s.length());
256         }
257 
258         StringBuilder sb = new StringBuilder();
259 
260         char[] c = s.toCharArray();
261 
262         for (int i = 0; i < c.length; i++) {
263             if ((i + 1 != c.length) &&
264                 (Character.isLowerCase(c[i + 1]))) {
265 
266                 sb.append(s.substring(i, c.length));
267 
268                 break;
269             }
270             else {
271                 sb.append(Character.toLowerCase(c[i]));
272             }
273         }
274 
275         return sb.toString();
276     }
277 
278     private static String _formatJ(String s) {
279         StringBuilder sb = new StringBuilder();
280 
281         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
282         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
283 
284         char[] c = s.toCharArray();
285 
286         for (int i = 0; i < c.length; i++) {
287             if ((i == 0) || (c[i - 1] == ' ')) {
288                 sb.append(Character.toUpperCase(c[i]));
289             }
290             else {
291                 sb.append(Character.toLowerCase(c[i]));
292             }
293         }
294 
295         return sb.toString();
296     }
297 
298     private static String _formatK(String s) {
299         s = _formatH(s);
300         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
301 
302         return s;
303     }
304 
305     private static String _formatL(String s) {
306         if (s.length() == 1) {
307             return s.toLowerCase();
308         }
309         else if (Character.isUpperCase(s.charAt(0)) &&
310                  Character.isUpperCase(s.charAt(1))) {
311 
312             return s;
313         }
314         else {
315             return Character.toLowerCase(s.charAt(0)) + s.substring(1);
316         }
317     }
318 
319     private static String _formatM(String s) {
320         StringBuilder sb = new StringBuilder();
321 
322         char[] c = s.toCharArray();
323 
324         for (int i = 0; i < c.length; i++) {
325             if (c[i] == '-') {
326             }
327             else if ((i > 0) && (c[i - 1] == '-')) {
328                 sb.append(Character.toUpperCase(c[i]));
329             }
330             else {
331                 sb.append(c[i]);
332             }
333         }
334 
335         return sb.toString();
336     }
337 
338 }