一、前言
关于dyuproject-util-1.1.6.jar源码包中的com.dyuproject.util.validate.IPDomainValidator实现ip及域名校验器,对域名判断校验isValid、不同域名常见方式重写校验等操作。
二、源码说明
package com.dyuproject.util.validate;@b@@b@public final class IPDomainValidator@b@{@b@ public static final int INVALID = 0;@b@ public static final int PLAIN = 1;@b@ public static final int ALPHANUMERIC = 2;@b@ public static final int HYPHENATED = 3;@b@ public static final int MIXED = 4;@b@ public static final int IP = 5;@b@@b@ public static int tokenValidate(char[] part, int start, int len)@b@ {@b@ int digitCount = 0; int lastHyphen = -1;@b@ for (int i = 0; i < len; ++i)@b@ {@b@ char c = part[(start++)];@b@ if (Character.isDigit(c)) {@b@ ++digitCount;@b@ } else if (!(Character.isLetter(c)))@b@ {@b@ if (c == '-')@b@ {@b@ if (i < len - 1)@b@ {@b@ if ((lastHyphen + 1 == i) || (lastHyphen - 1 == i))@b@ {@b@ return 0;@b@ }@b@ lastHyphen = i; break label90:@b@ }@b@@b@ return 0;@b@ }@b@@b@ return 0;@b@ }@b@ }@b@ if (digitCount == len)@b@ label90: return 5;@b@@b@ if (digitCount == 0)@b@ return ((lastHyphen == -1) ? 1 : 3);@b@@b@ return ((lastHyphen == -1) ? 2 : 4);@b@ }@b@@b@ public static int indexOf(char[] ch, char c, int start)@b@ {@b@ for (int i = start; i < ch.length; ++i)@b@ {@b@ if (ch[i] == c)@b@ return i;@b@ }@b@ return -1;@b@ }@b@@b@ public static int lastIndexOf(char[] ch, char c, int start)@b@ {@b@ int i = start;@b@ do if (i-- <= 0)@b@ break label18;@b@ while (ch[i] != c);@b@ return i;@b@@b@ label18: return -1;@b@ }@b@@b@ public static int validate(String domain, int start, int end)@b@ {@b@ char[] ch = new char[end - start];@b@ domain.getChars(start, end, ch, 0);@b@ return validate(ch);@b@ }@b@@b@ public static int validate(String domain)@b@ {@b@ return validate(domain.toCharArray());@b@ }@b@@b@ public static int validate(char[] domain)@b@ {@b@ return validate(domain, 0, domain.length);@b@ }@b@@b@ public static int validate(char[] domain, int start, int end)@b@ {@b@ boolean mixed = false; boolean hyphenated = false; boolean alphanumeric = false;@b@ int tokens = 0; int digitTokens = 0; int extLen = 0;@b@ for (int i = end; i > 0; )@b@ {@b@ int idx = lastIndexOf(domain, '.', i - 1);@b@ int l = i - idx - 1;@b@ int check = tokenValidate(domain, idx + 1, l);@b@ i = idx;@b@ switch (check)@b@ {@b@ case 0:@b@ return 0;@b@ case 1:@b@ if (tokens == 0)@b@ {@b@ if (l == 1)@b@ return 0;@b@@b@ extLen = l; } break;@b@ case 2:@b@ if (tokens == 0)@b@ {@b@ return 0;@b@ }@b@ alphanumeric = true;@b@ break;@b@ case 3:@b@ if (tokens == 0)@b@ {@b@ return 0;@b@ }@b@ hyphenated = true;@b@ break;@b@ case 4:@b@ if (tokens == 0)@b@ {@b@ return 0;@b@ }@b@ mixed = true;@b@ break;@b@ case 5:@b@ if ((tokens == 0) && (l > 3))@b@ {@b@ return 0;@b@ }@b@ alphanumeric = true;@b@ ++digitTokens;@b@ }@b@@b@ if (l > 63)@b@ return 0;@b@ ++tokens;@b@ }@b@ if (tokens == digitTokens)@b@ return ((tokens > 4) ? 0 : 5);@b@@b@ if ((extLen == 0) || (tokens == 1))@b@ return 0;@b@@b@ if (mixed)@b@ return 4;@b@@b@ if (hyphenated)@b@ return 3;@b@@b@ return ((alphanumeric) ? 2 : 1);@b@ }@b@@b@ public static boolean isValid(String domain)@b@ {@b@ return (validate(domain) != 0);@b@ }@b@@b@ public static boolean isValid(char[] domain)@b@ {@b@ return (validate(domain) != 0);@b@ }@b@@b@ public static boolean isValid(String domain, int start, int end)@b@ {@b@ return (validate(domain, start, end) != 0);@b@ }@b@@b@ public static boolean isValid(char[] domain, int start, int end)@b@ {@b@ return (validate(domain, start, end) != 0);@b@ }@b@}