📄 valuevalidator.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
*
* Copyright c 2002-2004 E-Business Technology Institute
* The University of Hong Kong,
* Pukfulam Road,
* Hong Kong.
* All Rights Reserved.
*
* InputChecking - provides methods for various types of input checking.
*
* $Author$
* $Date$
* $Revision$
*
*/
package eti.bi.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Vector;
/*
*
* A class that provides methods for various types of data type checking.
*
*/
public class ValueValidator
{
public static boolean debug;
private static HashSet<String> m_SymbolList = null;
static
{
m_SymbolList = new HashSet<String>();
m_SymbolList.add("~");
m_SymbolList.add("`");
m_SymbolList.add("!");
m_SymbolList.add("@");
m_SymbolList.add("#");
m_SymbolList.add("$");
m_SymbolList.add("%");
m_SymbolList.add("^");
m_SymbolList.add("&");
m_SymbolList.add("*");
m_SymbolList.add("(");
m_SymbolList.add(")");
m_SymbolList.add("-");
m_SymbolList.add("_");
m_SymbolList.add("+");
m_SymbolList.add("=");
m_SymbolList.add("{");
m_SymbolList.add("[");
m_SymbolList.add("}");
m_SymbolList.add("]");
m_SymbolList.add("|");
m_SymbolList.add("\\");
m_SymbolList.add(":");
m_SymbolList.add(";");
m_SymbolList.add("\"");
m_SymbolList.add("'");
m_SymbolList.add("<");
m_SymbolList.add(",");
m_SymbolList.add(">");
m_SymbolList.add(".");
m_SymbolList.add("?");
m_SymbolList.add("/");
}
/** Check whether the argument value contains space or not **/
public static boolean containsSpace(String a_Value)
{
if (a_Value.indexOf(" ") != -1 || a_Value.indexOf("\t") != -1)
return true;
else return false;
}
/** Check whether the argument value contains alphabet only or not **/
public static boolean isAlphabet(String a_Value)
{
char c;
int valueLength = a_Value.length();
for (int i = 0; i < valueLength; i++)
{
c = a_Value.charAt(i);
if (!((c >= 65 && c <= 90) || (c >= 97 && c <= 122)))
return false;
}
return true;
}
/** Check whether the argument value represents a numeric number **/
public static boolean isDouble(String a_Value)
{
try
{
Double.parseDouble(a_Value);
}
catch (NumberFormatException nfe)
{
if (debug)
System.out.println(nfe.toString());
return false;
}
return true;
}
/** Check whether the argument value represents a numeric number **/
public static boolean isFloat(String a_Value)
{
try
{
Float.parseFloat(a_Value);
}
catch (NumberFormatException nfe)
{
if (debug)
System.out.println(nfe.toString());
return false;
}
return true;
}
/** Check whether the argument value represents a numeric number **/
public static boolean isNumeric(String a_Value)
{
try
{
Long.parseLong(a_Value);
}
catch (NumberFormatException nfe)
{
if (debug)
System.out.println(nfe.toString());
return false;
}
return true;
}
public static boolean isNumber(String a_Value)
{
a_Value = a_Value.trim();
for (int i=0; i<a_Value.length(); i++){
if (a_Value.charAt(i) < '0' || a_Value.charAt(i) > '9')
return false;
}
return true;
}
/** Check whether the argument value represents a integer number **/
public static boolean isInteger(String a_Value)
{
try
{
Integer.parseInt(a_Value);
}
catch (NumberFormatException nfe)
{
if (debug)
System.out.println(nfe.toString());
return false;
}
return true;
}
/**
* Checks whethe the argument a_Value represents a symbol or not.
*
* @param a_Value the sting to be checked.
**/
public static boolean isSymbol(String a_Value)
{
if (a_Value.length() != 1)
return false;
char symbolCharacter = a_Value.charAt(0);
return !Character.isLetter(symbolCharacter);
}
/** Check whether the host name is valid or not (May not check for host
existence) **/
public static boolean isValidHost(String a_Host)
{
@SuppressWarnings("unused") InetAddress add;
/** If the network does not exists or the host represents an IP address
, return true **/
if (ValueValidator.isNetworkExist() == false && ValueValidator.isIPAddress(a_Host) == false)
return true;
try
{
add = InetAddress.getByName(a_Host);
}
catch (UnknownHostException ukhe)
{
if (debug)
System.out.println(ukhe.toString());
return false;
}
catch (SecurityException se)
{
if (debug)
System.out.println(se.toString());
return false;
}
return true;
}
/** Check whether the network exists or not **/
public static boolean isNetworkExist()
{
boolean exists = true;
try
{
@SuppressWarnings("unused") InetAddress yahoo = InetAddress.getByName("www.yahoo.com");
}
catch (UnknownHostException ue)
{
exists = false;
}
try
{
@SuppressWarnings("unused") InetAddress google = InetAddress.getByName("www.google.com");
}
catch (UnknownHostException ue)
{
exists = false;
}
return exists;
}
/** Check whehther the input IP address is a valid IP address o not **/
public static boolean isIPAddress(String a_IpAddress)
{
int start_index = 0;
int end_index;
for (int i = 0; i < 4; i++)
{
end_index = a_IpAddress.indexOf(".", start_index);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -