// ============================================================================ // // Copyright (C) 2006-2016 Talend Inc. - www.talend.com // // This source code is available under agreement available at // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt // // You should have received a copy of the agreement // along with this program; if not, write to Talend SA // 9 rue Pages 92150 Suresnes, France // // ============================================================================ package org.talend.dataquality.email.CommonCheck; import java.io.Serializable; /** * created by talend on 2014年12月30日 Detailled comment * */ public class InetAddressValidator implements Serializable { private static final long serialVersionUID = -919201640201914789L; private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; /** * Singleton instance of this class. */ private static final InetAddressValidator VALIDATOR = new InetAddressValidator(); /** IPv4 RegexValidator */ private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX); /** * Returns the singleton instance of this validator. * * @return the singleton instance of this validator */ public static InetAddressValidator getInstance() { return VALIDATOR; } /** * Checks if the specified string is a valid IP address. * * @param inetAddress the string to validate * @return true if the string validates as an IP address */ public boolean isValid(String inetAddress) { return isValidInet4Address(inetAddress); } /** * Validates an IPv4 address. Returns true if valid. * * @param inet4Address the IPv4 address to validate * @return true if the argument contains a valid IPv4 address */ public boolean isValidInet4Address(String inet4Address) { // verify that address conforms to generic IPv4 format String[] groups = ipv4Validator.match(inet4Address); if (groups == null) { return false; } // verify that address subgroups are legal for (int i = 0; i <= 3; i++) { String ipSegment = groups[i]; if (ipSegment == null || ipSegment.length() <= 0) { return false; } int iIpSegment = 0; try { iIpSegment = Integer.parseInt(ipSegment); } catch (NumberFormatException e) { return false; } if (iIpSegment > 255) { return false; } } return true; } }