⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:
package org.kxml;import java.io.*;import java.util.*;/** A class containing several static xml methods, mainly for escaping    special characters like angle brakets and quotes. This class    contains also some (partially shared) constants for the parser and    kDOM. */public class Xml {    public static final String NO_NAMESPACE = "";       /** Integer constant for comments */    public static final int COMMENT = 1;    /** Integer constant for doctype */    public static final int DOCTYPE = 2;    /** Integer constant for elements */    public static final int ELEMENT = 4;    /** Integer constant returned by ParseEvent.getType if the end of	the document has been reached */    public static final int END_DOCUMENT = 8;        /** Integer constant assigned to an EndTag parse event */    public static final int END_TAG = 16;    /** Integer constant assigned to a processing instruction */     public static final int PROCESSING_INSTRUCTION = 32;        /** Integer constant assigned to StartTag parse event */    public static final int START_TAG = 64;    /** Integer constant assigned to text nodes and events */    public static final int TEXT = 128;    /** Integer constant for whitespace nodes and events */    public static final int WHITESPACE = 256;    /** minimum escaping, quotes are not escaped */    public static final int ENCODE_MIN = 0;    /** forces escaping of quotes */    public static final int ENCODE_QUOT = 1;    /** forces escaping of all character coded greater than 127 */        public static int ENCODE_128 = 2;    /** Constant identifying wap extension events */    public static final int WAP_EXTENSION = 1024;    public static String normalize (String in) {	StringBuffer result = new StringBuffer ();	boolean first = true;	int len = in.length ();	for (int i = 0; i < len; i++) {	    char c = in.charAt (i);	    if (c <= ' ') {		if (first) {		    result.append (' ');		    first = false;		}	    }	    else {		result.append (c);		first = true;	    }	}	return result.toString ();    }    /** unescapes an xml encoded string */        public static String decode (String cooked) {		StringBuffer result = new StringBuffer ();	int i0 = 0;	while (true) {	    	    int i1 = cooked.indexOf ('&', i0);	    if (i1 == -1) break;	    	    result.append (cooked.substring (i0, i1));	    int i2 = cooked.indexOf (';', i1);	    if (i2 == -1) {		result.append ("&amp;");		i0 = i1+1;		break;	    }	    	    String decode = cooked.substring (i1 + 1, i2);	    if (decode.startsWith ("#x"))		result.append 		    ((char) Integer.parseInt (decode.substring (2), 16));			    else if (decode.startsWith ("#")) 		result.append 		    ((char) Integer.parseInt (decode.substring (1)));	    	    else if (decode.equals ("lt"))		result.append ('<');	    else if (decode.equals ("gt"))		result.append ('>');	    else if (decode.equals ("quot"))		result.append ('"');	    else if (decode.equals ("amp"))		result.append ('&');	    else if (decode.equals ("apos"))		result.append ('\'');	    else throw new RuntimeException 		("illegal character entity: &"+decode+";");	    i0 = i2+1;	}	result.append (cooked.substring (i0));	return result.toString ();    }    /** convenience method for encode (String raw, ENCODE_MIN) */        public static String encode (String raw) {	return encode (raw, ENCODE_MIN);    }    /* encodes an attribute with the given name and value.  A single	space is inserted before the name of the attribute     public static String encodeAttr (String name, String value) {	return " "+name+"=\"" + encode (value, ENCODE_QUOT) + "\"";    }*/    /** encodes a string escaping less than etc. */    public static String encode (String raw, int flags) {      	int len = raw.length ();    	StringBuffer cooked = new StringBuffer (raw.length ());	for (int i = 0; i < len; i++) {	    char c = raw.charAt (i);	  	    switch (c) {	    case '<': cooked.append ("&lt;"); break;	    case '>': cooked.append ("&gt;"); break;	    case '&': cooked.append ("&amp;"); break;	    case '"': 		{ 		    if ((flags & ENCODE_QUOT) != 0) 			cooked.append ("&quot;"); 		    else			cooked.append ('"');		}		break;	    default:		if (c >= 128 && ((flags & ENCODE_128) != 0)) 		    cooked.append ("&#"+((int) c)+";");		else		    cooked.append (c);	    }	}	return cooked.toString ();    }    /** quotes a string by adding quote chars at the beginning	and the end and escaping all quotes and XML special	chars contained in the string */    public static String quote (String q) {	return "\"" + encode (q, ENCODE_QUOT) + "\"";    }    /** like String.trim, but additionally all groups of one or more	whitespace characters in the string are replaced by a single	space (0x040) */    public static String trim (String s) {		char [] buf = new char [s.length ()]; 	boolean ignoring = false;	int targetPos = 0;	for (int i = 0; i < s.length (); i++) {	    char c = s.charAt (i);	    	    if (c <= ' ') 		ignoring = true;	    else {		if (ignoring) {		    if (targetPos != 0) buf [targetPos++] = ' '; 		    ignoring = false;		}		buf [targetPos++] = c;	    }	}	return new String (buf, 0, targetPos);    }}

⌨️ 快捷键说明

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