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.NoSuchElementException; michael@0: michael@0: import ch.boye.httpclientandroidlib.FormattedHeader; michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.HeaderElementIterator; michael@0: import ch.boye.httpclientandroidlib.HeaderIterator; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * Basic implementation of a {@link HeaderElementIterator}. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class BasicHeaderElementIterator implements HeaderElementIterator { michael@0: michael@0: private final HeaderIterator headerIt; michael@0: private final HeaderValueParser parser; michael@0: michael@0: private HeaderElement currentElement = null; michael@0: private CharArrayBuffer buffer = null; michael@0: private ParserCursor cursor = null; michael@0: michael@0: /** michael@0: * Creates a new instance of BasicHeaderElementIterator michael@0: */ michael@0: public BasicHeaderElementIterator( michael@0: final HeaderIterator headerIterator, michael@0: final HeaderValueParser parser) { michael@0: if (headerIterator == null) { michael@0: throw new IllegalArgumentException("Header iterator may not be null"); michael@0: } michael@0: if (parser == null) { michael@0: throw new IllegalArgumentException("Parser may not be null"); michael@0: } michael@0: this.headerIt = headerIterator; michael@0: this.parser = parser; michael@0: } michael@0: michael@0: michael@0: public BasicHeaderElementIterator(final HeaderIterator headerIterator) { michael@0: this(headerIterator, BasicHeaderValueParser.DEFAULT); michael@0: } michael@0: michael@0: michael@0: private void bufferHeaderValue() { michael@0: this.cursor = null; michael@0: this.buffer = null; michael@0: while (this.headerIt.hasNext()) { michael@0: Header h = this.headerIt.nextHeader(); michael@0: if (h instanceof FormattedHeader) { michael@0: this.buffer = ((FormattedHeader) h).getBuffer(); michael@0: this.cursor = new ParserCursor(0, this.buffer.length()); michael@0: this.cursor.updatePos(((FormattedHeader) h).getValuePos()); michael@0: break; michael@0: } else { michael@0: String value = h.getValue(); michael@0: if (value != null) { michael@0: this.buffer = new CharArrayBuffer(value.length()); michael@0: this.buffer.append(value); michael@0: this.cursor = new ParserCursor(0, this.buffer.length()); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: private void parseNextElement() { michael@0: // loop while there are headers left to parse michael@0: while (this.headerIt.hasNext() || this.cursor != null) { michael@0: if (this.cursor == null || this.cursor.atEnd()) { michael@0: // get next header value michael@0: bufferHeaderValue(); michael@0: } michael@0: // Anything buffered? michael@0: if (this.cursor != null) { michael@0: // loop while there is data in the buffer michael@0: while (!this.cursor.atEnd()) { michael@0: HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor); michael@0: if (!(e.getName().length() == 0 && e.getValue() == null)) { michael@0: // Found something michael@0: this.currentElement = e; michael@0: return; michael@0: } michael@0: } michael@0: // if at the end of the buffer michael@0: if (this.cursor.atEnd()) { michael@0: // discard it michael@0: this.cursor = null; michael@0: this.buffer = null; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: public boolean hasNext() { michael@0: if (this.currentElement == null) { michael@0: parseNextElement(); michael@0: } michael@0: return this.currentElement != null; michael@0: } michael@0: michael@0: public HeaderElement nextElement() throws NoSuchElementException { michael@0: if (this.currentElement == null) { michael@0: parseNextElement(); michael@0: } michael@0: michael@0: if (this.currentElement == null) { michael@0: throw new NoSuchElementException("No more header elements available"); michael@0: } michael@0: michael@0: HeaderElement element = this.currentElement; michael@0: this.currentElement = null; michael@0: return element; michael@0: } michael@0: michael@0: public final Object next() throws NoSuchElementException { michael@0: return nextElement(); michael@0: } michael@0: michael@0: public void remove() throws UnsupportedOperationException { michael@0: throw new UnsupportedOperationException("Remove not supported"); michael@0: } michael@0: michael@0: }