|
|
@@ -2,18 +2,19 @@ package cn.hutool.json;
|
|
|
|
|
|
/**
|
|
|
* XML分析器,继承自JSONTokener,提供XML的语法分析
|
|
|
- *
|
|
|
+ *
|
|
|
* @author JSON.org
|
|
|
*/
|
|
|
public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/**
|
|
|
- * The table of entity values. It initially contains Character values for amp, apos, gt, lt, quot.
|
|
|
+ * The table of entity values.
|
|
|
+ * It initially contains Character values for amp, apos, gt, lt, quot.
|
|
|
*/
|
|
|
public static final java.util.HashMap<String, Character> entity;
|
|
|
|
|
|
static {
|
|
|
- entity = new java.util.HashMap<String, Character>(8);
|
|
|
+ entity = new java.util.HashMap<>(8);
|
|
|
entity.put("amp", XML.AMP);
|
|
|
entity.put("apos", XML.APOS);
|
|
|
entity.put("gt", XML.GT);
|
|
|
@@ -23,16 +24,17 @@ public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/**
|
|
|
* Construct an XMLTokener from a string.
|
|
|
- *
|
|
|
- * @param s A source string.
|
|
|
+ *
|
|
|
+ * @param s A source string.
|
|
|
+ * @param config JSON配置
|
|
|
*/
|
|
|
- public XMLTokener(String s) {
|
|
|
- super(s);
|
|
|
+ public XMLTokener(CharSequence s, JSONConfig config) {
|
|
|
+ super(s, config);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Get the text in the CDATA block.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return The string up to the <code>]]></code>.
|
|
|
* @throws JSONException If the <code>]]></code> is not found.
|
|
|
*/
|
|
|
@@ -40,7 +42,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
char c;
|
|
|
int i;
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
c = next();
|
|
|
if (end()) {
|
|
|
throw syntaxError("Unclosed CDATA");
|
|
|
@@ -55,7 +57,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Get the next XML outer token, trimming whitespace.
|
|
|
+ * Get the next XML outer token, trimming whitespace.
|
|
|
* There are two kinds of tokens: the '>' character which begins a markup tag, and the content text between markup tags.
|
|
|
*
|
|
|
* @return A string, or a '>' Character, or null if there is no more source text.
|
|
|
@@ -74,7 +76,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
return XML.LT;
|
|
|
}
|
|
|
sb = new StringBuilder();
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
if (c == '<' || c == 0) {
|
|
|
back();
|
|
|
return sb.toString().trim();
|
|
|
@@ -90,14 +92,14 @@ public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/**
|
|
|
* Return the next entity. These entities are translated to Characters: <code>& ' > < "</code>.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param ampersand An ampersand character.
|
|
|
* @return A Character or an entity String if the entity is not recognized.
|
|
|
* @throws JSONException If missing ';' in XML entity.
|
|
|
*/
|
|
|
public Object nextEntity(char ampersand) throws JSONException {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
char c = next();
|
|
|
if (Character.isLetterOrDigit(c) || c == '#') {
|
|
|
sb.append(Character.toLowerCase(c));
|
|
|
@@ -114,7 +116,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/**
|
|
|
* Returns the next XML meta token. This is used for skipping over <!...> and <?...?> structures.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return Syntax characters (<code>< > / = ! ?</code>) are returned as Character, and strings and names are returned as Boolean. We don't care what the values actually are.
|
|
|
* @throws JSONException 字符串中属性未关闭或XML结构错误抛出此异常。If a string is not properly closed or if the XML is badly structured.
|
|
|
*/
|
|
|
@@ -142,7 +144,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
case '"':
|
|
|
case '\'':
|
|
|
q = c;
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
c = next();
|
|
|
if (c == 0) {
|
|
|
throw syntaxError("Unterminated string");
|
|
|
@@ -152,7 +154,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
}
|
|
|
}
|
|
|
default:
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
c = next();
|
|
|
if (Character.isWhitespace(c)) {
|
|
|
return Boolean.TRUE;
|
|
|
@@ -177,7 +179,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
/**
|
|
|
* Get the next XML Token. These tokens are found inside of angle brackets. It may be one of these characters: <code>/ > = ! ?</code> or it may be a string wrapped in single quotes or double
|
|
|
* quotes, or it may be a name.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return a String or a Character.
|
|
|
* @throws JSONException If the XML is not well formed.
|
|
|
*/
|
|
|
@@ -210,7 +212,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
case '\'':
|
|
|
q = c;
|
|
|
sb = new StringBuilder();
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
c = next();
|
|
|
if (c == 0) {
|
|
|
throw syntaxError("Unterminated string");
|
|
|
@@ -229,7 +231,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
// Name
|
|
|
|
|
|
sb = new StringBuilder();
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
sb.append(c);
|
|
|
c = next();
|
|
|
if (Character.isWhitespace(c)) {
|
|
|
@@ -258,7 +260,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/**
|
|
|
* Skip characters until past the requested string. If it is not found, we are left at the end of the source with a result of false.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param to A string to skip past.
|
|
|
* @return 是否成功skip
|
|
|
* @throws JSONException JSON异常
|
|
|
@@ -286,7 +288,7 @@ public class XMLTokener extends JSONTokener {
|
|
|
|
|
|
/* We will loop, possibly for all of the remaining characters. */
|
|
|
|
|
|
- for (;;) {
|
|
|
+ for (; ; ) {
|
|
|
j = offset;
|
|
|
b = true;
|
|
|
|