1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  
26  import java.text.NumberFormat;
27  
28  import java.util.Locale;
29  
30  /**
31   * <a href="TextFormatter.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class TextFormatter {
37  
38      // Web Search --> WEB_SEARCH
39  
40      public static final int A = 0;
41  
42      // Web Search --> websearch
43  
44      public static final int B = 1;
45  
46      // Web Search --> web_search
47  
48      public static final int C = 2;
49  
50      // Web Search --> WebSearch
51  
52      public static final int D = 3;
53  
54      // Web Search --> web search
55  
56      public static final int E = 4;
57  
58      // Web Search --> webSearch
59  
60      public static final int F = 5;
61  
62      // formatId --> FormatId
63  
64      public static final int G = 6;
65  
66      // formatId --> format id
67  
68      public static final int H = 7;
69  
70      // FormatId --> formatId
71  
72      public static final int I = 8;
73  
74      // format-id --> Format Id
75  
76      public static final int J = 9;
77  
78      // formatId --> format-id
79  
80      public static final int K = 10;
81  
82      // FormatId --> formatId, FOrmatId --> FOrmatId
83  
84      public static final int L = 11;
85  
86      // format-id --> formatId
87  
88      public static final int M = 12;
89  
90      // format-id --> format_id
91  
92      public static final int N = 13;
93  
94      public static String format(String s, int style) {
95          if (Validator.isNull(s)) {
96              return null;
97          }
98  
99          s = s.trim();
100 
101         if (style == A) {
102             return _formatA(s);
103         }
104         else if (style == B) {
105             return _formatB(s);
106         }
107         else if (style == C) {
108             return _formatC(s);
109         }
110         else if (style == D) {
111             return _formatD(s);
112         }
113         else if (style == E) {
114             return _formatE(s);
115         }
116         else if (style == F) {
117             return _formatF(s);
118         }
119         else if (style == G) {
120             return _formatG(s);
121         }
122         else if (style == H) {
123             return _formatH(s);
124         }
125         else if (style == I) {
126             return _formatI(s);
127         }
128         else if (style == J) {
129             return _formatJ(s);
130         }
131         else if (style == K) {
132             return _formatK(s);
133         }
134         else if (style == L) {
135             return _formatL(s);
136         }
137         else if (style == M) {
138             return _formatM(s);
139         }
140         else if (style == N) {
141             return _formatN(s);
142         }
143         else {
144             return s;
145         }
146     }
147 
148     public static String formatKB(double size, Locale locale) {
149         NumberFormat nf = NumberFormat.getInstance(locale);
150         nf.setMaximumFractionDigits(1);
151         nf.setMinimumFractionDigits(1);
152 
153         return nf.format(size / 1024.0);
154     }
155 
156     public static String formatKB(int size, Locale locale) {
157         return formatKB((double)size, locale);
158     }
159 
160     public static String formatName(String name) {
161         if (Validator.isNull(name)) {
162             return name;
163         }
164 
165         char[] c = name.toLowerCase().trim().toCharArray();
166 
167         if (c.length > 0) {
168             c[0] = Character.toUpperCase(c[0]);
169         }
170 
171         for (int i = 0; i < c.length; i++) {
172             if (c[i] == ' ') {
173                 c[i + 1] = Character.toUpperCase(c[i + 1]);
174             }
175         }
176 
177         return new String(c);
178     }
179 
180     public static String formatPlural(String s) {
181         if (Validator.isNull(s)) {
182             return s;
183         }
184 
185         if (s.endsWith("s")) {
186             s = s.substring(0, s.length() -1) + "ses";
187         }
188         else if (s.endsWith("y")) {
189             s = s.substring(0, s.length() -1) + "ies";
190         }
191         else {
192             s = s + "s";
193         }
194 
195         return s;
196     }
197 
198     private static String _formatA(String s) {
199         return StringUtil.replace(
200             s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
201     }
202 
203     private static String _formatB(String s) {
204         return StringUtil.replace(
205             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
206     }
207 
208     private static String _formatC(String s) {
209         return StringUtil.replace(
210             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
211     }
212 
213     private static String _formatD(String s) {
214         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
215     }
216 
217     private static String _formatE(String s) {
218         return s.toLowerCase();
219     }
220 
221     private static String _formatF(String s) {
222         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
223         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
224 
225         return s;
226     }
227 
228     private static String _formatG(String s) {
229         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
230     }
231 
232     private static String _formatH(String s) {
233         StringBuilder sb = new StringBuilder();
234 
235         char[] c = s.toCharArray();
236 
237         for (int i = 0; i < c.length; i++) {
238             if (Character.isUpperCase(c[i])) {
239                 sb.append(StringPool.SPACE);
240                 sb.append(Character.toLowerCase(c[i]));
241             }
242             else {
243                 sb.append(c[i]);
244             }
245         }
246 
247         return sb.toString();
248     }
249 
250     private static String _formatI(String s) {
251         if (s.length() == 1) {
252             return s.toLowerCase();
253         }
254 
255         if (Character.isUpperCase(s.charAt(0)) &&
256             Character.isLowerCase(s.charAt(1))) {
257 
258             return Character.toLowerCase(s.charAt(0)) +
259                 s.substring(1, s.length());
260         }
261 
262         StringBuilder sb = new StringBuilder();
263 
264         char[] c = s.toCharArray();
265 
266         for (int i = 0; i < c.length; i++) {
267             if ((i + 1 != c.length) &&
268                 (Character.isLowerCase(c[i + 1]))) {
269 
270                 sb.append(s.substring(i, c.length));
271 
272                 break;
273             }
274             else {
275                 sb.append(Character.toLowerCase(c[i]));
276             }
277         }
278 
279         return sb.toString();
280     }
281 
282     private static String _formatJ(String s) {
283         StringBuilder sb = new StringBuilder();
284 
285         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
286         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
287 
288         char[] c = s.toCharArray();
289 
290         for (int i = 0; i < c.length; i++) {
291             if ((i == 0) || (c[i - 1] == ' ')) {
292                 sb.append(Character.toUpperCase(c[i]));
293             }
294             else {
295                 sb.append(Character.toLowerCase(c[i]));
296             }
297         }
298 
299         return sb.toString();
300     }
301 
302     private static String _formatK(String s) {
303         s = _formatH(s);
304         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
305 
306         return s;
307     }
308 
309     private static String _formatL(String s) {
310         if (s.length() == 1) {
311             return s.toLowerCase();
312         }
313         else if (Character.isUpperCase(s.charAt(0)) &&
314                  Character.isUpperCase(s.charAt(1))) {
315 
316             return s;
317         }
318         else {
319             return Character.toLowerCase(s.charAt(0)) + s.substring(1);
320         }
321     }
322 
323     private static String _formatM(String s) {
324         StringBuilder sb = new StringBuilder();
325 
326         char[] c = s.toCharArray();
327 
328         for (int i = 0; i < c.length; i++) {
329             if (c[i] == '-') {
330             }
331             else if ((i > 0) && (c[i - 1] == '-')) {
332                 sb.append(Character.toUpperCase(c[i]));
333             }
334             else {
335                 sb.append(c[i]);
336             }
337         }
338 
339         return sb.toString();
340     }
341 
342     private static String _formatN(String s) {
343         return StringUtil.replace(s, StringPool.DASH, StringPool.UNDERLINE);
344     }
345 
346 }