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