001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
027:
028: import java.text.NumberFormat;
029:
030: import java.util.Locale;
031:
032: /**
033: * <a href="TextFormatter.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class TextFormatter {
039:
040: // Web Search --> WEB_SEARCH
041: // Web Search --> websearch
042: // Web Search --> web_search
043: // Web Search --> WebSearch
044: // Web Search --> web search
045: // Web Search --> webSearch
046:
047: public static final int A = 0;
048: public static final int B = 1;
049: public static final int C = 2;
050: public static final int D = 3;
051: public static final int E = 4;
052: public static final int F = 5;
053:
054: // formatId --> FormatId
055: // formatId --> format id
056:
057: public static final int G = 6;
058: public static final int H = 7;
059:
060: // FormatId --> formatId
061:
062: public static final int I = 8;
063:
064: // format-id --> Format Id
065:
066: public static final int J = 9;
067:
068: // formatId --> format-id
069:
070: public static final int K = 10;
071:
072: public static String format(String s, int style) {
073: if (Validator.isNull(s)) {
074: return null;
075: }
076:
077: s = s.trim();
078:
079: if (style == A) {
080: return _formatA(s);
081: } else if (style == B) {
082: return _formatB(s);
083: } else if (style == C) {
084: return _formatC(s);
085: } else if (style == D) {
086: return _formatD(s);
087: } else if (style == E) {
088: return _formatE(s);
089: } else if (style == F) {
090: return _formatF(s);
091: } else if (style == G) {
092: return _formatG(s);
093: } else if (style == H) {
094: return _formatH(s);
095: } else if (style == I) {
096: return _formatI(s);
097: } else if (style == J) {
098: return _formatJ(s);
099: } else if (style == K) {
100: return _formatK(s);
101: } else {
102: return s;
103: }
104: }
105:
106: public static String formatKB(double size, Locale locale) {
107: NumberFormat nf = NumberFormat.getInstance(locale);
108: nf.setMaximumFractionDigits(1);
109: nf.setMinimumFractionDigits(1);
110:
111: return nf.format(size / 1024.0);
112: }
113:
114: public static String formatKB(int size, Locale locale) {
115: return formatKB((double) size, locale);
116: }
117:
118: public static String formatName(String name) {
119: if (Validator.isNull(name)) {
120: return name;
121: }
122:
123: char[] c = name.toLowerCase().trim().toCharArray();
124:
125: if (c.length > 0) {
126: c[0] = Character.toUpperCase(c[0]);
127: }
128:
129: for (int i = 0; i < c.length; i++) {
130: if (c[i] == ' ') {
131: c[i + 1] = Character.toUpperCase(c[i + 1]);
132: }
133: }
134:
135: return new String(c);
136: }
137:
138: public static String formatPlural(String s) {
139: if (Validator.isNull(s)) {
140: return s;
141: }
142:
143: if (s.endsWith("y")) {
144: s = s.substring(0, s.length() - 1) + "ies";
145: } else {
146: s = s + "s";
147: }
148:
149: return s;
150: }
151:
152: private static String _formatA(String s) {
153: return StringUtil.replace(s.toUpperCase(), StringPool.SPACE,
154: StringPool.UNDERLINE);
155: }
156:
157: private static String _formatB(String s) {
158: return StringUtil.replace(s.toLowerCase(), StringPool.SPACE,
159: StringPool.BLANK);
160: }
161:
162: private static String _formatC(String s) {
163: return StringUtil.replace(s.toLowerCase(), StringPool.SPACE,
164: StringPool.UNDERLINE);
165: }
166:
167: private static String _formatD(String s) {
168: return StringUtil
169: .replace(s, StringPool.SPACE, StringPool.BLANK);
170: }
171:
172: private static String _formatE(String s) {
173: return s.toLowerCase();
174: }
175:
176: private static String _formatF(String s) {
177: s = StringUtil.replace(s, StringPool.SPACE, StringPool.BLANK);
178: s = Character.toLowerCase(s.charAt(0))
179: + s.substring(1, s.length());
180:
181: return s;
182: }
183:
184: private static String _formatG(String s) {
185: return s.substring(0, 1).toUpperCase()
186: + s.substring(1, s.length());
187: }
188:
189: private static String _formatH(String s) {
190: StringMaker sm = new StringMaker();
191:
192: char[] c = s.toCharArray();
193:
194: for (int i = 0; i < c.length; i++) {
195: if (Character.isUpperCase(c[i])) {
196: sm.append(StringPool.SPACE);
197: sm.append(Character.toLowerCase(c[i]));
198: } else {
199: sm.append(c[i]);
200: }
201: }
202:
203: return sm.toString();
204: }
205:
206: private static String _formatI(String s) {
207: if (s.length() == 1) {
208: return s.toLowerCase();
209: }
210:
211: if (Character.isUpperCase(s.charAt(0))
212: && Character.isLowerCase(s.charAt(1))) {
213:
214: return Character.toLowerCase(s.charAt(0))
215: + s.substring(1, s.length());
216: }
217:
218: StringMaker sm = new StringMaker();
219:
220: char[] c = s.toCharArray();
221:
222: for (int i = 0; i < c.length; i++) {
223: if ((i + 1 != c.length)
224: && (Character.isLowerCase(c[i + 1]))) {
225:
226: sm.append(s.substring(i, c.length));
227:
228: break;
229: } else {
230: sm.append(Character.toLowerCase(c[i]));
231: }
232: }
233:
234: return sm.toString();
235: }
236:
237: private static String _formatJ(String s) {
238: StringMaker sm = new StringMaker();
239:
240: s = StringUtil.replace(s, StringPool.DASH, StringPool.SPACE);
241: s = StringUtil.replace(s, StringPool.UNDERLINE,
242: StringPool.SPACE);
243:
244: char[] c = s.toCharArray();
245:
246: for (int i = 0; i < c.length; i++) {
247: if ((i == 0) || (c[i - 1] == ' ')) {
248: sm.append(Character.toUpperCase(c[i]));
249: } else {
250: sm.append(Character.toLowerCase(c[i]));
251: }
252: }
253:
254: return sm.toString();
255: }
256:
257: private static String _formatK(String s) {
258: s = _formatH(s);
259: s = StringUtil.replace(s, StringPool.SPACE, StringPool.DASH);
260:
261: return s;
262: }
263:
264: }
|