datavaluechecker.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 573 行 · 第 1/2 页

JAVA
573
字号
package org.mindswap.swoop.utils;import java.awt.Frame;import java.math.BigInteger;import java.net.URI;import java.net.URISyntaxException;import java.text.SimpleDateFormat;import java.util.Hashtable;import java.util.StringTokenizer;import javax.swing.JOptionPane;import org.semanticweb.owl.impl.model.OWLConcreteDataTypeImpl;import org.semanticweb.owl.io.vocabulary.RDFVocabularyAdapter;import org.semanticweb.owl.io.vocabulary.XMLSchemaSimpleDatatypeVocabulary;/** * @author Dave * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments *//* *  *  * @author Dave * * This class is used to check for type checkig.  That is, making sure users are entering * correct values for a particular data type.  This class contains static methods * for easy invocation. *  *  */public class DataValueChecker {		/**	 * Pops an error message box in the owner Frame	 */	private static void popupError(Frame owner, String msg) 	{		JOptionPane.showMessageDialog(owner, msg, "Error", JOptionPane.ERROR_MESSAGE);	}		/**	 *  Test to see if given String consists only alphabets	 */	private static boolean areAllAlphabets( String str )	{		for (int i = 0; i < str.length(); i++)		{			char c = str.charAt( i );			if (!Character.isLetter(c))				return false;		}		return true;	}		/**	 *  Test to see if given String consists only alphanumerics	 */	private static boolean areAllAlphaNumeric( String str)	{		for (int i = 0; i < str.length(); i++)		{			char c = str.charAt( i );			if (!Character.isLetterOrDigit(c))				return false;		}		return true;	} 		/**	 *  Test to see if given String consists only hex digits	 */	private static boolean isValidHexBinary(String value)	{		int permittedLength = 32;		boolean flag = false;		String sub = "";		if (!(value.length() < permittedLength))		{			sub = value.substring(0, 32);			value = value.substring(32);		}		else			sub = value;		flag = isValidBaseXBinary(sub, 16);		if (!flag)			return false;		return true;	}		private static boolean isValidBaseXBinary(String value, int base)	{		try			{				long k = Long.parseLong(value, base);			}			catch (NumberFormatException e)			{				return false;			}		return true;	}		/**	 *  Test to see if given String is a valid String tag defined in <http://www.ietf.org/rfc/rfc3066.txt>	 */	private static boolean isValidLanguage(String value)	{		StringTokenizer tokens = new StringTokenizer(value, "-/n");		if (tokens.hasMoreTokens())		{	  		String first = tokens.nextToken();			if ( first.length() > 8)				return false;			if (!areAllAlphabets(first) )				return false;			while ( tokens.hasMoreTokens() )			{				String token = tokens.nextToken();				if (!areAllAlphaNumeric( token ) )					return false;			}	  			  	}		else			return false; // empty or "-" value				return true;	}		/**	 * Check for the validity of the property-value pair based on the range of the 	 * property and the data value specified by the user	 * @param dt - XSD DataType (or rdfs:Literal)	 * @param value - string value specified by user	 * @return true (data value falls into the defined datatype range), false (otherwise)	 */	public static boolean isValidValue(Frame owner, OWLConcreteDataTypeImpl dt, String value) {		return isValidValue( owner, dt.getURI(),  value);	}		public static boolean isValidValue(Frame owner, URI uri, String value) {		boolean valid = false;		String xsd = XMLSchemaSimpleDatatypeVocabulary.XS;		String errorMsg = "Invalid Value for Specified Datatype - require ";				//checking for xsd:anyURI		if (uri.toString().equals(xsd+"anyURI")) {			try			{				URI dummyURI = new URI( value );				valid = true;			}			catch (URISyntaxException e)			{				valid = false;				popupError(owner, errorMsg+"a valid absolute or relative URI");			}		}		//checking for xsd:boolean		else if (uri.toString().equals(xsd+"boolean")) {			if (value.equals("true") || value.equals("false") ||					value.equals("1") || value.equals("0") ) valid = true;			else {				valid = false;				popupError(owner, errorMsg+"'true' or 'false'");			}		}		//checking for xsd:base64Binary		else if (uri.toString().equals(xsd+"base64Binary")) {			valid = true; // todo: need to fix this!		}		//checking for xsd:byte		else if (uri.toString().equals(xsd+"byte")) {			try			{				byte b = Byte.parseByte( value );				valid = true;			}			catch (NumberFormatException e)			{				popupError(owner, errorMsg+"byte value: [-127, 128]");			}		}		//checking for xsd:date		else if (uri.toString().equals(xsd+"date")) {			// format: CCYY-MM-DD			SimpleDateFormat df = new SimpleDateFormat();			df.applyPattern("yyyy-MM-dd");			df.setLenient(false);			try {				df.parse(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"format: YYYY-MM-DD");			}			return valid;					}		//checking for xsd:dateTime		else if (uri.toString().equals(xsd+"dateTime")) {			// format: ['-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?] 			// ex: 1999-05-31T13:20:00.000			// here we do a fixed (no-optional adoption)			SimpleDateFormat df = new SimpleDateFormat();			df.applyPattern("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS");			df.setLenient(false);			try {				df.parse(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"format: YYYY-MM-DD'T'hh:mm:ss.sss  ex: 1999-05-31T13:20:00.000");			}			return valid;				}		//checking for xsd:decimal or xsd:double		else if (uri.toString().equals(xsd+"decimal") || uri.toString().equals(xsd+"double")) {			try {				double checkDouble = Double.parseDouble(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"double precision value");			}		}		//checking for xsd:float		else if (uri.toString().equals(xsd+"float")) {			try {				float checkFloat = Float.parseFloat(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"floating point value");			}		}		//checking for xsd:gDay		else if (uri.toString().equals(xsd+"gDay")) {			// format: DD			// ex: 31			SimpleDateFormat df = new SimpleDateFormat();			df.applyPattern("dd");			df.setLenient(false);			try {				df.parse(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"format: DD");			}			return valid;			}		//checking for xsd:gMonth		else if (uri.toString().equals(xsd+"gMonth")) {			// format: MM			// ex: 05			SimpleDateFormat df = new SimpleDateFormat();			df.applyPattern("MM");			df.setLenient(false);			try {				df.parse(value);				valid = true;			}			catch (Exception e) {				valid = false;				popupError(owner, errorMsg+"format: MM    ex: 05");			}			return valid;			}		//checking for xsd:gMonthDay		else if (uri.toString().equals(xsd+"gMonthDay")) {			// format: MM-DD			// ex: 05-31			SimpleDateFormat df = new SimpleDateFormat();			df.applyPattern("MM'-'dd");			df.setLenient(false);			try {				df.parse(value);				valid = true;			}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?