1 20 21 package org.jivesoftware.whack.util; 22 23 import java.io.UnsupportedEncodingException ; 24 import java.security.MessageDigest ; 25 import java.security.NoSuchAlgorithmException ; 26 import java.util.Random ; 27 28 31 public class StringUtils { 32 33 private static final char[] QUOTE_ENCODE = """.toCharArray(); 34 private static final char[] AMP_ENCODE = "&".toCharArray(); 35 private static final char[] LT_ENCODE = "<".toCharArray(); 36 private static final char[] GT_ENCODE = ">".toCharArray(); 37 38 46 public static String parseName(String XMPPAddress) { 47 if (XMPPAddress == null) { 48 return null; 49 } 50 int atIndex = XMPPAddress.indexOf("@"); 51 if (atIndex <= 0) { 52 return ""; 53 } 54 else { 55 return XMPPAddress.substring(0, atIndex); 56 } 57 } 58 59 67 public static String parseServer(String XMPPAddress) { 68 if (XMPPAddress == null) { 69 return null; 70 } 71 int atIndex = XMPPAddress.indexOf("@"); 72 if (atIndex + 1 > XMPPAddress.length()) { 74 return ""; 75 } 76 int slashIndex = XMPPAddress.indexOf("/"); 77 if (slashIndex > 0) { 78 return XMPPAddress.substring(atIndex + 1, slashIndex); 79 } 80 else { 81 return XMPPAddress.substring(atIndex + 1); 82 } 83 } 84 85 93 public static String parseResource(String XMPPAddress) { 94 if (XMPPAddress == null) { 95 return null; 96 } 97 int slashIndex = XMPPAddress.indexOf("/"); 98 if (slashIndex + 1 > XMPPAddress.length() || slashIndex < 0) { 99 return ""; 100 } 101 else { 102 return XMPPAddress.substring(slashIndex + 1); 103 } 104 } 105 106 114 public static String parseBareAddress(String XMPPAddress) { 115 if (XMPPAddress == null) { 116 return null; 117 } 118 int slashIndex = XMPPAddress.indexOf("/"); 119 if (slashIndex < 0) { 120 return XMPPAddress; 121 } 122 else if (slashIndex == 0) { 123 return ""; 124 } 125 else { 126 return XMPPAddress.substring(0, slashIndex); 127 } 128 } 129 130 137 public static final String escapeForXML(String string) { 138 if (string == null) { 139 return null; 140 } 141 char ch; 142 int i=0; 143 int last=0; 144 char[] input = string.toCharArray(); 145 int len = input.length; 146 StringBuffer out = new StringBuffer ((int)(len*1.3)); 147 for (; i < len; i++) { 148 ch = input[i]; 149 if (ch > '>') { 150 continue; 151 } 152 else if (ch == '<') { 153 if (i > last) { 154 out.append(input, last, i - last); 155 } 156 last = i + 1; 157 out.append(LT_ENCODE); 158 } 159 else if (ch == '>') { 160 if (i > last) { 161 out.append(input, last, i - last); 162 } 163 last = i + 1; 164 out.append(GT_ENCODE); 165 } 166 167 else if (ch == '&') { 168 if (i > last) { 169 out.append(input, last, i - last); 170 } 171 if (!(len > i + 5 173 && input[i + 1] == '#' 174 && Character.isDigit(input[i + 2]) 175 && Character.isDigit(input[i + 3]) 176 && Character.isDigit(input[i + 4]) 177 && input[i + 5] == ';')) { 178 last = i + 1; 179 out.append(AMP_ENCODE); 180 } 181 } 182 else if (ch == '"') { 183 if (i > last) { 184 out.append(input, last, i - last); 185 } 186 last = i + 1; 187 out.append(QUOTE_ENCODE); 188 } 189 } 190 if (last == 0) { 191 return string; 192 } 193 if (i > last) { 194 out.append(input, last, i - last); 195 } 196 return out.toString(); 197 } 198 199 202 private static MessageDigest digest = null; 203 204 220 public synchronized static final String hash(String data) { 221 if (digest == null) { 222 try { 223 digest = MessageDigest.getInstance("SHA-1"); 224 } 225 catch (NoSuchAlgorithmException nsae) { 226 System.err.println("Failed to load the SHA-1 MessageDigest. " + 227 "Jive will be unable to function normally."); 228 } 229 } 230 try { 232 digest.update(data.getBytes("UTF-8")); 233 } 234 catch (UnsupportedEncodingException e) { 235 System.err.println(e); 236 } 237 return encodeHex(digest.digest()); 238 } 239 240 251 public static final String encodeHex(byte[] bytes) { 252 StringBuffer buf = new StringBuffer (bytes.length * 2); 253 int i; 254 255 for (i = 0; i < bytes.length; i++) { 256 if (((int) bytes[i] & 0xff) < 0x10) { 257 buf.append("0"); 258 } 259 buf.append(Long.toString((int) bytes[i] & 0xff, 16)); 260 } 261 return buf.toString(); 262 } 263 264 274 private static final int fillchar = '='; 275 private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 276 + "0123456789+/"; 277 278 284 public static String encodeBase64(String data) { 285 byte [] bytes = null; 286 try { 287 bytes = data.getBytes("ISO-8859-1"); 288 } 289 catch (UnsupportedEncodingException uee) { 290 uee.printStackTrace(); 291 } 292 return encodeBase64(bytes); 293 } 294 295 301 public static String encodeBase64(byte[] data) { 302 int c; 303 int len = data.length; 304 StringBuffer ret = new StringBuffer (((len / 3) + 1) * 4); 305 for (int i = 0; i < len; ++i) { 306 c = (data[i] >> 2) & 0x3f; 307 ret.append(cvt.charAt(c)); 308 c = (data[i] << 4) & 0x3f; 309 if (++i < len) 310 c |= (data[i] >> 4) & 0x0f; 311 312 ret.append(cvt.charAt(c)); 313 if (i < len) { 314 c = (data[i] << 2) & 0x3f; 315 if (++i < len) 316 c |= (data[i] >> 6) & 0x03; 317 318 ret.append(cvt.charAt(c)); 319 } 320 else { 321 ++i; 322 ret.append((char) fillchar); 323 } 324 325 if (i < len) { 326 c = data[i] & 0x3f; 327 ret.append(cvt.charAt(c)); 328 } 329 else { 330 ret.append((char) fillchar); 331 } 332 } 333 return ret.toString(); 334 } 335 336 342 public static byte[] decodeBase64(String data) { 343 byte [] bytes = null; 344 try { 345 bytes = data.getBytes("ISO-8859-1"); 346 return decodeBase64(bytes).getBytes("ISO-8859-1"); 347 } 348 catch (UnsupportedEncodingException uee) { 349 uee.printStackTrace(); 350 } 351 return new byte[] { }; 352 } 353 354 360 private static String decodeBase64(byte[] data) { 361 int c, c1; 362 int len = data.length; 363 StringBuffer ret = new StringBuffer ((len * 3) / 4); 364 for (int i = 0; i < len; ++i) { 365 c = cvt.indexOf(data[i]); 366 ++i; 367 c1 = cvt.indexOf(data[i]); 368 c = ((c << 2) | ((c1 >> 4) & 0x3)); 369 ret.append((char) c); 370 if (++i < len) { 371 c = data[i]; 372 if (fillchar == c) 373 break; 374 375 c = cvt.indexOf(c); 376 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); 377 ret.append((char) c1); 378 } 379 380 if (++i < len) { 381 c1 = data[i]; 382 if (fillchar == c1) 383 break; 384 385 c1 = cvt.indexOf(c1); 386 c = ((c << 6) & 0xc0) | c1; 387 ret.append((char) c); 388 } 389 } 390 return ret.toString(); 391 } 392 393 398 private static Random randGen = new Random (); 399 400 406 private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + 407 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); 408 409 422 public static final String randomString(int length) { 423 if (length < 1) { 424 return null; 425 } 426 char [] randBuffer = new char[length]; 428 for (int i=0; i<randBuffer.length; i++) { 429 randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; 430 } 431 return new String (randBuffer); 432 } 433 434 private StringUtils() { 435 } 437 } | Popular Tags |