michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.message; michael@0: michael@0: import java.util.List; michael@0: import java.util.ArrayList; michael@0: michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.NameValuePair; michael@0: import ch.boye.httpclientandroidlib.ParseException; michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * Basic implementation for parsing header values into elements. michael@0: * Instances of this class are stateless and thread-safe. michael@0: * Derived classes are expected to maintain these properties. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class BasicHeaderValueParser implements HeaderValueParser { michael@0: michael@0: /** michael@0: * A default instance of this class, for use as default or fallback. michael@0: * Note that {@link BasicHeaderValueParser} is not a singleton, there michael@0: * can be many instances of the class itself and of derived classes. michael@0: * The instance here provides non-customized, default behavior. michael@0: */ michael@0: public final static michael@0: BasicHeaderValueParser DEFAULT = new BasicHeaderValueParser(); michael@0: michael@0: private final static char PARAM_DELIMITER = ';'; michael@0: private final static char ELEM_DELIMITER = ','; michael@0: private final static char[] ALL_DELIMITERS = new char[] { michael@0: PARAM_DELIMITER, michael@0: ELEM_DELIMITER michael@0: }; michael@0: michael@0: // public default constructor michael@0: michael@0: michael@0: /** michael@0: * Parses elements with the given parser. michael@0: * michael@0: * @param value the header value to parse michael@0: * @param parser the parser to use, or null for default michael@0: * michael@0: * @return array holding the header elements, never null michael@0: */ michael@0: public final static michael@0: HeaderElement[] parseElements(final String value, michael@0: HeaderValueParser parser) michael@0: throws ParseException { michael@0: michael@0: if (value == null) { michael@0: throw new IllegalArgumentException michael@0: ("Value to parse may not be null"); michael@0: } michael@0: michael@0: if (parser == null) michael@0: parser = BasicHeaderValueParser.DEFAULT; michael@0: michael@0: CharArrayBuffer buffer = new CharArrayBuffer(value.length()); michael@0: buffer.append(value); michael@0: ParserCursor cursor = new ParserCursor(0, value.length()); michael@0: return parser.parseElements(buffer, cursor); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HeaderValueParser michael@0: public HeaderElement[] parseElements(final CharArrayBuffer buffer, michael@0: final ParserCursor cursor) { michael@0: michael@0: if (buffer == null) { michael@0: throw new IllegalArgumentException("Char array buffer may not be null"); michael@0: } michael@0: if (cursor == null) { michael@0: throw new IllegalArgumentException("Parser cursor may not be null"); michael@0: } michael@0: michael@0: List elements = new ArrayList(); michael@0: while (!cursor.atEnd()) { michael@0: HeaderElement element = parseHeaderElement(buffer, cursor); michael@0: if (!(element.getName().length() == 0 && element.getValue() == null)) { michael@0: elements.add(element); michael@0: } michael@0: } michael@0: return (HeaderElement[]) michael@0: elements.toArray(new HeaderElement[elements.size()]); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Parses an element with the given parser. michael@0: * michael@0: * @param value the header element to parse michael@0: * @param parser the parser to use, or null for default michael@0: * michael@0: * @return the parsed header element michael@0: */ michael@0: public final static michael@0: HeaderElement parseHeaderElement(final String value, michael@0: HeaderValueParser parser) michael@0: throws ParseException { michael@0: michael@0: if (value == null) { michael@0: throw new IllegalArgumentException michael@0: ("Value to parse may not be null"); michael@0: } michael@0: michael@0: if (parser == null) michael@0: parser = BasicHeaderValueParser.DEFAULT; michael@0: michael@0: CharArrayBuffer buffer = new CharArrayBuffer(value.length()); michael@0: buffer.append(value); michael@0: ParserCursor cursor = new ParserCursor(0, value.length()); michael@0: return parser.parseHeaderElement(buffer, cursor); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HeaderValueParser michael@0: public HeaderElement parseHeaderElement(final CharArrayBuffer buffer, michael@0: final ParserCursor cursor) { michael@0: michael@0: if (buffer == null) { michael@0: throw new IllegalArgumentException("Char array buffer may not be null"); michael@0: } michael@0: if (cursor == null) { michael@0: throw new IllegalArgumentException("Parser cursor may not be null"); michael@0: } michael@0: michael@0: NameValuePair nvp = parseNameValuePair(buffer, cursor); michael@0: NameValuePair[] params = null; michael@0: if (!cursor.atEnd()) { michael@0: char ch = buffer.charAt(cursor.getPos() - 1); michael@0: if (ch != ELEM_DELIMITER) { michael@0: params = parseParameters(buffer, cursor); michael@0: } michael@0: } michael@0: return createHeaderElement(nvp.getName(), nvp.getValue(), params); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Creates a header element. michael@0: * Called from {@link #parseHeaderElement}. michael@0: * michael@0: * @return a header element representing the argument michael@0: */ michael@0: protected HeaderElement createHeaderElement( michael@0: final String name, michael@0: final String value, michael@0: final NameValuePair[] params) { michael@0: return new BasicHeaderElement(name, value, params); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Parses parameters with the given parser. michael@0: * michael@0: * @param value the parameter list to parse michael@0: * @param parser the parser to use, or null for default michael@0: * michael@0: * @return array holding the parameters, never null michael@0: */ michael@0: public final static michael@0: NameValuePair[] parseParameters(final String value, michael@0: HeaderValueParser parser) michael@0: throws ParseException { michael@0: michael@0: if (value == null) { michael@0: throw new IllegalArgumentException michael@0: ("Value to parse may not be null"); michael@0: } michael@0: michael@0: if (parser == null) michael@0: parser = BasicHeaderValueParser.DEFAULT; michael@0: michael@0: CharArrayBuffer buffer = new CharArrayBuffer(value.length()); michael@0: buffer.append(value); michael@0: ParserCursor cursor = new ParserCursor(0, value.length()); michael@0: return parser.parseParameters(buffer, cursor); michael@0: } michael@0: michael@0: michael@0: michael@0: // non-javadoc, see interface HeaderValueParser michael@0: public NameValuePair[] parseParameters(final CharArrayBuffer buffer, michael@0: final ParserCursor cursor) { michael@0: michael@0: if (buffer == null) { michael@0: throw new IllegalArgumentException("Char array buffer may not be null"); michael@0: } michael@0: if (cursor == null) { michael@0: throw new IllegalArgumentException("Parser cursor may not be null"); michael@0: } michael@0: michael@0: int pos = cursor.getPos(); michael@0: int indexTo = cursor.getUpperBound(); michael@0: michael@0: while (pos < indexTo) { michael@0: char ch = buffer.charAt(pos); michael@0: if (HTTP.isWhitespace(ch)) { michael@0: pos++; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: cursor.updatePos(pos); michael@0: if (cursor.atEnd()) { michael@0: return new NameValuePair[] {}; michael@0: } michael@0: michael@0: List params = new ArrayList(); michael@0: while (!cursor.atEnd()) { michael@0: NameValuePair param = parseNameValuePair(buffer, cursor); michael@0: params.add(param); michael@0: char ch = buffer.charAt(cursor.getPos() - 1); michael@0: if (ch == ELEM_DELIMITER) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return (NameValuePair[]) michael@0: params.toArray(new NameValuePair[params.size()]); michael@0: } michael@0: michael@0: /** michael@0: * Parses a name-value-pair with the given parser. michael@0: * michael@0: * @param value the NVP to parse michael@0: * @param parser the parser to use, or null for default michael@0: * michael@0: * @return the parsed name-value pair michael@0: */ michael@0: public final static michael@0: NameValuePair parseNameValuePair(final String value, michael@0: HeaderValueParser parser) michael@0: throws ParseException { michael@0: michael@0: if (value == null) { michael@0: throw new IllegalArgumentException michael@0: ("Value to parse may not be null"); michael@0: } michael@0: michael@0: if (parser == null) michael@0: parser = BasicHeaderValueParser.DEFAULT; michael@0: michael@0: CharArrayBuffer buffer = new CharArrayBuffer(value.length()); michael@0: buffer.append(value); michael@0: ParserCursor cursor = new ParserCursor(0, value.length()); michael@0: return parser.parseNameValuePair(buffer, cursor); michael@0: } michael@0: michael@0: michael@0: // non-javadoc, see interface HeaderValueParser michael@0: public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, michael@0: final ParserCursor cursor) { michael@0: return parseNameValuePair(buffer, cursor, ALL_DELIMITERS); michael@0: } michael@0: michael@0: private static boolean isOneOf(final char ch, final char[] chs) { michael@0: if (chs != null) { michael@0: for (int i = 0; i < chs.length; i++) { michael@0: if (ch == chs[i]) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, michael@0: final ParserCursor cursor, michael@0: final char[] delimiters) { michael@0: michael@0: if (buffer == null) { michael@0: throw new IllegalArgumentException("Char array buffer may not be null"); michael@0: } michael@0: if (cursor == null) { michael@0: throw new IllegalArgumentException("Parser cursor may not be null"); michael@0: } michael@0: michael@0: boolean terminated = false; michael@0: michael@0: int pos = cursor.getPos(); michael@0: int indexFrom = cursor.getPos(); michael@0: int indexTo = cursor.getUpperBound(); michael@0: michael@0: // Find name michael@0: String name = null; michael@0: while (pos < indexTo) { michael@0: char ch = buffer.charAt(pos); michael@0: if (ch == '=') { michael@0: break; michael@0: } michael@0: if (isOneOf(ch, delimiters)) { michael@0: terminated = true; michael@0: break; michael@0: } michael@0: pos++; michael@0: } michael@0: michael@0: if (pos == indexTo) { michael@0: terminated = true; michael@0: name = buffer.substringTrimmed(indexFrom, indexTo); michael@0: } else { michael@0: name = buffer.substringTrimmed(indexFrom, pos); michael@0: pos++; michael@0: } michael@0: michael@0: if (terminated) { michael@0: cursor.updatePos(pos); michael@0: return createNameValuePair(name, null); michael@0: } michael@0: michael@0: // Find value michael@0: String value = null; michael@0: int i1 = pos; michael@0: michael@0: boolean qouted = false; michael@0: boolean escaped = false; michael@0: while (pos < indexTo) { michael@0: char ch = buffer.charAt(pos); michael@0: if (ch == '"' && !escaped) { michael@0: qouted = !qouted; michael@0: } michael@0: if (!qouted && !escaped && isOneOf(ch, delimiters)) { michael@0: terminated = true; michael@0: break; michael@0: } michael@0: if (escaped) { michael@0: escaped = false; michael@0: } else { michael@0: escaped = qouted && ch == '\\'; michael@0: } michael@0: pos++; michael@0: } michael@0: michael@0: int i2 = pos; michael@0: // Trim leading white spaces michael@0: while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) { michael@0: i1++; michael@0: } michael@0: // Trim trailing white spaces michael@0: while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) { michael@0: i2--; michael@0: } michael@0: // Strip away quotes if necessary michael@0: if (((i2 - i1) >= 2) michael@0: && (buffer.charAt(i1) == '"') michael@0: && (buffer.charAt(i2 - 1) == '"')) { michael@0: i1++; michael@0: i2--; michael@0: } michael@0: value = buffer.substring(i1, i2); michael@0: if (terminated) { michael@0: pos++; michael@0: } michael@0: cursor.updatePos(pos); michael@0: return createNameValuePair(name, value); michael@0: } michael@0: michael@0: /** michael@0: * Creates a name-value pair. michael@0: * Called from {@link #parseNameValuePair}. michael@0: * michael@0: * @param name the name michael@0: * @param value the value, or null michael@0: * michael@0: * @return a name-value pair representing the arguments michael@0: */ michael@0: protected NameValuePair createNameValuePair(final String name, final String value) { michael@0: return new BasicNameValuePair(name, value); michael@0: } michael@0: michael@0: } michael@0: