1 43 package net.jforum.util; 44 45 52 public class URLNormalizer 53 { 54 public static final int LIMIT = 30; 55 56 63 public static String normalize(String url) 64 { 65 return normalize(url, LIMIT, true); 66 } 67 68 77 public static String normalize(String url, int limit, boolean friendlyTruncate) 78 { 79 char[] chars = url.toCharArray(); 80 81 StringBuffer sb = new StringBuffer (url.length()); 82 83 for (int i = 0; i < chars.length; i++) { 84 if (i <= limit || (friendlyTruncate && i > limit && sb.charAt(sb.length() - 1) != '_')) { 85 86 if (Character.isSpaceChar(chars[i]) || chars[i] == '-') { 87 if (friendlyTruncate && i > limit) { 88 break; 89 } 90 91 if (i > 0 && sb.charAt(sb.length() - 1) != '_') { 92 sb.append('_'); 93 } 94 } 95 96 if (Character.isLetterOrDigit(chars[i])) { 97 sb.append(chars[i]); 98 } 99 else if (friendlyTruncate && i > limit) { 100 break; 101 } 102 } 103 } 104 105 return sb.toString().toLowerCase(); 106 } 107 } 108 | Popular Tags |