Skip to content

Commit 386673e

Browse files
author
Mathieu Hubbard
committed
Merge remote-tracking branch 'origin/master'
2 parents 958e8b7 + fe1a386 commit 386673e

1 file changed

Lines changed: 53 additions & 20 deletions

File tree

XML.java

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
import java.util.Iterator;
28-
import java.util.Map.Entry;
2928

3029
/**
3130
* This provides static methods to convert an XML text into a JSONObject, and to
@@ -141,7 +140,7 @@ public static String escape(String string) {
141140
if (mustEscape(cp)) {
142141
sb.append("&#x");
143142
sb.append(Integer.toHexString(cp));
144-
sb.append(';');
143+
sb.append(";");
145144
} else {
146145
sb.appendCodePoint(cp);
147146
}
@@ -191,12 +190,36 @@ public static String unescape(String string) {
191190
final int semic = string.indexOf(';', i);
192191
if (semic > i) {
193192
final String entity = string.substring(i + 1, semic);
194-
sb.append(XMLTokener.unescapeEntity(entity));
193+
if (entity.charAt(0) == '#') {
194+
int cp;
195+
if (entity.charAt(1) == 'x') {
196+
// hex encoded unicode
197+
cp = Integer.parseInt(entity.substring(2), 16);
198+
} else {
199+
// decimal encoded unicode
200+
cp = Integer.parseInt(entity.substring(1));
201+
}
202+
sb.appendCodePoint(cp);
203+
} else {
204+
if ("quot".equalsIgnoreCase(entity)) {
205+
sb.append('"');
206+
} else if ("amp".equalsIgnoreCase(entity)) {
207+
sb.append('&');
208+
} else if ("apos".equalsIgnoreCase(entity)) {
209+
sb.append('\'');
210+
} else if ("lt".equalsIgnoreCase(entity)) {
211+
sb.append('<');
212+
} else if ("gt".equalsIgnoreCase(entity)) {
213+
sb.append('>');
214+
} else {
215+
sb.append('&').append(entity).append(';');
216+
}
217+
}
195218
// skip past the entity we just parsed.
196219
i += entity.length() + 1;
197220
} else {
198221
// this shouldn't happen in most cases since the parser
199-
// errors on unclosed entries.
222+
// errors on unclosed enties.
200223
sb.append(c);
201224
}
202225
} else {
@@ -340,7 +363,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
340363
throw x.syntaxError("Missing value");
341364
}
342365
jsonobject.accumulate(string,
343-
keepStrings ? ((String)token) : stringToValue((String) token));
366+
keepStrings ? unescape((String)token) : stringToValue((String) token));
344367
token = null;
345368
} else {
346369
jsonobject.accumulate(string, "");
@@ -372,7 +395,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
372395
string = (String) token;
373396
if (string.length() > 0) {
374397
jsonobject.accumulate("content",
375-
keepStrings ? string : stringToValue(string));
398+
keepStrings ? unescape(string) : stringToValue(string));
376399
}
377400

378401
} else if (token == LT) {
@@ -399,14 +422,18 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
399422
}
400423

401424
/**
402-
* This method is the same as {@link JSONObject#stringToValue(String)}
425+
* This method is the same as {@link JSONObject.stringToValue(String)}
403426
* except that this also tries to unescape String values.
404427
*
405428
* @param string String to convert
406429
* @return JSON value of this string or the string
407430
*/
408431
public static Object stringToValue(String string) {
409-
return JSONObject.stringToValue(string);
432+
Object ret = JSONObject.stringToValue(string);
433+
if(ret instanceof String){
434+
return unescape((String)ret);
435+
}
436+
return ret;
410437
}
411438

412439
/**
@@ -481,12 +508,15 @@ public static String toString(Object object) throws JSONException {
481508
* @return A string.
482509
* @throws JSONException Thrown if there is an error parsing the string
483510
*/
484-
public static String toString(final Object object, final String tagName)
511+
public static String toString(Object object, String tagName)
485512
throws JSONException {
486513
StringBuilder sb = new StringBuilder();
487514
JSONArray ja;
488515
JSONObject jo;
516+
String key;
517+
Iterator<String> keys;
489518
String string;
519+
Object value;
490520

491521
if (object instanceof JSONObject) {
492522

@@ -507,6 +537,7 @@ public static String toString(final Object object, final String tagName)
507537
} else if (value.getClass().isArray()) {
508538
value = new JSONArray(value);
509539
}
540+
string = value instanceof String ? (String) value : null;
510541

511542
// Emit content in body
512543
if ("content".equals(key)) {
@@ -573,19 +604,21 @@ public static String toString(final Object object, final String tagName)
573604

574605
}
575606

576-
if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
577-
if(object.getClass().isArray()) {
578-
ja = new JSONArray(object);
579-
} else {
580-
ja = (JSONArray) object;
607+
if (object != null) {
608+
if (object.getClass().isArray()) {
609+
object = new JSONArray(object);
581610
}
582-
for (Object val : ja) {
583-
// XML does not have good support for arrays. If an array
584-
// appears in a place where XML is lacking, synthesize an
585-
// <array> element.
586-
sb.append(toString(val, tagName == null ? "array" : tagName));
611+
612+
if (object instanceof JSONArray) {
613+
ja = (JSONArray) object;
614+
for (Object val : ja) {
615+
// XML does not have good support for arrays. If an array
616+
// appears in a place where XML is lacking, synthesize an
617+
// <array> element.
618+
sb.append(toString(val, tagName == null ? "array" : tagName));
619+
}
620+
return sb.toString();
587621
}
588-
return sb.toString();
589622
}
590623

591624
string = (object == null) ? "null" : escape(object.toString());

0 commit comments

Comments
 (0)