001package org.json;
002
003/*
004Copyright (c) 2002 JSON.org
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016The Software shall be used for Good, not Evil.
017
018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024SOFTWARE.
025 */
026
027/**
028 * The HTTPTokener extends the JSONTokener to provide additional methods
029 * for the parsing of HTTP headers.
030 * @author JSON.org
031@version $Id$
032@since Ptolemy II 10.0
033 * @version 2008-09-18
034 */
035public class HTTPTokener extends JSONTokener {
036
037    /**
038     * Construct an HTTPTokener from a string.
039     * @param s A source string.
040     */
041    public HTTPTokener(String s) {
042        super(s);
043    }
044
045    /**
046     * Get the next token or string. This is used in parsing HTTP headers.
047     * @exception JSONException
048     * @return A String.
049     */
050    public String nextToken() throws JSONException {
051        char c;
052        char q;
053        StringBuffer sb = new StringBuffer();
054        do {
055            c = next();
056        } while (Character.isWhitespace(c));
057        if (c == '"' || c == '\'') {
058            q = c;
059            for (;;) {
060                c = next();
061                if (c < ' ') {
062                    throw syntaxError("Unterminated string.");
063                }
064                if (c == q) {
065                    return sb.toString();
066                }
067                sb.append(c);
068            }
069        }
070        for (;;) {
071            if (c == 0 || Character.isWhitespace(c)) {
072                return sb.toString();
073            }
074            sb.append(c);
075            c = next();
076        }
077    }
078}