1   /**
2    * Copyright (c) 2000-2008 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      // Web Search --> websearch
43      // Web Search --> web_search
44      // Web Search --> WebSearch
45      // Web Search --> web search
46      // Web Search --> webSearch
47  
48      public static final int A = 0;
49      public static final int B = 1;
50      public static final int C = 2;
51      public static final int D = 3;
52      public static final int E = 4;
53      public static final int F = 5;
54  
55      // formatId --> FormatId
56      // formatId --> format id
57  
58      public static final int G = 6;
59      public static final int H = 7;
60  
61      // FormatId --> formatId
62  
63      public static final int I = 8;
64  
65      // format-id --> Format Id
66  
67      public static final int J = 9;
68  
69      // formatId --> format-id
70  
71      public static final int K = 10;
72  
73      public static String format(String s, int style) {
74          if (Validator.isNull(s)) {
75              return null;
76          }
77  
78          s = s.trim();
79  
80          if (style == A) {
81              return _formatA(s);
82          }
83          else if (style == B) {
84              return _formatB(s);
85          }
86          else if (style == C) {
87              return _formatC(s);
88          }
89          else if (style == D) {
90              return _formatD(s);
91          }
92          else if (style == E) {
93              return _formatE(s);
94          }
95          else if (style == F) {
96              return _formatF(s);
97          }
98          else if (style == G) {
99              return _formatG(s);
100         }
101         else if (style == H) {
102             return _formatH(s);
103         }
104         else if (style == I) {
105             return _formatI(s);
106         }
107         else if (style == J) {
108             return _formatJ(s);
109         }
110         else if (style == K) {
111             return _formatK(s);
112         }
113         else {
114             return s;
115         }
116     }
117 
118     public static String formatKB(double size, Locale locale) {
119         NumberFormat nf = NumberFormat.getInstance(locale);
120         nf.setMaximumFractionDigits(1);
121         nf.setMinimumFractionDigits(1);
122 
123         return nf.format(size / 1024.0);
124     }
125 
126     public static String formatKB(int size, Locale locale) {
127         return formatKB((double)size, locale);
128     }
129 
130     public static String formatName(String name) {
131         if (Validator.isNull(name)) {
132             return name;
133         }
134 
135         char[] c = name.toLowerCase().trim().toCharArray();
136 
137         if (c.length > 0) {
138             c[0] = Character.toUpperCase(c[0]);
139         }
140 
141         for (int i = 0; i < c.length; i++) {
142             if (c[i] == ' ') {
143                 c[i + 1] = Character.toUpperCase(c[i + 1]);
144             }
145         }
146 
147         return new String(c);
148     }
149 
150     public static String formatPlural(String s) {
151         if (Validator.isNull(s)) {
152             return s;
153         }
154 
155         if (s.endsWith("s")) {
156             s = s.substring(0, s.length() -1) + "ses";
157         }
158         else if (s.endsWith("y")) {
159             s = s.substring(0, s.length() -1) + "ies";
160         }
161         else {
162             s = s + "s";
163         }
164 
165         return s;
166     }
167 
168     private static String _formatA(String s) {
169         return StringUtil.replace(
170             s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
171     }
172 
173     private static String _formatB(String s) {
174         return StringUtil.replace(
175             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
176     }
177 
178     private static String _formatC(String s) {
179         return StringUtil.replace(
180             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
181     }
182 
183     private static String _formatD(String s) {
184         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
185     }
186 
187     private static String _formatE(String s) {
188         return s.toLowerCase();
189     }
190 
191     private static String _formatF(String s) {
192         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
193         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
194 
195         return s;
196     }
197 
198     private static String _formatG(String s) {
199         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
200     }
201 
202     private static String _formatH(String s) {
203         StringBuilder sb = new StringBuilder();
204 
205         char[] c = s.toCharArray();
206 
207         for (int i = 0; i < c.length; i++) {
208             if (Character.isUpperCase(c[i])) {
209                 sb.append(StringPool.SPACE);
210                 sb.append(Character.toLowerCase(c[i]));
211             }
212             else {
213                 sb.append(c[i]);
214             }
215         }
216 
217         return sb.toString();
218     }
219 
220     private static String _formatI(String s) {
221         if (s.length() == 1) {
222             return s.toLowerCase();
223         }
224 
225         if (Character.isUpperCase(s.charAt(0)) &&
226             Character.isLowerCase(s.charAt(1))) {
227 
228             return Character.toLowerCase(s.charAt(0)) +
229                 s.substring(1, s.length());
230         }
231 
232         StringBuilder sb = new StringBuilder();
233 
234         char[] c = s.toCharArray();
235 
236         for (int i = 0; i < c.length; i++) {
237             if ((i + 1 != c.length) &&
238                 (Character.isLowerCase(c[i + 1]))) {
239 
240                 sb.append(s.substring(i, c.length));
241 
242                 break;
243             }
244             else {
245                 sb.append(Character.toLowerCase(c[i]));
246             }
247         }
248 
249         return sb.toString();
250     }
251 
252     private static String _formatJ(String s) {
253         StringBuilder sb = new StringBuilder();
254 
255         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
256         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
257 
258         char[] c = s.toCharArray();
259 
260         for (int i = 0; i < c.length; i++) {
261             if ((i == 0) || (c[i - 1] == ' ')) {
262                 sb.append(Character.toUpperCase(c[i]));
263             }
264             else {
265                 sb.append(Character.toLowerCase(c[i]));
266             }
267         }
268 
269         return sb.toString();
270     }
271 
272     private static String _formatK(String s) {
273         s = _formatH(s);
274         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
275 
276         return s;
277     }
278 
279 }