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("y")) {
156             s = s.substring(0, s.length() -1) + "ies";
157         }
158         else {
159             s = s + "s";
160         }
161 
162         return s;
163     }
164 
165     private static String _formatA(String s) {
166         return StringUtil.replace(
167             s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
168     }
169 
170     private static String _formatB(String s) {
171         return StringUtil.replace(
172             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
173     }
174 
175     private static String _formatC(String s) {
176         return StringUtil.replace(
177             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
178     }
179 
180     private static String _formatD(String s) {
181         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
182     }
183 
184     private static String _formatE(String s) {
185         return s.toLowerCase();
186     }
187 
188     private static String _formatF(String s) {
189         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
190         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
191 
192         return s;
193     }
194 
195     private static String _formatG(String s) {
196         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
197     }
198 
199     private static String _formatH(String s) {
200         StringBuilder sb = new StringBuilder();
201 
202         char[] c = s.toCharArray();
203 
204         for (int i = 0; i < c.length; i++) {
205             if (Character.isUpperCase(c[i])) {
206                 sb.append(StringPool.SPACE);
207                 sb.append(Character.toLowerCase(c[i]));
208             }
209             else {
210                 sb.append(c[i]);
211             }
212         }
213 
214         return sb.toString();
215     }
216 
217     private static String _formatI(String s) {
218         if (s.length() == 1) {
219             return s.toLowerCase();
220         }
221 
222         if (Character.isUpperCase(s.charAt(0)) &&
223             Character.isLowerCase(s.charAt(1))) {
224 
225             return Character.toLowerCase(s.charAt(0)) +
226                 s.substring(1, s.length());
227         }
228 
229         StringBuilder sb = new StringBuilder();
230 
231         char[] c = s.toCharArray();
232 
233         for (int i = 0; i < c.length; i++) {
234             if ((i + 1 != c.length) &&
235                 (Character.isLowerCase(c[i + 1]))) {
236 
237                 sb.append(s.substring(i, c.length));
238 
239                 break;
240             }
241             else {
242                 sb.append(Character.toLowerCase(c[i]));
243             }
244         }
245 
246         return sb.toString();
247     }
248 
249     private static String _formatJ(String s) {
250         StringBuilder sb = new StringBuilder();
251 
252         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
253         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
254 
255         char[] c = s.toCharArray();
256 
257         for (int i = 0; i < c.length; i++) {
258             if ((i == 0) || (c[i - 1] == ' ')) {
259                 sb.append(Character.toUpperCase(c[i]));
260             }
261             else {
262                 sb.append(Character.toLowerCase(c[i]));
263             }
264         }
265 
266         return sb.toString();
267     }
268 
269     private static String _formatK(String s) {
270         s = _formatH(s);
271         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
272 
273         return s;
274     }
275 
276 }