1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #362 from johnjaylward/FixXMLUnescape

Fixes XML Unescaping
This commit is contained in:
Sean Leary 2017-08-27 10:59:26 -05:00 committed by GitHub
commit 2565abdaaa
3 changed files with 37 additions and 36 deletions

View file

@ -174,7 +174,7 @@ public class JSONML {
if (!(token instanceof String)) { if (!(token instanceof String)) {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
newjo.accumulate(attribute, keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)); newjo.accumulate(attribute, keepStrings ? ((String)token) :XML.stringToValue((String)token));
token = null; token = null;
} else { } else {
newjo.accumulate(attribute, ""); newjo.accumulate(attribute, "");

View file

@ -141,7 +141,7 @@ public class XML {
if (mustEscape(cp)) { if (mustEscape(cp)) {
sb.append("&#x"); sb.append("&#x");
sb.append(Integer.toHexString(cp)); sb.append(Integer.toHexString(cp));
sb.append(";"); sb.append(';');
} else { } else {
sb.appendCodePoint(cp); sb.appendCodePoint(cp);
} }
@ -191,31 +191,7 @@ public class XML {
final int semic = string.indexOf(';', i); final int semic = string.indexOf(';', i);
if (semic > i) { if (semic > i) {
final String entity = string.substring(i + 1, semic); final String entity = string.substring(i + 1, semic);
if (entity.charAt(0) == '#') { sb.append(XMLTokener.unescapeEntity(entity));
int cp;
if (entity.charAt(1) == 'x') {
// hex encoded unicode
cp = Integer.parseInt(entity.substring(2), 16);
} else {
// decimal encoded unicode
cp = Integer.parseInt(entity.substring(1));
}
sb.appendCodePoint(cp);
} else {
if ("quot".equalsIgnoreCase(entity)) {
sb.append('"');
} else if ("amp".equalsIgnoreCase(entity)) {
sb.append('&');
} else if ("apos".equalsIgnoreCase(entity)) {
sb.append('\'');
} else if ("lt".equalsIgnoreCase(entity)) {
sb.append('<');
} else if ("gt".equalsIgnoreCase(entity)) {
sb.append('>');
} else {// unsupported xml entity. leave encoded
sb.append('&').append(entity).append(';');
}
}
// skip past the entity we just parsed. // skip past the entity we just parsed.
i += entity.length() + 1; i += entity.length() + 1;
} else { } else {
@ -364,7 +340,7 @@ public class XML {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
jsonobject.accumulate(string, jsonobject.accumulate(string,
keepStrings ? unescape((String)token) : stringToValue((String) token)); keepStrings ? ((String)token) : stringToValue((String) token));
token = null; token = null;
} else { } else {
jsonobject.accumulate(string, ""); jsonobject.accumulate(string, "");
@ -396,7 +372,7 @@ public class XML {
string = (String) token; string = (String) token;
if (string.length() > 0) { if (string.length() > 0) {
jsonobject.accumulate("content", jsonobject.accumulate("content",
keepStrings ? unescape(string) : stringToValue(string)); keepStrings ? string : stringToValue(string));
} }
} else if (token == LT) { } else if (token == LT) {
@ -430,11 +406,7 @@ public class XML {
* @return JSON value of this string or the string * @return JSON value of this string or the string
*/ */
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
Object ret = JSONObject.stringToValue(string); return JSONObject.stringToValue(string);
if(ret instanceof String){
return unescape((String)ret);
}
return ret;
} }
/** /**

View file

@ -138,8 +138,37 @@ public class XMLTokener extends JSONTokener {
} }
} }
String string = sb.toString(); String string = sb.toString();
Object object = entity.get(string); return unescapeEntity(string);
return object != null ? object : ampersand + string + ";"; }
/**
* Unescapes an XML entity encoding;
* @param e entity (only the actual entity value, not the preceding & or ending ;
* @return
*/
static String unescapeEntity(String e) {
// validate
if (e == null || e.isEmpty()) {
return "";
}
// if our entity is an encoded unicode point, parse it.
if (e.charAt(0) == '#') {
int cp;
if (e.charAt(1) == 'x') {
// hex encoded unicode
cp = Integer.parseInt(e.substring(2), 16);
} else {
// decimal encoded unicode
cp = Integer.parseInt(e.substring(1));
}
return new String(new int[] {cp},0,1);
}
Character knownEntity = entity.get(e);
if(knownEntity==null) {
// we don't know the entity so keep it encoded
return '&' + e + ';';
}
return knownEntity.toString();
} }