mirror of
https://github.com/stleary/JSON-java.git
synced 2026-03-20 00:00:50 -04:00
Refactor XMLTokener.unescapeEntity() to reduce complexity
Extracted hex and decimal parsing logic into separate methods to address SonarQube complexity warning: - parseHexEntity(): handles ઼ format - parseDecimalEntity(): handles { format This reduces cyclomatic complexity while maintaining identical functionality and all validation checks.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -16,3 +16,6 @@ build
|
|||||||
/gradlew
|
/gradlew
|
||||||
/gradlew.bat
|
/gradlew.bat
|
||||||
.gitmodules
|
.gitmodules
|
||||||
|
|
||||||
|
# ignore compiled class files
|
||||||
|
*.class
|
||||||
|
|||||||
@@ -161,37 +161,12 @@ public class XMLTokener extends JSONTokener {
|
|||||||
}
|
}
|
||||||
// if our entity is an encoded unicode point, parse it.
|
// if our entity is an encoded unicode point, parse it.
|
||||||
if (e.charAt(0) == '#') {
|
if (e.charAt(0) == '#') {
|
||||||
int cp;
|
|
||||||
// Check minimum length for numeric character reference
|
|
||||||
if (e.length() < 2) {
|
if (e.length() < 2) {
|
||||||
throw new JSONException("Invalid numeric character reference: &#;");
|
throw new JSONException("Invalid numeric character reference: &#;");
|
||||||
}
|
}
|
||||||
if (e.charAt(1) == 'x' || e.charAt(1) == 'X') {
|
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
||||||
// hex encoded unicode - need at least one hex digit after #x
|
? parseHexEntity(e)
|
||||||
if (e.length() < 3) {
|
: parseDecimalEntity(e);
|
||||||
throw new JSONException("Invalid hex character reference: missing hex digits in &#" + e.substring(1) + ";");
|
|
||||||
}
|
|
||||||
String hex = e.substring(2);
|
|
||||||
if (!isValidHex(hex)) {
|
|
||||||
throw new JSONException("Invalid hex character reference: &#" + e.substring(1) + ";");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
cp = Integer.parseInt(hex, 16);
|
|
||||||
} catch (NumberFormatException nfe) {
|
|
||||||
throw new JSONException("Invalid hex character reference: &#" + e.substring(1) + ";", nfe);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// decimal encoded unicode
|
|
||||||
String decimal = e.substring(1);
|
|
||||||
if (!isValidDecimal(decimal)) {
|
|
||||||
throw new JSONException("Invalid decimal character reference: &#" + decimal + ";");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
cp = Integer.parseInt(decimal);
|
|
||||||
} catch (NumberFormatException nfe) {
|
|
||||||
throw new JSONException("Invalid decimal character reference: &#" + decimal + ";", nfe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new String(new int[] {cp}, 0, 1);
|
return new String(new int[] {cp}, 0, 1);
|
||||||
}
|
}
|
||||||
Character knownEntity = entity.get(e);
|
Character knownEntity = entity.get(e);
|
||||||
@@ -202,6 +177,46 @@ public class XMLTokener extends JSONTokener {
|
|||||||
return knownEntity.toString();
|
return knownEntity.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a hexadecimal numeric character reference (e.g., "઼").
|
||||||
|
* @param e entity string starting with '#' (e.g., "#x1F4A9")
|
||||||
|
* @return the Unicode code point
|
||||||
|
* @throws JSONException if the format is invalid
|
||||||
|
*/
|
||||||
|
private static int parseHexEntity(String e) throws JSONException {
|
||||||
|
// hex encoded unicode - need at least one hex digit after #x
|
||||||
|
if (e.length() < 3) {
|
||||||
|
throw new JSONException("Invalid hex character reference: missing hex digits in &#" + e.substring(1) + ";");
|
||||||
|
}
|
||||||
|
String hex = e.substring(2);
|
||||||
|
if (!isValidHex(hex)) {
|
||||||
|
throw new JSONException("Invalid hex character reference: &#" + e.substring(1) + ";");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(hex, 16);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
throw new JSONException("Invalid hex character reference: &#" + e.substring(1) + ";", nfe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a decimal numeric character reference (e.g., "{").
|
||||||
|
* @param e entity string starting with '#' (e.g., "#123")
|
||||||
|
* @return the Unicode code point
|
||||||
|
* @throws JSONException if the format is invalid
|
||||||
|
*/
|
||||||
|
private static int parseDecimalEntity(String e) throws JSONException {
|
||||||
|
String decimal = e.substring(1);
|
||||||
|
if (!isValidDecimal(decimal)) {
|
||||||
|
throw new JSONException("Invalid decimal character reference: &#" + decimal + ";");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(decimal);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
throw new JSONException("Invalid decimal character reference: &#" + decimal + ";", nfe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a string contains only valid hexadecimal digits.
|
* Check if a string contains only valid hexadecimal digits.
|
||||||
* @param s the string to check
|
* @param s the string to check
|
||||||
|
|||||||
Reference in New Issue
Block a user