1 package org.javabb.infra; 2 3 import java.security.MessageDigest ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Random ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 26 27 32 public class Utils { 33 private static final Log LOG = LogFactory.getLog(Utils.class); 34 35 private static final Random RANDOM = new Random (); 36 37 42 public static String encrypt(String str) { 43 String sign = null; 44 45 try { 46 MessageDigest md = MessageDigest.getInstance("MD5"); 47 md.update(str.getBytes()); 48 49 byte[] hash = md.digest(); 50 StringBuffer hexString = new StringBuffer (); 51 52 for (int i = 0; i < hash.length; i++) { 53 if ((0xff & hash[i]) < 0x10) { 54 hexString.append("0" + Integer.toHexString((0xFF & hash[i]))); 55 } else { 56 hexString.append(Integer.toHexString(0xFF & hash[i])); 57 } 58 } 59 60 sign = hexString.toString(); 61 } catch (Exception e) { 62 LOG.error(e.getMessage(), e); 63 } 64 return sign; 65 } 66 67 71 public static String randomNumber() { 72 if (System.currentTimeMillis() % 10000 == 0) { 73 RANDOM.setSeed(System.currentTimeMillis()); 74 } 75 return String.valueOf(RANDOM.nextLong()); 76 } 77 78 83 public static String validateWebSite(String ws) { 84 if ((ws != null) && (ws.length() > 0)) { 85 ws = ws.replaceAll("http://", ""); 86 ws = "http://" + ws; 87 88 return ws; 89 } 90 return ""; 91 } 92 93 97 public static String verifyURLs(String text) { 98 final String regex = "([\\s\\n\\r\\t\\.,]|^)((?:http://|https://|ftp://)(?:\\S*))"; 99 final String replacement = "$1[url]$2[/url]"; 100 101 try { 103 text = text.replaceAll(regex, replacement); 104 } catch (RuntimeException e) { 105 LOG.error(e.getMessage(), e); 106 } 107 108 final String wwwregex = "([\\s\\n\\r\\t\\.,]|^)(www\\.\\S*)"; 109 final String wwwreplacement = "$1[url=\"http://$2\"]$2[/url]"; 110 111 try { 113 text = text.replaceAll(wwwregex, wwwreplacement); 114 } catch (RuntimeException e) { 115 LOG.error(e.getMessage(), e); 116 } 117 118 final String emailregex = "([\\s\\n\\r\\t\\.,]|^)([\\w-]+(?:\\.[\\w-]+)*@[\\w-]+\\.[\\w-]+(?:\\.[\\w-]+)*)"; 119 final String emailreplacement = "$1[url=\"mailto:$2\"]$2[/url]"; 120 121 try { 123 text = text.replaceAll(emailregex, emailreplacement); 124 } catch (RuntimeException e) { 125 LOG.error(e.getMessage(), e); 126 } 127 128 return text; 129 } 130 131 135 public static String replaceHTML(String texto) { 136 return texto.replaceAll("<", "<").replaceAll(">", ">"); 137 } 138 139 143 public static String getCodeUser(String userName) { 144 String codeUser = userName + System.currentTimeMillis() + Utils.randomNumber(); 145 return Utils.encrypt(codeUser); 146 } 147 148 152 public static String avoidNull(String string) { 153 154 return string != null ? string : ""; 155 } 156 157 164 public static List indexOf(String text, String key){ 165 if(text == null || key == null){ 166 return null; 167 } 168 List lst = new ArrayList (); 169 int i = 0; 170 while(true){ 171 int occur = text.indexOf(key, ((i>text.length())?text.length():i)); 172 if(occur == -1){ 173 break; 174 } else { 175 lst.add(new Integer (occur)); 176 i= occur + 1; 177 } 178 } 179 return lst; 180 } 181 182 183 public static boolean isBetween(int index, List initCodePos, List finalCodePos){ 184 for(int y=0; y<initCodePos.size(); y++){ 185 int init = ((Integer ) initCodePos.get(y)).intValue(); 186 int limit = ((Integer ) finalCodePos.get(y)).intValue(); 187 LOG.info("init:" + init + " limit:" + limit+ " index:" + index); 188 if(index > init && index < limit){ 189 return true; 190 } 191 } 192 return false; 193 } 194 195 196 public static void main(String [] args){ 197 199 System.out.println(Utils.indexOf("dalt:)on ca:)m..:)", ":)")); 200 201 } 202 } 203 | Popular Tags |