Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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