@Test public void testAutoLink() { String msg = "普通文本abc"; String html = IMUtils.autoLink(msg); System.out.println(html); assertEquals(msg, html); msg = "中 www.baidu.com abc"; html = IMUtils.autoLink(msg); System.out.println(html); assertEquals("中 <a href=\"http: html); html = IMUtils.autoLink(html); System.out.println(html); assertEquals("中 <a href=\"http: html); msg = "<a href=\"file: html = IMUtils.autoLink(msg); assertEquals(msg, html); }
public static String autoLink(String input) { Pattern p = Patterns.WEB_URL; Matcher m = p.matcher(input); List<String> groups = new ArrayList<>(); List<Integer> starts = new ArrayList<>(); List<Integer> ends = new ArrayList<>(); while (m.find()) { starts.add(m.start()); ends.add(m.end()); groups.add(m.group()); } if (!starts.isEmpty()) { StringBuilder sb = new StringBuilder(input); int offset = 0; for (int i = 0; i < starts.size(); i++) { int s = starts.get(i); int e = ends.get(i); String g = groups.get(i); String http = null; String ucs = ""; String rg = UCS_REGEX_BEGIN.matcher(g).replaceAll("$2"); if (g.length() > rg.length()) { ucs = g.substring(0, g.length() - rg.length()); g = rg; s = s + ucs.length(); } rg = UCS_REGEX_END.matcher(g).replaceAll("$1"); if (g.length() > rg.length()) { ucs = g.substring(rg.length()); g = rg; e = e - ucs.length(); } if (!PROTOCOL.matcher(g).find()) { boolean f = g.startsWith("www.") || g.endsWith(".com") || g.endsWith(".cn"); if (!f) { continue; } else { http = "http: } } int pos = offset + s; if (pos > 2) { char c = sb.charAt(pos - 1); if (c == '\'' || c == '"') { c = sb.charAt(pos - 2); if (c == '=') { continue; } } else if (c == '>') { continue; } } sb.delete(offset + s, offset + e); String link = http == null ? g : http + g; String ng = g; if (IMG_EXTS.indexOf(FileUtils.getExtension(g).toLowerCase()) >= 0) { ng = String.format("<a href=\"%s\"><img src=\"%s\" alt=\"%s\" border=\"0\"/></a>", link, link, "无法预览，请尝试点击"); } else { ng = String.format("<a href=\"%s\">%s</a>", link, g); } sb.insert(offset + s, ng); offset += ng.length() - g.length(); } return sb.toString(); } return input; }
IMUtils { public static String autoLink(String input) { Pattern p = Patterns.WEB_URL; Matcher m = p.matcher(input); List<String> groups = new ArrayList<>(); List<Integer> starts = new ArrayList<>(); List<Integer> ends = new ArrayList<>(); while (m.find()) { starts.add(m.start()); ends.add(m.end()); groups.add(m.group()); } if (!starts.isEmpty()) { StringBuilder sb = new StringBuilder(input); int offset = 0; for (int i = 0; i < starts.size(); i++) { int s = starts.get(i); int e = ends.get(i); String g = groups.get(i); String http = null; String ucs = ""; String rg = UCS_REGEX_BEGIN.matcher(g).replaceAll("$2"); if (g.length() > rg.length()) { ucs = g.substring(0, g.length() - rg.length()); g = rg; s = s + ucs.length(); } rg = UCS_REGEX_END.matcher(g).replaceAll("$1"); if (g.length() > rg.length()) { ucs = g.substring(rg.length()); g = rg; e = e - ucs.length(); } if (!PROTOCOL.matcher(g).find()) { boolean f = g.startsWith("www.") || g.endsWith(".com") || g.endsWith(".cn"); if (!f) { continue; } else { http = "http: } } int pos = offset + s; if (pos > 2) { char c = sb.charAt(pos - 1); if (c == '\'' || c == '"') { c = sb.charAt(pos - 2); if (c == '=') { continue; } } else if (c == '>') { continue; } } sb.delete(offset + s, offset + e); String link = http == null ? g : http + g; String ng = g; if (IMG_EXTS.indexOf(FileUtils.getExtension(g).toLowerCase()) >= 0) { ng = String.format("<a href=\"%s\"><img src=\"%s\" alt=\"%s\" border=\"0\"/></a>", link, link, "无法预览，请尝试点击"); } else { ng = String.format("<a href=\"%s\">%s</a>", link, g); } sb.insert(offset + s, ng); offset += ng.length() - g.length(); } return sb.toString(); } return input; } }
IMUtils { public static String autoLink(String input) { Pattern p = Patterns.WEB_URL; Matcher m = p.matcher(input); List<String> groups = new ArrayList<>(); List<Integer> starts = new ArrayList<>(); List<Integer> ends = new ArrayList<>(); while (m.find()) { starts.add(m.start()); ends.add(m.end()); groups.add(m.group()); } if (!starts.isEmpty()) { StringBuilder sb = new StringBuilder(input); int offset = 0; for (int i = 0; i < starts.size(); i++) { int s = starts.get(i); int e = ends.get(i); String g = groups.get(i); String http = null; String ucs = ""; String rg = UCS_REGEX_BEGIN.matcher(g).replaceAll("$2"); if (g.length() > rg.length()) { ucs = g.substring(0, g.length() - rg.length()); g = rg; s = s + ucs.length(); } rg = UCS_REGEX_END.matcher(g).replaceAll("$1"); if (g.length() > rg.length()) { ucs = g.substring(rg.length()); g = rg; e = e - ucs.length(); } if (!PROTOCOL.matcher(g).find()) { boolean f = g.startsWith("www.") || g.endsWith(".com") || g.endsWith(".cn"); if (!f) { continue; } else { http = "http: } } int pos = offset + s; if (pos > 2) { char c = sb.charAt(pos - 1); if (c == '\'' || c == '"') { c = sb.charAt(pos - 2); if (c == '=') { continue; } } else if (c == '>') { continue; } } sb.delete(offset + s, offset + e); String link = http == null ? g : http + g; String ng = g; if (IMG_EXTS.indexOf(FileUtils.getExtension(g).toLowerCase()) >= 0) { ng = String.format("<a href=\"%s\"><img src=\"%s\" alt=\"%s\" border=\"0\"/></a>", link, link, "无法预览，请尝试点击"); } else { ng = String.format("<a href=\"%s\">%s</a>", link, g); } sb.insert(offset + s, ng); offset += ng.length() - g.length(); } return sb.toString(); } return input; }  }
IMUtils { public static String autoLink(String input) { Pattern p = Patterns.WEB_URL; Matcher m = p.matcher(input); List<String> groups = new ArrayList<>(); List<Integer> starts = new ArrayList<>(); List<Integer> ends = new ArrayList<>(); while (m.find()) { starts.add(m.start()); ends.add(m.end()); groups.add(m.group()); } if (!starts.isEmpty()) { StringBuilder sb = new StringBuilder(input); int offset = 0; for (int i = 0; i < starts.size(); i++) { int s = starts.get(i); int e = ends.get(i); String g = groups.get(i); String http = null; String ucs = ""; String rg = UCS_REGEX_BEGIN.matcher(g).replaceAll("$2"); if (g.length() > rg.length()) { ucs = g.substring(0, g.length() - rg.length()); g = rg; s = s + ucs.length(); } rg = UCS_REGEX_END.matcher(g).replaceAll("$1"); if (g.length() > rg.length()) { ucs = g.substring(rg.length()); g = rg; e = e - ucs.length(); } if (!PROTOCOL.matcher(g).find()) { boolean f = g.startsWith("www.") || g.endsWith(".com") || g.endsWith(".cn"); if (!f) { continue; } else { http = "http: } } int pos = offset + s; if (pos > 2) { char c = sb.charAt(pos - 1); if (c == '\'' || c == '"') { c = sb.charAt(pos - 2); if (c == '=') { continue; } } else if (c == '>') { continue; } } sb.delete(offset + s, offset + e); String link = http == null ? g : http + g; String ng = g; if (IMG_EXTS.indexOf(FileUtils.getExtension(g).toLowerCase()) >= 0) { ng = String.format("<a href=\"%s\"><img src=\"%s\" alt=\"%s\" border=\"0\"/></a>", link, link, "无法预览，请尝试点击"); } else { ng = String.format("<a href=\"%s\">%s</a>", link, g); } sb.insert(offset + s, ng); offset += ng.length() - g.length(); } return sb.toString(); } return input; }  static String getName(String path); static String formatFileSize(long length); static boolean isEmpty(CharSequence text); static boolean isEmpty(Collection<?> list); static String encodeHtml(String msg); static String formatMsg(long time, String name, CharSequence msg); static boolean isMySendMsg(String raw); static String formatHtmlMsg(String msg, boolean encodeHtml); static String formatHtmlMsg(long time, String name, CharSequence msg); static String formatHtmlMyMsg(long time, String name, CharSequence msg); static String formatHtmlMsg(boolean my, boolean encodeHtml, long time, String name, String msg); static String autoReviewLink(String input); static String autoLink(String input); }
IMUtils { public static String autoLink(String input) { Pattern p = Patterns.WEB_URL; Matcher m = p.matcher(input); List<String> groups = new ArrayList<>(); List<Integer> starts = new ArrayList<>(); List<Integer> ends = new ArrayList<>(); while (m.find()) { starts.add(m.start()); ends.add(m.end()); groups.add(m.group()); } if (!starts.isEmpty()) { StringBuilder sb = new StringBuilder(input); int offset = 0; for (int i = 0; i < starts.size(); i++) { int s = starts.get(i); int e = ends.get(i); String g = groups.get(i); String http = null; String ucs = ""; String rg = UCS_REGEX_BEGIN.matcher(g).replaceAll("$2"); if (g.length() > rg.length()) { ucs = g.substring(0, g.length() - rg.length()); g = rg; s = s + ucs.length(); } rg = UCS_REGEX_END.matcher(g).replaceAll("$1"); if (g.length() > rg.length()) { ucs = g.substring(rg.length()); g = rg; e = e - ucs.length(); } if (!PROTOCOL.matcher(g).find()) { boolean f = g.startsWith("www.") || g.endsWith(".com") || g.endsWith(".cn"); if (!f) { continue; } else { http = "http: } } int pos = offset + s; if (pos > 2) { char c = sb.charAt(pos - 1); if (c == '\'' || c == '"') { c = sb.charAt(pos - 2); if (c == '=') { continue; } } else if (c == '>') { continue; } } sb.delete(offset + s, offset + e); String link = http == null ? g : http + g; String ng = g; if (IMG_EXTS.indexOf(FileUtils.getExtension(g).toLowerCase()) >= 0) { ng = String.format("<a href=\"%s\"><img src=\"%s\" alt=\"%s\" border=\"0\"/></a>", link, link, "无法预览，请尝试点击"); } else { ng = String.format("<a href=\"%s\">%s</a>", link, g); } sb.insert(offset + s, ng); offset += ng.length() - g.length(); } return sb.toString(); } return input; }  static String getName(String path); static String formatFileSize(long length); static boolean isEmpty(CharSequence text); static boolean isEmpty(Collection<?> list); static String encodeHtml(String msg); static String formatMsg(long time, String name, CharSequence msg); static boolean isMySendMsg(String raw); static String formatHtmlMsg(String msg, boolean encodeHtml); static String formatHtmlMsg(long time, String name, CharSequence msg); static String formatHtmlMyMsg(long time, String name, CharSequence msg); static String formatHtmlMsg(boolean my, boolean encodeHtml, long time, String name, String msg); static String autoReviewLink(String input); static String autoLink(String input); static final String DIV_SENDER_FORMAT; static final String DIV_CONTENT_FORMAT; static final String DIV_ROW_FORMAT; static final List<String> IMG_EXTS; static final String CODE_REGEX; static final String LINK_REGEX; static final String UCS_CHAR; static final Pattern UCS_REGEX_END; static final Pattern UCS_REGEX_BEGIN; static final Pattern PROTOCOL; }