📄 verifier.java
字号:
/* Verifier.java{{IS_NOTE Purpose: Description: History: 2001/10/21 17:03:23, Create, Tom M. Yeh.}}IS_NOTECopyright (C) 2001 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.idom;import org.xml.sax.Locator;/** * The verifier to verify W3C/DOM related constraints. * * @author tomyeh */public class Verifier { private Verifier() { } /** * Checks whether an element's name is valid. */ public static final void checkElementName(String name, Locator loc) { if (name.indexOf(":") >= 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Element or attribute names cannot contain colons", loc); checkXMLName(name, loc); } /** * Checks whether an attribute's name is valid. */ public static final void checkAttributeName(String name, Locator loc) { // Allow xml:space and xml:lang as special cases if (!name.equals("xml:space") && !name.equals("xml:lang")) checkElementName(name, loc); } protected static final StringBuffer appendAsHex(StringBuffer sb, char c) { return sb.append('\'').append(c) .append("' (0x").append(Integer.toHexString(c)).append(')'); } /** * Checks whether a text is valid. */ public static final void checkCharacterData(String text, Locator loc) { if (text == null) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "A null is not a legal XML value", loc); for (int j = 0, len = text.length(); j<len; j++) if (!isXMLCharacter(text.charAt(j))) { StringBuffer sb = new StringBuffer(40); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, appendAsHex(sb, text.charAt(j)) .append(" is not a valid XML character").toString(), loc); } } /** * Checks whether a CDATA is valid. */ public static final void checkCData(String data, Locator loc) { if (data.indexOf("]]>") >= 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "']]>' is not allowed inside a CDATA string", loc); checkCharacterData(data, loc); } /** * Checks whether the prefix of a namespace is valid. */ public static final void checkNamespacePrefix(String prefix, Locator loc) { if (prefix == null || prefix.length() == 0) return; //OK: null or empty String reason = null; char first = prefix.charAt(0); if (isXMLDigit(first)) { reason = "a number"; } else if (first == '$') { reason = "a dollar sign ($)"; } else if (first == '-') { reason = "a hyphen (-)"; } else if (first == '.') { reason = "a period (.)"; } else { final String s = prefix.toLowerCase(); if (s.startsWith("xml") && !s.equals("xmlns")) reason = "\"xml\" in any combination of case"; } if (reason != null) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Namespace prefixes, "+prefix+", cannot begin with " + reason, loc); for (int j=0, len = prefix.length(); j<len; j++) if (!isXMLNameCharacter(prefix.charAt(j))) { StringBuffer sb = new StringBuffer("Namespace prefixes cannot contain "); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, appendAsHex(sb, prefix.charAt(j)).toString(), loc); } if (prefix.indexOf(":") >= 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Namespace prefixes cannot contain colons", loc); } /** * Checks whether the URI of a namespace is valid. */ public static final void checkNamespaceURI(String uri, Locator loc) { if (uri == null || uri.length() == 0) return; //OK: null or empty String reason = null; char first = uri.charAt(0); if (Character.isDigit(first)) reason = "a number"; else if (first == '$') reason = "a dollar sign ($)"; else if (first == '-') reason = "a hyphen (-)"; if (reason != null) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Namespace URIs cannot begin with " + reason, loc); } /** * Checks whether a processing instruction target is valid. */ public static final void checkPITarget(String target, Locator loc) { if (target.indexOf(":") >= 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Processing instruction targets cannot contain colons", loc); if (target.equalsIgnoreCase("xml")) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Processing instruction targets cannot be " + "\"xml\" in any combination of case", loc); checkXMLName(target, loc); } /** * Checks whether a comment data is valid. */ public static final void checkCommentData(String data, Locator loc) { if (data.indexOf("--") >= 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Comments cannot contain double hyphens (--)", loc); checkCharacterData(data, loc); } /** * Checks whether a name is valid. */ public static void checkXMLName(String name, Locator loc) { if (name == null || name.length() == 0) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "XML names cannot be null or empty", loc); if (!isXMLNameStartCharacter(name.charAt(0))) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "XML names cannot begin with \'" + name.charAt(0) + '\'', loc); for (int j=0, len = name.length(); j<len; j++) if (!isXMLNameCharacter(name.charAt(j))) throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "XML names cannot contain \'" + name.charAt(j) + '\'', loc); } /** * Checks whether a character is valid. */ public static boolean isXMLCharacter(char c) { if (c == '\n') return true; if (c == '\r') return true; if (c == '\t') return true; if (c < 0x20) return false; if (c <= 0xD7FF) return true; if (c < 0xE000) return false; if (c <= 0xFFFD) return true; if (c < 0x10000) return false; if (c <= 0x10FFFF) return true; return false; } /** * Checks whether a character can be part of a name. */ public static boolean isXMLNameCharacter(char c) { return isXMLLetter(c) || isXMLDigit(c) || c == '.' || c == '-' || c == '_' || c == ':' || isXMLCombiningChar(c) || isXMLExtender(c); } /** * Checks whether a character can be the first character of a name. */ public static boolean isXMLNameStartCharacter(char c) { return isXMLLetter(c) || c == '_' || c ==':'; } /** * Checks whether a character is a letter or digit. */ public static boolean isXMLLetterOrDigit(char c) { return (isXMLLetter(c) || isXMLDigit(c)); } /** * Checks whether a character is a letter. */ public static boolean isXMLLetter(char c) { if (c < 0x0041) return false; if (c <= 0x005a) return true; if (c < 0x0061) return false; if (c <= 0x007A) return true; if (c < 0x00C0) return false; if (c <= 0x00D6) return true; if (c < 0x00D8) return false; if (c <= 0x00F6) return true; if (c < 0x00F8) return false; if (c <= 0x00FF) return true; if (c < 0x0100) return false; if (c <= 0x0131) return true; if (c < 0x0134) return false; if (c <= 0x013E) return true; if (c < 0x0141) return false; if (c <= 0x0148) return true; if (c < 0x014A) return false; if (c <= 0x017E) return true; if (c < 0x0180) return false; if (c <= 0x01C3) return true; if (c < 0x01CD) return false; if (c <= 0x01F0) return true; if (c < 0x01F4) return false; if (c <= 0x01F5) return true; if (c < 0x01FA) return false; if (c <= 0x0217) return true; if (c < 0x0250) return false; if (c <= 0x02A8) return true; if (c < 0x02BB) return false; if (c <= 0x02C1) return true; if (c == 0x0386) return true; if (c < 0x0388) return false; if (c <= 0x038A) return true; if (c == 0x038C) return true; if (c < 0x038E) return false; if (c <= 0x03A1) return true; if (c < 0x03A3) return false; if (c <= 0x03CE) return true; if (c < 0x03D0) return false; if (c <= 0x03D6) return true; if (c == 0x03DA) return true; if (c == 0x03DC) return true; if (c == 0x03DE) return true; if (c == 0x03E0) return true; if (c < 0x03E2) return false; if (c <= 0x03F3) return true; if (c < 0x0401) return false; if (c <= 0x040C) return true; if (c < 0x040E) return false; if (c <= 0x044F) return true; if (c < 0x0451) return false; if (c <= 0x045C) return true; if (c < 0x045E) return false; if (c <= 0x0481) return true; if (c < 0x0490) return false; if (c <= 0x04C4) return true; if (c < 0x04C7) return false; if (c <= 0x04C8) return true; if (c < 0x04CB) return false; if (c <= 0x04CC) return true; if (c < 0x04D0) return false; if (c <= 0x04EB) return true; if (c < 0x04EE) return false; if (c <= 0x04F5) return true; if (c < 0x04F8) return false; if (c <= 0x04F9) return true; if (c < 0x0531) return false; if (c <= 0x0556) return true; if (c == 0x0559) return true; if (c < 0x0561) return false; if (c <= 0x0586) return true; if (c < 0x05D0) return false; if (c <= 0x05EA) return true; if (c < 0x05F0) return false; if (c <= 0x05F2) return true; if (c < 0x0621) return false; if (c <= 0x063A) return true; if (c < 0x0641) return false; if (c <= 0x064A) return true; if (c < 0x0671) return false; if (c <= 0x06B7) return true; if (c < 0x06BA) return false; if (c <= 0x06BE) return true; if (c < 0x06C0) return false; if (c <= 0x06CE) return true; if (c < 0x06D0) return false; if (c <= 0x06D3) return true; if (c == 0x06D5) return true; if (c < 0x06E5) return false; if (c <= 0x06E6) return true; if (c < 0x0905) return false; if (c <= 0x0939) return true; if (c == 0x093D) return true; if (c < 0x0958) return false; if (c <= 0x0961) return true; if (c < 0x0985) return false; if (c <= 0x098C) return true; if (c < 0x098F) return false; if (c <= 0x0990) return true; if (c < 0x0993) return false; if (c <= 0x09A8) return true; if (c < 0x09AA) return false; if (c <= 0x09B0) return true; if (c == 0x09B2) return true; if (c < 0x09B6) return false; if (c <= 0x09B9) return true; if (c < 0x09DC) return false; if (c <= 0x09DD) return true; if (c < 0x09DF) return false; if (c <= 0x09E1) return true; if (c < 0x09F0) return false; if (c <= 0x09F1) return true; if (c < 0x0A05) return false; if (c <= 0x0A0A) return true; if (c < 0x0A0F) return false; if (c <= 0x0A10) return true; if (c < 0x0A13) return false; if (c <= 0x0A28) return true; if (c < 0x0A2A) return false; if (c <= 0x0A30) return true; if (c < 0x0A32) return false; if (c <= 0x0A33) return true; if (c < 0x0A35) return false; if (c <= 0x0A36) return true; if (c < 0x0A38) return false; if (c <= 0x0A39) return true; if (c < 0x0A59) return false; if (c <= 0x0A5C) return true; if (c == 0x0A5E) return true; if (c < 0x0A72) return false; if (c <= 0x0A74) return true; if (c < 0x0A85) return false; if (c <= 0x0A8B) return true; if (c == 0x0A8D) return true; if (c < 0x0A8F) return false; if (c <= 0x0A91) return true; if (c < 0x0A93) return false; if (c <= 0x0AA8) return true; if (c < 0x0AAA) return false; if (c <= 0x0AB0) return true; if (c < 0x0AB2) return false; if (c <= 0x0AB3) return true; if (c < 0x0AB5) return false; if (c <= 0x0AB9) return true; if (c == 0x0ABD) return true; if (c == 0x0AE0) return true; if (c < 0x0B05) return false; if (c <= 0x0B0C) return true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -