xmlutils.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 436 行 · 第 1/2 页
JAVA
436 行
}
for (int i = 0; i < digitRange.length; i += 2) {
for (int j = digitRange[i]; j <= digitRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME;
}
}
for (int i = 0; i < combiningCharRange.length; i += 2) {
for (int j = combiningCharRange[i]; j <= combiningCharRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME;
}
}
for (int i = 0; i < combiningCharChar.length; i++) {
CHARS[combiningCharChar[i]] |= MASK_NAME;
}
for (int i = 0; i < extenderRange.length; i += 2) {
for (int j = extenderRange[i]; j <= extenderRange[i + 1]; j++) {
CHARS[j] |= MASK_NAME;
}
}
for (int i = 0; i < extenderChar.length; i++) {
CHARS[extenderChar[i]] |= MASK_NAME;
}
}
// Constructor
//-------------------------------------------------------------------------
/**
* <p>Constructor for use by tools that required <code>JavaBean</code> instances.</p>
*
* <p>This constructor is public <strong>only</strong>
* to permit tools that require a JavaBean instance to operate.
* <code>XMLUtils</code> instances should <strong>not</strong> be constructed in standard
* programming. Instead, the class methods should be called directly.</p>
*/
public XMLUtils() {}
// Class methods
//-------------------------------------------------------------------------
/**
* <p>Escape the <code>toString</code> of the given object.
* For use as body text.</p>
*
* @param value escape <code>value.toString()</code>
* @return text with escaped delimiters
*/
public static final String escapeBodyValue(Object value) {
StringBuffer buffer = new StringBuffer(value.toString());
for (int i=0, size = buffer.length(); i <size; i++) {
switch (buffer.charAt(i)) {
case '<':
buffer.replace(i, i+1, LESS_THAN_ENTITY);
size += 3;
i+=3;
break;
case '>':
buffer.replace(i, i+1, GREATER_THAN_ENTITY);
size += 3;
i += 3;
break;
case '&':
buffer.replace(i, i+1, AMPERSAND_ENTITY);
size += 4;
i += 4;
break;
}
}
return buffer.toString();
}
/**
* <p>Escape the <code>toString</code> of the given object.
* For use in an attribute value.</p>
*
* @param value escape <code>value.toString()</code>
* @return text with characters restricted (for use in attributes) escaped
*/
public static final String escapeAttributeValue(Object value) {
StringBuffer buffer = new StringBuffer(value.toString());
for (int i=0, size = buffer.length(); i <size; i++) {
switch (buffer.charAt(i)) {
case '<':
buffer.replace(i, i+1, LESS_THAN_ENTITY);
size += 3;
i+=3;
break;
case '>':
buffer.replace(i, i+1, GREATER_THAN_ENTITY);
size += 3;
i += 3;
break;
case '&':
buffer.replace(i, i+1, AMPERSAND_ENTITY);
size += 4;
i += 4;
break;
case '\'':
buffer.replace(i, i+1, APOSTROPHE_ENTITY);
size += 5;
i += 5;
break;
case '\"':
buffer.replace(i, i+1, QUOTE_ENTITY);
size += 5;
i += 5;
break;
}
}
return buffer.toString();
}
/**
* Escapes the given content suitable for insertion within a
* <code>CDATA</code> sequence.
* Within a <code>CDATA</code> section, only the <code>CDEnd</code>
* string ']]>' is recognized as markup.
* @param content the body content whose character data should
* be escaped in a way appropriate for use within a <code>CDATA</code>
* section of xml.
* @return escaped character data, not null
*/
public static final String escapeCDATAContent(String content) {
StringBuffer buffer = new StringBuffer(content);
escapeCDATAContent(buffer);
return buffer.toString();
}
/**
* Escapes the given content suitable for insertion within a
* <code>CDATA</code> sequence.
* Within a <code>CDATA</code> section, only the <code>CDEnd</code>
* string ']]>' is recognized as markup.
* @param bufferedContent the body content within a buffer
* whose character data should
* be escaped in a way appropriate for use within a <code>CDATA</code>
* section of xml.
* @return escaped character data, not null
*/
public static final void escapeCDATAContent(StringBuffer bufferedContent) {
for (int i=2, size = bufferedContent.length(); i<size; i++) {
char at = bufferedContent.charAt(i);
if ( at == '>'
&& bufferedContent.charAt(i-1) == ']'
&& bufferedContent.charAt(i-2) == ']') {
bufferedContent.replace(i, i+1, GREATER_THAN_ENTITY);
size += 3;
i+=3;
}
}
}
/**
* <p>Is this string a well formed xml name?</p>
*
* <p>Only certain characters are allowed in well formed element and attribute
* names in xml. For example, white space is not allowed in a name.</p>
*
* <p>The code for this method is based on code in
* <code>org.apache.xerces.util.XMLChar</code>
* in <a href='http://xml.apache.org/xerces2-j/index.html'>Apache Xerces</a>.
* The authors of this class are credited at the top of this class.</p>
*
* @param name the <code>String</code> to be checked for use as an xml attribute
* or element name. Returns false if <code>name</code> is null
* @return true if this string would be a well-formed name
*/
public static boolean isWellFormedXMLName( String name ) {
if ( name == null ) {
return false;
}
if ( name.length() == 0 ) {
return false;
}
char ch = name.charAt(0);
if( isNameStartChar(ch) == false) {
return false;
}
for (int i = 1; i < name.length(); i++ ) {
ch = name.charAt(i);
if( isNameChar( ch ) == false ) {
return false;
}
}
return true;
}
/**
* Returns true if the specified character is a valid name
* character as defined by the XML 1.0 specification.
*
* @param c The character to check.
* @return true if this is an XML name character
*/
public static boolean isNameChar(int c) {
return c < 0x10000 && (CHARS[c] & MASK_NAME) != 0;
}
/**
* Returns true if the specified character is a valid name start
* character as defined in the XML 1.0 specification.
*
* @param c The character to check.
* @return trus if this is an XML name start character
*/
public static boolean isNameStartChar(int c) {
return c < 0x10000 && (CHARS[c] & MASK_NAME_START) != 0;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?