001
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
027package armyc2.c5isr.web.json.utilities;
028
029/**
030 * The HTTPTokener extends the JSONTokener to provide additional methods
031 * for the parsing of HTTP headers.
032 * @author JSON.org
033 * @version 2010-12-24
034 */
035public class HTTPTokener extends JSONTokener {
036
037    /**
038     * Construct an HTTPTokener from a string.
039     * @param string A source string.
040     */
041    public HTTPTokener(String string) {
042        super(string);
043    }
044
045
046    /**
047     * Get the next token or string. This is used in parsing HTTP headers.
048     * @throws JSONException
049     * @return A String.
050     */
051    public String nextToken() throws JSONException {
052        char c;
053        char q;
054        StringBuffer sb = new StringBuffer();
055        do {
056            c = next();
057        } while (Character.isWhitespace(c));
058        if (c == '"' || c == '\'') {
059            q = c;
060            for (;;) {
061                c = next();
062                if (c < ' ') {
063                    throw syntaxError("Unterminated string.");
064                }
065                if (c == q) {
066                    return sb.toString();
067                }
068                sb.append(c);
069            }
070        } 
071        for (;;) {
072            if (c == 0 || Character.isWhitespace(c)) {
073                return sb.toString();
074            }
075            sb.append(c);
076            c = next();
077        }
078    }
079}