root / src / org / json / HTTPTokener.java @ 4bf05cad
History | View | Annotate | Download (2.3 kB)
1 | 14ad7326 | pastith | package org.json; |
---|---|---|---|
2 | 14ad7326 | pastith | |
3 | 14ad7326 | pastith | /*
|
4 | 14ad7326 | pastith | Copyright (c) 2002 JSON.org
|
5 | 14ad7326 | pastith | |
6 | 14ad7326 | pastith | Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 14ad7326 | pastith | of this software and associated documentation files (the "Software"), to deal
|
8 | 14ad7326 | pastith | in the Software without restriction, including without limitation the rights
|
9 | 14ad7326 | pastith | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 14ad7326 | pastith | copies of the Software, and to permit persons to whom the Software is
|
11 | 14ad7326 | pastith | furnished to do so, subject to the following conditions:
|
12 | 14ad7326 | pastith | |
13 | 14ad7326 | pastith | The above copyright notice and this permission notice shall be included in all
|
14 | 14ad7326 | pastith | copies or substantial portions of the Software.
|
15 | 14ad7326 | pastith | |
16 | 14ad7326 | pastith | The Software shall be used for Good, not Evil.
|
17 | 14ad7326 | pastith | |
18 | 14ad7326 | pastith | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | 14ad7326 | pastith | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | 14ad7326 | pastith | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21 | 14ad7326 | pastith | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | 14ad7326 | pastith | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | 14ad7326 | pastith | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24 | 14ad7326 | pastith | SOFTWARE.
|
25 | 14ad7326 | pastith | */
|
26 | 14ad7326 | pastith | |
27 | 14ad7326 | pastith | /**
|
28 | 14ad7326 | pastith | * The HTTPTokener extends the JSONTokener to provide additional methods
|
29 | 14ad7326 | pastith | * for the parsing of HTTP headers.
|
30 | 14ad7326 | pastith | * @author JSON.org
|
31 | 14ad7326 | pastith | * @version 2008-09-18
|
32 | 14ad7326 | pastith | */
|
33 | 14ad7326 | pastith | public class HTTPTokener extends JSONTokener { |
34 | 14ad7326 | pastith | |
35 | 14ad7326 | pastith | /**
|
36 | 14ad7326 | pastith | * Construct an XMLTokener from a string.
|
37 | 14ad7326 | pastith | * @param s A source string.
|
38 | 14ad7326 | pastith | */
|
39 | 14ad7326 | pastith | public HTTPTokener(String s) { |
40 | 14ad7326 | pastith | super(s);
|
41 | 14ad7326 | pastith | } |
42 | 14ad7326 | pastith | |
43 | 14ad7326 | pastith | |
44 | 14ad7326 | pastith | /**
|
45 | 14ad7326 | pastith | * Get the next token or string. This is used in parsing HTTP headers.
|
46 | 14ad7326 | pastith | * @throws JSONException
|
47 | 14ad7326 | pastith | * @return A String.
|
48 | 14ad7326 | pastith | */
|
49 | 14ad7326 | pastith | public String nextToken() throws JSONException { |
50 | 14ad7326 | pastith | char c;
|
51 | 14ad7326 | pastith | char q;
|
52 | 14ad7326 | pastith | StringBuffer sb = new StringBuffer(); |
53 | 14ad7326 | pastith | do {
|
54 | 14ad7326 | pastith | c = next(); |
55 | 14ad7326 | pastith | } while (Character.isWhitespace(c)); |
56 | 14ad7326 | pastith | if (c == '"' || c == '\'') { |
57 | 14ad7326 | pastith | q = c; |
58 | 14ad7326 | pastith | for (;;) {
|
59 | 14ad7326 | pastith | c = next(); |
60 | 14ad7326 | pastith | if (c < ' ') { |
61 | 14ad7326 | pastith | throw syntaxError("Unterminated string."); |
62 | 14ad7326 | pastith | } |
63 | 14ad7326 | pastith | if (c == q) {
|
64 | 14ad7326 | pastith | return sb.toString();
|
65 | 14ad7326 | pastith | } |
66 | 14ad7326 | pastith | sb.append(c); |
67 | 14ad7326 | pastith | } |
68 | 14ad7326 | pastith | } |
69 | 14ad7326 | pastith | for (;;) {
|
70 | 14ad7326 | pastith | if (c == 0 || Character.isWhitespace(c)) { |
71 | 14ad7326 | pastith | return sb.toString();
|
72 | 14ad7326 | pastith | } |
73 | 14ad7326 | pastith | sb.append(c); |
74 | 14ad7326 | pastith | c = next(); |
75 | 14ad7326 | pastith | } |
76 | 14ad7326 | pastith | } |
77 | 14ad7326 | pastith | } |