datavaluechecker.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 573 行 · 第 1/2 页
JAVA
573 行
catch (Exception e) { valid = false; popupError(owner, errorMsg+"format: MM-DD"); } return valid; } //checking for xsd:gYear else if (uri.toString().equals(xsd+"gYear")) { // format: yyyy // ex: 1999 SimpleDateFormat df = new SimpleDateFormat(); df.applyPattern("yyyy"); df.setLenient(false); try { df.parse(value); valid = true; } catch (Exception e) { valid = false; popupError(owner, errorMsg+"format: YYYY"); } return valid; } //checking for xsd:gYearMonth else if (uri.toString().equals(xsd+"gYearMonth")) { // format: CCYY-MM // ex: 1999-05 SimpleDateFormat df = new SimpleDateFormat(); df.applyPattern("yyyy'-'MM"); df.setLenient(false); try { df.parse(value); valid = true; } catch (Exception e) { valid = false; popupError(owner, errorMsg+"format: YYYY-MM"); } return valid; } // checking for xsd:hexBinary else if (uri.toString().equals(xsd+"hexBinary")) { System.out.println("checking binary...? "+valid); valid = isValidHexBinary(value); System.out.println("hexBin: vaild? "+valid); if (!valid) popupError(owner, errorMsg+"binary in HEX (0-9,A,B,C,D,E,F)"); return valid; } // checking for xsd:int else if (uri.toString().equals(xsd+"int") || uri.toString().equals(xsd+"integer")) { try { BigInteger Int = new BigInteger( value ); valid = true; } catch (Exception e) { valid = false; popupError(owner, errorMsg+"an integer of decimal digits"); } } // checking for xsd:language else if (uri.toString().equals(xsd+"int")) { valid = isValidLanguage( value ); if (!valid) popupError(owner, errorMsg+"language tag. See http://www.ietf.org/rfc/rfc3066.txt for details"); } //checking for xsd:long else if (uri.toString().equals(xsd+"long")) { try { long checkInt = Long.parseLong(value); valid = true; } catch (Exception e) { valid = false; popupError(owner, errorMsg+"long number"); } } //checking for xsd:Name else if (uri.toString().equals(xsd+"Name")) { valid = true; // todo: allowing all strings, but need more restrictions. consult: http://www.w3.org/TR/2000/WD-xml-2e-20000814#NT-Name } //checking for xsd:normalizedString else if (uri.toString().equals(xsd+"normlizedString")) { // cannot contain line feed (10), carriage return (13), tab (9) if (( value.indexOf( 13 ) != -1 ) || ( value.indexOf( 10 ) != -1 ) || ( value.indexOf( 9 ) != -1 ) ) valid = false; else valid = true; if (!valid) popupError(owner, errorMsg+"valid normalizedString: see http://www.w3.org/TR/xmlschema-2/#normalizedString"); } //checking for xsd:Name else if (uri.toString().equals(xsd+"NCName")) { valid = true; // todo: allowing all strings, but need more restrictions. consult: http://www.w3.org/TR/1999/REC-xml-names-19990114/#NT-NCName } //checking for xsd:NMTOKEN else if (uri.toString().equals(xsd+"NMTOKEN")) { valid = true; // todo: allowing all strings, but need more restrictions. consult: http://www.w3.org/TR/2000/WD-xml-2e-20000814#NT-Nmtoken } //checking for xsd:token else if (uri.toString().equals(xsd+"token")) { // cannot contain line feed (10), carriage return (13), tab (9) if (( value.indexOf( 13 ) != -1 ) || ( value.indexOf( 10 ) != -1 ) || ( value.indexOf( 9 ) != -1 ) ) valid = false; // cannot contain internal white space of length >1 else if ( value.indexOf(" ") != -1) valid = false; // cannot contain leading/trailing white spaces else if ( !value.trim().equals(value )) valid = false; else valid = true; if (!valid) popupError(owner, errorMsg+"valid token: see http://www.w3.org/TR/xmlschema-2/#token"); } //checking for xsd:negativeInteger else if (uri.toString().equals(xsd+"negativeInteger")) { try { int checkInt = Integer.parseInt(value); if (checkInt < 0) valid = true; else valid = false; } catch (Exception e) { valid = false; } if (!valid) popupError(owner, errorMsg+"negative integer"); } //checking for xsd:nonNegativeInteger else if (uri.toString().equals(xsd+"nonNegativeInteger")) { try { int checkInt = Integer.parseInt(value); if (checkInt>=0) valid = true; else valid = false; } catch (Exception e) { valid = false; } if (!valid) popupError(owner, errorMsg+"non-negative integer"); } //checking for xsd:nonPositiveInteger else if (uri.toString().equals(xsd+"nonPositiveInteger")) { try { int checkInt = Integer.parseInt(value); if (checkInt<=0) valid = true; else valid = false; } catch (Exception e) { valid = false; } if (!valid) popupError(owner, errorMsg+"non-positive integer"); } //checking for xsd:positiveInteger else if (uri.toString().equals(xsd+"positiveInteger")) { try { int checkInt = Integer.parseInt(value); if (checkInt>0) valid = true; else valid = false; } catch (Exception e) { valid = false; } if (!valid) popupError(owner, errorMsg+"positive integer"); } //checking for xsd:string (always valid) else if (uri.toString().equals(xsd+"string")) { valid = true; } //checking for xsd:time else if (uri.toString().equals(xsd+"time")) { // format: hh:mm:ss.sss SimpleDateFormat df = new SimpleDateFormat(); df.applyPattern("hh:mm:ss.SSS"); df.setLenient(false); try { df.parse(value); valid = true; } catch (Exception e) { valid = false; popupError(owner, errorMsg+"format: hh:mm:ss.sss"); } return valid; } // checking xsd:ungisnedInt else if (uri.toString().equals(xsd+"unsignedInt")) { try { BigInteger checkie = new BigInteger( value ); if (checkie.compareTo( new BigInteger("4294967295")) == 1) // bigger than max value allowed valid = false; else if (checkie.compareTo( BigInteger.ZERO) == -1) // smaller than min value allowed valid = false; else valid = true; } catch (Exception e) { // possibly NumberFormatException valid = false; } if (!valid) popupError(owner, errorMsg+"unsigned integer: [0, 4294967295]"); } // checking xsd:ungisnedLong else if (uri.toString().equals(xsd+"ungisnedLong")) { try { BigInteger checkie = new BigInteger( value ); if (checkie.compareTo( new BigInteger("18446744073709551615")) == 1) // bigger than max value allowed valid = false; else if (checkie.compareTo( BigInteger.ZERO) == -1) // smaller than min value allowed valid = false; else valid = true; } catch (Exception e) { // possibly NumberFormatException valid = false; } if (!valid) popupError(owner, errorMsg+"unsigned integer: [0, 18446744073709551615]"); } // checking xsd:ungisnedShort else if (uri.toString().equals(xsd+"ungisnedShort")) { try { Integer checkie = new Integer( value ); if ((checkie.intValue() > 65535 ) || ( checkie.intValue() < 0 )) valid = false; else valid = true; } catch (Exception e) { // possibly NumberFormatException valid = false; } if (!valid) popupError(owner, errorMsg+"unsigned short: [0, 65535]"); } // checking xsd:ungisnedShort else if (uri.toString().equals(xsd+"ungisnedByte")) { try { Integer checkie = new Integer( value ); if ((checkie.intValue() > 255 ) || ( checkie.intValue() < 0 )) valid = false; else valid = true; } catch (Exception e) { // possibly NumberFormatException valid = false; } if (!valid) popupError(owner, errorMsg+"unsigned byte: [0, 255]"); } //checking for RDFLiteral (always true) else if (uri.toString().equals(RDFVocabularyAdapter.RDF+"XMLLiteral")) { valid = true; } else if (!valid) popupError(owner, "We do not recognize this xsd:datatype"); return valid; } public static boolean isGood(String dummy) { return isValidLanguage(dummy); } public static void main(String [] args) throws Exception { System.out.println( isGood("EN") ); System.out.println( isGood("EN-US")); System.out.println( isGood("BIG-5")); System.out.println( isGood("EN1")); System.out.println( isGood("ENLISHIDK-US")); System.out.println( isGood("EN-us")); System.out.println( isGood("jp-75m,d")); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?