001    package org.apache.tapestry.json;
002    
003    /*
004    Copyright (c) 2002 JSON.org
005    
006    Permission is hereby granted, free of charge, to any person obtaining a copy
007    of this software and associated documentation files (the "Software"), to deal
008    in the Software without restriction, including without limitation the rights
009    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010    copies of the Software, and to permit persons to whom the Software is
011    furnished to do so, subject to the following conditions:
012    
013    The above copyright notice and this permission notice shall be included in all
014    copies or substantial portions of the Software.
015    
016    The Software shall be used for Good, not Evil.
017    
018    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024    SOFTWARE.
025    */
026    
027    import java.text.ParseException;
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 0.1
034     */
035    public class HTTPTokener extends JSONTokener {
036    
037        /**
038         * Construct an XMLTokener from a string.
039         * @param s A source string.
040         */
041        public HTTPTokener(String s) {
042            super(s);
043        }
044    
045    
046        /**
047         * Get the next token or string. This is used in parsing HTTP headers.
048         * @throws ParseException
049         * @return A String.
050         */
051        public String nextToken() throws ParseException {
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                while (true) {
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            while (true) {
072                if (c == 0 || Character.isWhitespace(c)) {
073                    return sb.toString();
074                }
075                sb.append(c);
076                c = next();
077            }
078        }
079    }