1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TextFormatter {
39  
40      // Web Search --> WEB_SEARCH
41  
42      public static final int A = 0;
43  
44      // Web Search --> websearch
45  
46      public static final int B = 1;
47  
48      // Web Search --> web_search
49  
50      public static final int C = 2;
51  
52      // Web Search --> WebSearch
53  
54      public static final int D = 3;
55  
56      // Web Search --> web search
57  
58      public static final int E = 4;
59  
60      // Web Search --> webSearch
61  
62      public static final int F = 5;
63  
64      // formatId --> FormatId
65  
66      public static final int G = 6;
67  
68      // formatId --> format id
69  
70      public static final int H = 7;
71  
72      // FormatId --> formatId
73  
74      public static final int I = 8;
75  
76      // format-id --> Format Id
77  
78      public static final int J = 9;
79  
80      // formatId --> format-id
81  
82      public static final int K = 10;
83  
84      // FormatId --> formatId, FOrmatId --> FOrmatId
85  
86      public static final int L = 11;
87  
88      // format-id --> formatId
89  
90      public static final int M = 12;
91  
92      // format-id --> format_id
93  
94      public static final int N = 13;
95  
96      // format_id --> format-id
97  
98      public static final int O = 14;
99  
100     public static String format(String s, int style) {
101         if (Validator.isNull(s)) {
102             return null;
103         }
104 
105         s = s.trim();
106 
107         if (style == A) {
108             return _formatA(s);
109         }
110         else if (style == B) {
111             return _formatB(s);
112         }
113         else if (style == C) {
114             return _formatC(s);
115         }
116         else if (style == D) {
117             return _formatD(s);
118         }
119         else if (style == E) {
120             return _formatE(s);
121         }
122         else if (style == F) {
123             return _formatF(s);
124         }
125         else if (style == G) {
126             return _formatG(s);
127         }
128         else if (style == H) {
129             return _formatH(s);
130         }
131         else if (style == I) {
132             return _formatI(s);
133         }
134         else if (style == J) {
135             return _formatJ(s);
136         }
137         else if (style == K) {
138             return _formatK(s);
139         }
140         else if (style == L) {
141             return _formatL(s);
142         }
143         else if (style == M) {
144             return _formatM(s);
145         }
146         else if (style == N) {
147             return _formatN(s);
148         }
149         else if (style == O) {
150             return _formatO(s);
151         }
152         else {
153             return s;
154         }
155     }
156 
157     public static String formatKB(double size, Locale locale) {
158         NumberFormat nf = NumberFormat.getInstance(locale);
159         nf.setMaximumFractionDigits(1);
160         nf.setMinimumFractionDigits(1);
161 
162         return nf.format(size / 1024.0);
163     }
164 
165     public static String formatKB(int size, Locale locale) {
166         return formatKB((double)size, locale);
167     }
168 
169     public static String formatName(String name) {
170         if (Validator.isNull(name)) {
171             return name;
172         }
173 
174         char[] c = name.toLowerCase().trim().toCharArray();
175 
176         if (c.length > 0) {
177             c[0] = Character.toUpperCase(c[0]);
178         }
179 
180         for (int i = 0; i < c.length; i++) {
181             if (c[i] == ' ') {
182                 c[i + 1] = Character.toUpperCase(c[i + 1]);
183             }
184         }
185 
186         return new String(c);
187     }
188 
189     public static String formatPlural(String s) {
190         if (Validator.isNull(s)) {
191             return s;
192         }
193 
194         if (s.endsWith("s")) {
195             s = s.substring(0, s.length() -1) + "ses";
196         }
197         else if (s.endsWith("y")) {
198             s = s.substring(0, s.length() -1) + "ies";
199         }
200         else {
201             s = s + "s";
202         }
203 
204         return s;
205     }
206 
207     private static String _formatA(String s) {
208         return StringUtil.replace(
209             s.toUpperCase(), StringPool.SPACE, StringPool.UNDERLINE);
210     }
211 
212     private static String _formatB(String s) {
213         return StringUtil.replace(
214             s.toLowerCase(), StringPool.SPACE, StringPool.BLANK);
215     }
216 
217     private static String _formatC(String s) {
218         return StringUtil.replace(
219             s.toLowerCase(), StringPool.SPACE, StringPool.UNDERLINE);
220     }
221 
222     private static String _formatD(String s) {
223         return StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
224     }
225 
226     private static String _formatE(String s) {
227         return s.toLowerCase();
228     }
229 
230     private static String _formatF(String s) {
231         s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
232         s = Character.toLowerCase(s.charAt(0)) + s.substring(1, s.length());
233 
234         return s;
235     }
236 
237     private static String _formatG(String s) {
238         return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
239     }
240 
241     private static String _formatH(String s) {
242         StringBuilder sb = new StringBuilder();
243 
244         char[] c = s.toCharArray();
245 
246         for (int i = 0; i < c.length; i++) {
247             if (Character.isUpperCase(c[i])) {
248                 sb.append(StringPool.SPACE);
249                 sb.append(Character.toLowerCase(c[i]));
250             }
251             else {
252                 sb.append(c[i]);
253             }
254         }
255 
256         return sb.toString();
257     }
258 
259     private static String _formatI(String s) {
260         if (s.length() == 1) {
261             return s.toLowerCase();
262         }
263 
264         if (Character.isUpperCase(s.charAt(0)) &&
265             Character.isLowerCase(s.charAt(1))) {
266 
267             return Character.toLowerCase(s.charAt(0)) +
268                 s.substring(1, s.length());
269         }
270 
271         StringBuilder sb = new StringBuilder();
272 
273         char[] c = s.toCharArray();
274 
275         for (int i = 0; i < c.length; i++) {
276             if ((i + 1 != c.length) &&
277                 (Character.isLowerCase(c[i + 1]))) {
278 
279                 sb.append(s.substring(i, c.length));
280 
281                 break;
282             }
283             else {
284                 sb.append(Character.toLowerCase(c[i]));
285             }
286         }
287 
288         return sb.toString();
289     }
290 
291     private static String _formatJ(String s) {
292         StringBuilder sb = new StringBuilder();
293 
294         s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
295         s = StringUtil.replace(s, StringPool.UNDERLINE, StringPool.SPACE);
296 
297         char[] c = s.toCharArray();
298 
299         for (int i = 0; i < c.length; i++) {
300             if ((i == 0) || (c[i - 1] == ' ')) {
301                 sb.append(Character.toUpperCase(c[i]));
302             }
303             else {
304                 sb.append(Character.toLowerCase(c[i]));
305             }
306         }
307 
308         return sb.toString();
309     }
310 
311     private static String _formatK(String s) {
312         s = _formatH(s);
313         s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
314 
315         return s;
316     }
317 
318     private static String _formatL(String s) {
319         if (s.length() == 1) {
320             return s.toLowerCase();
321         }
322         else if (Character.isUpperCase(s.charAt(0)) &&
323                  Character.isUpperCase(s.charAt(1))) {
324 
325             return s;
326         }
327         else {
328             return Character.toLowerCase(s.charAt(0)) + s.substring(1);
329         }
330     }
331 
332     private static String _formatM(String s) {
333         StringBuilder sb = new StringBuilder();
334 
335         char[] c = s.toCharArray();
336 
337         for (int i = 0; i < c.length; i++) {
338             if (c[i] == '-') {
339             }
340             else if ((i > 0) && (c[i - 1] == '-')) {
341                 sb.append(Character.toUpperCase(c[i]));
342             }
343             else {
344                 sb.append(c[i]);
345             }
346         }
347 
348         return sb.toString();
349     }
350 
351     private static String _formatN(String s) {
352         return StringUtil.replace(s, StringPool.DASH, StringPool.UNDERLINE);
353     }
354 
355     private static String _formatO(String s) {
356         return StringUtil.replace(s, StringPool.UNDERLINE, StringPool.DASH);
357     }
358 
359 }