1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 import java.util.HashMap;
\r
26 import java.util.Map;
\r
29 Copyright (c) 2002 JSON.org
\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
38 The above copyright notice and this permission notice shall be included in all
\r
39 copies or substantial portions of the Software.
\r
41 The Software shall be used for Good, not Evil.
\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
53 * The XMLTokener extends the JSONTokener to provide additional methods
\r
54 * for the parsing of XML texts.
\r
56 * @version 2012-11-13
\r
58 public class XMLTokener extends JSONTokener {
\r
61 /** The table of entity values. It initially contains Character values for
\r
62 * amp, apos, gt, lt, quot.
\r
64 public static final Map<String,Character> entity;
\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
76 * Construct an XMLTokener from a string.
\r
77 * @param s A source string.
\r
79 public XMLTokener(String s) {
\r
84 * Get the text in the CDATA block.
\r
85 * @return The string up to the <code>]]></code>.
\r
86 * @throws JSONException If the <code>]]></code> is not found.
\r
88 public String nextCDATA() throws JSONException {
\r
91 StringBuffer sb = new StringBuffer();
\r
95 throw syntaxError("Unclosed CDATA");
\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
102 return sb.toString();
\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
113 * @return A string, or a '<' Character, or null if there is no more
\r
115 * @throws JSONException
\r
117 public Object nextContent() throws JSONException {
\r
122 } while (Character.isWhitespace(c));
\r
129 sb = new StringBuffer();
\r
131 if (c == '<' || c == 0) {
\r
133 return sb.toString().trim();
\r
136 sb.append(nextEntity(c));
\r
146 * Return the next entity. These entities are translated to Characters:
\r
147 * <code>& ' > < "</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
152 public Object nextEntity(char ampersand) throws JSONException {
\r
153 StringBuffer sb = new StringBuffer();
\r
156 if (Character.isLetterOrDigit(c) || c == '#') {
\r
157 sb.append(Character.toLowerCase(c));
\r
158 } else if (c == ';') {
\r
161 throw syntaxError("Missing ';' in XML entity: &" + sb);
\r
164 String string = sb.toString();
\r
165 Object object = entity.get(string);
\r
166 return object != null ? object : ampersand + string + ";";
\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
179 public Object nextMeta() throws JSONException {
\r
184 } while (Character.isWhitespace(c));
\r
187 throw syntaxError("Misshaped meta tag");
\r
206 throw syntaxError("Unterminated string");
\r
209 return Boolean.TRUE;
\r
215 if (Character.isWhitespace(c)) {
\r
216 return Boolean.TRUE;
\r
229 return Boolean.TRUE;
\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
241 * @return a String or a Character.
\r
242 * @throws JSONException If the XML is not well formed.
\r
244 public Object nextToken() throws JSONException {
\r
250 } while (Character.isWhitespace(c));
\r
253 throw syntaxError("Misshaped element");
\r
255 throw syntaxError("Misplaced '<'");
\r
272 sb = new StringBuffer();
\r
276 throw syntaxError("Unterminated string");
\r
279 return sb.toString();
\r
282 sb.append(nextEntity(c));
\r
291 sb = new StringBuffer();
\r
295 if (Character.isWhitespace(c)) {
\r
296 return sb.toString();
\r
300 return sb.toString();
\r
309 return sb.toString();
\r
313 throw syntaxError("Bad character in a name");
\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
326 public boolean skipPast(String to) throws JSONException {
\r
332 int length = to.length();
\r
333 char[] circle = new char[length];
\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
340 for (i = 0; i < length; i += 1) {
\r
348 /* We will loop, possibly for all of the remaining characters. */
\r
354 /* Compare the circle buffer with the to string. */
\r
356 for (i = 0; i < length; i += 1) {
\r
357 if (circle[j] != to.charAt(i)) {
\r
367 /* If we exit the loop with b intact, then victory is ours. */
\r
373 /* Get the next character. If there isn't one, then defeat is ours. */
\r
380 * Shove the character in the circle buffer and advance the
\r
381 * circle offset. The offset is mod n.
\r
383 circle[offset] = c;
\r
385 if (offset >= length) {
\r