KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > JahiaString


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.utils;
14
15 import java.util.Locale JavaDoc;
16 import java.security.SecureRandom JavaDoc;
17
18 public class JahiaString {
19
20     public static String JavaDoc adjustStringSize(String JavaDoc str, int size) {
21         if (str == null) // FIXNE : Don't like this null return.
22
return null;
23         if (str.length() > size) {
24             return str.substring(0, size - 2) + "..";
25         } else {
26             StringBuffer JavaDoc emtpyStr = new StringBuffer JavaDoc();
27             for (int i = 0; i < size - str.length(); i++) {
28                 emtpyStr.append(" ");
29             }
30             return str + emtpyStr;
31         }
32     }
33
34     // This method help to support get proper string like Chinese
35
public static String JavaDoc getProperStr(String JavaDoc str, Locale JavaDoc locale) {
36         return str;
37     }
38
39     public static String JavaDoc generateRandomString(int length) {
40         SecureRandom JavaDoc randomGen = new SecureRandom JavaDoc();
41         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
42         int count = 0;
43         while (count < length) {
44             int randomSel = randomGen.nextInt(3);
45             int randomInt = randomGen.nextInt(26);
46             char randomChar = '0';
47             switch (randomSel) {
48                 case 0: randomChar = (char) (((int)'A') + randomInt); break;
49                 case 1: randomChar = (char) (((int)'a') + randomInt); break;
50                 case 2: randomChar = (char) (((int)'0') + (randomInt % 10)); break;
51             }
52             result.append(randomChar);
53             count++;
54         }
55         return result.toString();
56     }
57
58
59
60 }
Popular Tags