1 package net.javacoding.jspider.core.util; 2 3 import java.net.URL ; 4 import java.net.MalformedURLException ; 5 6 9 public class EMailAddressUtil { 10 11 public static final String PROTOCOL_PREFIX = "mailto:"; 12 13 public static boolean canBeEMailAddress(String urlString) { 14 boolean result = false; 15 URL url = fixEMailAddress(urlString); 16 17 if ( url != null ) { 18 result = isEMailAddress(fixEMailAddress(urlString)); 19 } 20 21 return result; 22 } 23 24 public static URL fixEMailAddress(String urlString) { 25 URL result = null; 26 try { 27 result = new URL (urlString); 28 } catch (MalformedURLException e) { 29 try { 30 result = new URL (PROTOCOL_PREFIX + urlString); 31 } catch (MalformedURLException ex) { } 32 } 33 34 if (result == null || !isEMailAddress(result)) { 35 result = null; 36 } 37 38 return result; 39 } 40 41 public static boolean isEMailAddress(URL url) { 42 boolean result = true; 43 44 String urlString = url.toString(); 45 46 if (url.getHost() != null && !url.getHost().trim().equals("")) { 47 result = false; 48 } 49 if (url.getPort() != -1) { 50 result = false; 51 } 52 53 int pos = urlString.indexOf('@'); 54 if (pos == -1) { 55 result = false; 56 } else { 57 if (urlString.indexOf('@', pos + 1) != -1) { 59 return false; 60 } 61 } 62 63 if ( pos > (urlString.length() - 6) ) { 65 result = false; 66 } 67 68 return result; 69 70 } 71 72 public static String getEMailAddress(URL url) { 73 String urlString = url.toString(); 74 if (urlString.toLowerCase().startsWith(PROTOCOL_PREFIX)) { 75 urlString = urlString.substring(PROTOCOL_PREFIX.length()); 76 } else { 77 urlString = null; 78 } 79 return urlString; 80 } 81 82 } 83 | Popular Tags |