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