Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.json

File XMLTokener.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

62
192
10
1
407
263
84
0.44
19.2
10
8.4

Classes

Class Line # Actions
XMLTokener 35 192 84
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package org.json;
2   
3    /*
4    Copyright (c) 2002 JSON.org
5   
6    Permission is hereby granted, free of charge, to any person obtaining a copy
7    of this software and associated documentation files (the "Software"), to deal
8    in the Software without restriction, including without limitation the rights
9    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10    copies of the Software, and to permit persons to whom the Software is
11    furnished to do so, subject to the following conditions:
12   
13    The above copyright notice and this permission notice shall be included in all
14    copies or substantial portions of the Software.
15   
16    The Software shall be used for Good, not Evil.
17   
18    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24    SOFTWARE.
25    */
26   
27    import java.io.Reader;
28   
29    /**
30    * The XMLTokener extends the JSONTokener to provide additional methods
31    * for the parsing of XML texts.
32    * @author JSON.org
33    * @version 2015-12-09
34    */
 
35    public class XMLTokener extends JSONTokener {
36   
37   
38    /** The table of entity values. It initially contains Character values for
39    * amp, apos, gt, lt, quot.
40    */
41    public static final java.util.HashMap<String, Character> entity;
42   
 
43  0 toggle static {
44  0 entity = new java.util.HashMap<String, Character>(8);
45  0 entity.put("amp", XML.AMP);
46  0 entity.put("apos", XML.APOS);
47  0 entity.put("gt", XML.GT);
48  0 entity.put("lt", XML.LT);
49  0 entity.put("quot", XML.QUOT);
50    }
51   
52    /**
53    * Construct an XMLTokener from a Reader.
54    * @param r A source reader.
55    */
 
56  0 toggle public XMLTokener(Reader r) {
57  0 super(r);
58    }
59   
60    /**
61    * Construct an XMLTokener from a string.
62    * @param s A source string.
63    */
 
64  0 toggle public XMLTokener(String s) {
65  0 super(s);
66    }
67   
68    /**
69    * Get the text in the CDATA block.
70    * @return The string up to the <code>]]&gt;</code>.
71    * @throws JSONException If the <code>]]&gt;</code> is not found.
72    */
 
73  0 toggle public String nextCDATA() throws JSONException {
74  0 char c;
75  0 int i;
76  0 StringBuilder sb = new StringBuilder();
77  0 while (more()) {
78  0 c = next();
79  0 sb.append(c);
80  0 i = sb.length() - 3;
81  0 if (i >= 0 && sb.charAt(i) == ']' &&
82    sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
83  0 sb.setLength(i);
84  0 return sb.toString();
85    }
86    }
87  0 throw syntaxError("Unclosed CDATA");
88    }
89   
90   
91    /**
92    * Get the next XML outer token, trimming whitespace. There are two kinds
93    * of tokens: the '<' character which begins a markup tag, and the content
94    * text between markup tags.
95    *
96    * @return A string, or a '<' Character, or null if there is no more
97    * source text.
98    * @throws JSONException
99    */
 
100  0 toggle public Object nextContent() throws JSONException {
101  0 char c;
102  0 StringBuilder sb;
103  0 do {
104  0 c = next();
105  0 } while (Character.isWhitespace(c));
106  0 if (c == 0) {
107  0 return null;
108    }
109  0 if (c == '<') {
110  0 return XML.LT;
111    }
112  0 sb = new StringBuilder();
113  0 for (;;) {
114  0 if (c == 0) {
115  0 return sb.toString().trim();
116    }
117  0 if (c == '<') {
118  0 back();
119  0 return sb.toString().trim();
120    }
121  0 if (c == '&') {
122  0 sb.append(nextEntity(c));
123    } else {
124  0 sb.append(c);
125    }
126  0 c = next();
127    }
128    }
129   
130   
131    /**
132    * Return the next entity. These entities are translated to Characters:
133    * <code>&amp; &apos; &gt; &lt; &quot;</code>.
134    * @param ampersand An ampersand character.
135    * @return A Character or an entity String if the entity is not recognized.
136    * @throws JSONException If missing ';' in XML entity.
137    */
 
138  0 toggle public Object nextEntity(char ampersand) throws JSONException {
139  0 StringBuilder sb = new StringBuilder();
140  0 for (;;) {
141  0 char c = next();
142  0 if (Character.isLetterOrDigit(c) || c == '#') {
143  0 sb.append(Character.toLowerCase(c));
144  0 } else if (c == ';') {
145  0 break;
146    } else {
147  0 throw syntaxError("Missing ';' in XML entity: &" + sb);
148    }
149    }
150  0 String string = sb.toString();
151  0 return unescapeEntity(string);
152    }
153   
154    /**
155    * Unescapes an XML entity encoding;
156    * @param e entity (only the actual entity value, not the preceding & or ending ;
157    * @return
158    */
 
159  0 toggle static String unescapeEntity(String e) {
160    // validate
161  0 if (e == null || e.isEmpty()) {
162  0 return "";
163    }
164    // if our entity is an encoded unicode point, parse it.
165  0 if (e.charAt(0) == '#') {
166  0 int cp;
167  0 if (e.charAt(1) == 'x') {
168    // hex encoded unicode
169  0 cp = Integer.parseInt(e.substring(2), 16);
170    } else {
171    // decimal encoded unicode
172  0 cp = Integer.parseInt(e.substring(1));
173    }
174  0 return new String(new int[] {cp},0,1);
175    }
176  0 Character knownEntity = entity.get(e);
177  0 if(knownEntity==null) {
178    // we don't know the entity so keep it encoded
179  0 return '&' + e + ';';
180    }
181  0 return knownEntity.toString();
182    }
183   
184   
185    /**
186    * Returns the next XML meta token. This is used for skipping over <!...>
187    * and <?...?> structures.
188    * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
189    * Character, and strings and names are returned as Boolean. We don't care
190    * what the values actually are.
191    * @throws JSONException If a string is not properly closed or if the XML
192    * is badly structured.
193    */
 
194  0 toggle public Object nextMeta() throws JSONException {
195  0 char c;
196  0 char q;
197  0 do {
198  0 c = next();
199  0 } while (Character.isWhitespace(c));
200  0 switch (c) {
201  0 case 0:
202  0 throw syntaxError("Misshaped meta tag");
203  0 case '<':
204  0 return XML.LT;
205  0 case '>':
206  0 return XML.GT;
207  0 case '/':
208  0 return XML.SLASH;
209  0 case '=':
210  0 return XML.EQ;
211  0 case '!':
212  0 return XML.BANG;
213  0 case '?':
214  0 return XML.QUEST;
215  0 case '"':
216  0 case '\'':
217  0 q = c;
218  0 for (;;) {
219  0 c = next();
220  0 if (c == 0) {
221  0 throw syntaxError("Unterminated string");
222    }
223  0 if (c == q) {
224  0 return Boolean.TRUE;
225    }
226    }
227  0 default:
228  0 for (;;) {
229  0 c = next();
230  0 if (Character.isWhitespace(c)) {
231  0 return Boolean.TRUE;
232    }
233  0 switch (c) {
234  0 case 0:
235  0 case '<':
236  0 case '>':
237  0 case '/':
238  0 case '=':
239  0 case '!':
240  0 case '?':
241  0 case '"':
242  0 case '\'':
243  0 back();
244  0 return Boolean.TRUE;
245    }
246    }
247    }
248    }
249   
250   
251    /**
252    * Get the next XML Token. These tokens are found inside of angle
253    * brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
254    * may be a string wrapped in single quotes or double quotes, or it may be a
255    * name.
256    * @return a String or a Character.
257    * @throws JSONException If the XML is not well formed.
258    */
 
259  0 toggle public Object nextToken() throws JSONException {
260  0 char c;
261  0 char q;
262  0 StringBuilder sb;
263  0 do {
264  0 c = next();
265  0 } while (Character.isWhitespace(c));
266  0 switch (c) {
267  0 case 0:
268  0 throw syntaxError("Misshaped element");
269  0 case '<':
270  0 throw syntaxError("Misplaced '<'");
271  0 case '>':
272  0 return XML.GT;
273  0 case '/':
274  0 return XML.SLASH;
275  0 case '=':
276  0 return XML.EQ;
277  0 case '!':
278  0 return XML.BANG;
279  0 case '?':
280  0 return XML.QUEST;
281   
282    // Quoted string
283   
284  0 case '"':
285  0 case '\'':
286  0 q = c;
287  0 sb = new StringBuilder();
288  0 for (;;) {
289  0 c = next();
290  0 if (c == 0) {
291  0 throw syntaxError("Unterminated string");
292    }
293  0 if (c == q) {
294  0 return sb.toString();
295    }
296  0 if (c == '&') {
297  0 sb.append(nextEntity(c));
298    } else {
299  0 sb.append(c);
300    }
301    }
302  0 default:
303   
304    // Name
305   
306  0 sb = new StringBuilder();
307  0 for (;;) {
308  0 sb.append(c);
309  0 c = next();
310  0 if (Character.isWhitespace(c)) {
311  0 return sb.toString();
312    }
313  0 switch (c) {
314  0 case 0:
315  0 return sb.toString();
316  0 case '>':
317  0 case '/':
318  0 case '=':
319  0 case '!':
320  0 case '?':
321  0 case '[':
322  0 case ']':
323  0 back();
324  0 return sb.toString();
325  0 case '<':
326  0 case '"':
327  0 case '\'':
328  0 throw syntaxError("Bad character in a name");
329    }
330    }
331    }
332    }
333   
334   
335    /**
336    * Skip characters until past the requested string.
337    * If it is not found, we are left at the end of the source with a result of false.
338    * @param to A string to skip past.
339    */
340    // The Android implementation of JSONTokener has a public method of public void skipPast(String to)
341    // even though ours does not have that method, to have API compatibility, our method in the subclass
342    // should match.
 
343  0 toggle public void skipPast(String to) {
344  0 boolean b;
345  0 char c;
346  0 int i;
347  0 int j;
348  0 int offset = 0;
349  0 int length = to.length();
350  0 char[] circle = new char[length];
351   
352    /*
353    * First fill the circle buffer with as many characters as are in the
354    * to string. If we reach an early end, bail.
355    */
356   
357  0 for (i = 0; i < length; i += 1) {
358  0 c = next();
359  0 if (c == 0) {
360  0 return;
361    }
362  0 circle[i] = c;
363    }
364   
365    /* We will loop, possibly for all of the remaining characters. */
366   
367  0 for (;;) {
368  0 j = offset;
369  0 b = true;
370   
371    /* Compare the circle buffer with the to string. */
372   
373  0 for (i = 0; i < length; i += 1) {
374  0 if (circle[j] != to.charAt(i)) {
375  0 b = false;
376  0 break;
377    }
378  0 j += 1;
379  0 if (j >= length) {
380  0 j -= length;
381    }
382    }
383   
384    /* If we exit the loop with b intact, then victory is ours. */
385   
386  0 if (b) {
387  0 return;
388    }
389   
390    /* Get the next character. If there isn't one, then defeat is ours. */
391   
392  0 c = next();
393  0 if (c == 0) {
394  0 return;
395    }
396    /*
397    * Shove the character in the circle buffer and advance the
398    * circle offset. The offset is mod n.
399    */
400  0 circle[offset] = c;
401  0 offset += 1;
402  0 if (offset >= length) {
403  0 offset -= length;
404    }
405    }
406    }
407    }