@@ -161,37 +161,12 @@ static String unescapeEntity(String e) throws JSONException {
161161 }
162162 // if our entity is an encoded unicode point, parse it.
163163 if (e .charAt (0 ) == '#' ) {
164- int cp ;
165- // Check minimum length for numeric character reference
166164 if (e .length () < 2 ) {
167165 throw new JSONException ("Invalid numeric character reference: &#;" );
168166 }
169- if (e .charAt (1 ) == 'x' || e .charAt (1 ) == 'X' ) {
170- // hex encoded unicode - need at least one hex digit after #x
171- if (e .length () < 3 ) {
172- throw new JSONException ("Invalid hex character reference: missing hex digits in &#" + e .substring (1 ) + ";" );
173- }
174- String hex = e .substring (2 );
175- if (!isValidHex (hex )) {
176- throw new JSONException ("Invalid hex character reference: &#" + e .substring (1 ) + ";" );
177- }
178- try {
179- cp = Integer .parseInt (hex , 16 );
180- } catch (NumberFormatException nfe ) {
181- throw new JSONException ("Invalid hex character reference: &#" + e .substring (1 ) + ";" , nfe );
182- }
183- } else {
184- // decimal encoded unicode
185- String decimal = e .substring (1 );
186- if (!isValidDecimal (decimal )) {
187- throw new JSONException ("Invalid decimal character reference: &#" + decimal + ";" );
188- }
189- try {
190- cp = Integer .parseInt (decimal );
191- } catch (NumberFormatException nfe ) {
192- throw new JSONException ("Invalid decimal character reference: &#" + decimal + ";" , nfe );
193- }
194- }
167+ int cp = (e .charAt (1 ) == 'x' || e .charAt (1 ) == 'X' )
168+ ? parseHexEntity (e )
169+ : parseDecimalEntity (e );
195170 return new String (new int [] {cp }, 0 , 1 );
196171 }
197172 Character knownEntity = entity .get (e );
@@ -202,6 +177,46 @@ static String unescapeEntity(String e) throws JSONException {
202177 return knownEntity .toString ();
203178 }
204179
180+ /**
181+ * Parse a hexadecimal numeric character reference (e.g., "઼").
182+ * @param e entity string starting with '#' (e.g., "#x1F4A9")
183+ * @return the Unicode code point
184+ * @throws JSONException if the format is invalid
185+ */
186+ private static int parseHexEntity (String e ) throws JSONException {
187+ // hex encoded unicode - need at least one hex digit after #x
188+ if (e .length () < 3 ) {
189+ throw new JSONException ("Invalid hex character reference: missing hex digits in &#" + e .substring (1 ) + ";" );
190+ }
191+ String hex = e .substring (2 );
192+ if (!isValidHex (hex )) {
193+ throw new JSONException ("Invalid hex character reference: &#" + e .substring (1 ) + ";" );
194+ }
195+ try {
196+ return Integer .parseInt (hex , 16 );
197+ } catch (NumberFormatException nfe ) {
198+ throw new JSONException ("Invalid hex character reference: &#" + e .substring (1 ) + ";" , nfe );
199+ }
200+ }
201+
202+ /**
203+ * Parse a decimal numeric character reference (e.g., "{").
204+ * @param e entity string starting with '#' (e.g., "#123")
205+ * @return the Unicode code point
206+ * @throws JSONException if the format is invalid
207+ */
208+ private static int parseDecimalEntity (String e ) throws JSONException {
209+ String decimal = e .substring (1 );
210+ if (!isValidDecimal (decimal )) {
211+ throw new JSONException ("Invalid decimal character reference: &#" + decimal + ";" );
212+ }
213+ try {
214+ return Integer .parseInt (decimal );
215+ } catch (NumberFormatException nfe ) {
216+ throw new JSONException ("Invalid decimal character reference: &#" + decimal + ";" , nfe );
217+ }
218+ }
219+
205220 /**
206221 * Check if a string contains only valid hexadecimal digits.
207222 * @param s the string to check
0 commit comments