001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java $
003: * $Revision: 592088 $
004: * $Date: 2007-11-05 18:03:39 +0100 (Mon, 05 Nov 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import java.util.NoSuchElementException;
035:
036: import org.apache.http.FormattedHeader;
037: import org.apache.http.Header;
038: import org.apache.http.HeaderElement;
039: import org.apache.http.HeaderElementIterator;
040: import org.apache.http.HeaderIterator;
041: import org.apache.http.util.CharArrayBuffer;
042:
043: /**
044: * Basic implementation of a {@link HeaderElementIterator}.
045: *
046: * @version $Revision: 592088 $
047: *
048: * @author Andrea Selva <selva.andre at gmail.com>
049: * @author Oleg Kalnichevski <oleg at ural.ru>
050: */
051: public class BasicHeaderElementIterator implements
052: HeaderElementIterator {
053:
054: private final HeaderIterator headerIt;
055: private final HeaderValueParser parser;
056:
057: private HeaderElement currentElement = null;
058: private CharArrayBuffer buffer = null;
059: private ParserCursor cursor = null;
060:
061: /**
062: * Creates a new instance of BasicHeaderElementIterator
063: */
064: public BasicHeaderElementIterator(
065: final HeaderIterator headerIterator,
066: final HeaderValueParser parser) {
067: if (headerIterator == null) {
068: throw new IllegalArgumentException(
069: "Header iterator may not be null");
070: }
071: if (parser == null) {
072: throw new IllegalArgumentException("Parser may not be null");
073: }
074: this .headerIt = headerIterator;
075: this .parser = parser;
076: }
077:
078: public BasicHeaderElementIterator(
079: final HeaderIterator headerIterator) {
080: this (headerIterator, BasicHeaderValueParser.DEFAULT);
081: }
082:
083: private void bufferHeaderValue() {
084: this .cursor = null;
085: this .buffer = null;
086: while (this .headerIt.hasNext()) {
087: Header h = this .headerIt.nextHeader();
088: if (h instanceof FormattedHeader) {
089: this .buffer = ((FormattedHeader) h).getBuffer();
090: this .cursor = new ParserCursor(0, this .buffer.length());
091: this .cursor.updatePos(((FormattedHeader) h)
092: .getValuePos());
093: break;
094: } else {
095: String value = h.getValue();
096: if (value != null) {
097: this .buffer = new CharArrayBuffer(value.length());
098: this .buffer.append(value);
099: this .cursor = new ParserCursor(0, this .buffer
100: .length());
101: break;
102: }
103: }
104: }
105: }
106:
107: private void parseNextElement() {
108: // loop while there are headers left to parse
109: while (this .headerIt.hasNext() || this .cursor != null) {
110: if (this .cursor == null || this .cursor.atEnd()) {
111: // get next header value
112: bufferHeaderValue();
113: }
114: // Anything buffered?
115: if (this .cursor != null) {
116: // loop while there is data in the buffer
117: while (!this .cursor.atEnd()) {
118: HeaderElement e = this .parser.parseHeaderElement(
119: this .buffer, this .cursor);
120: if (!(e.getName().length() == 0 && e.getValue() == null)) {
121: // Found something
122: this .currentElement = e;
123: return;
124: }
125: }
126: // if at the end of the buffer
127: if (this .cursor.atEnd()) {
128: // discard it
129: this .cursor = null;
130: this .buffer = null;
131: }
132: }
133: }
134: }
135:
136: public boolean hasNext() {
137: if (this .currentElement == null) {
138: parseNextElement();
139: }
140: return this .currentElement != null;
141: }
142:
143: public HeaderElement nextElement() throws NoSuchElementException {
144: if (this .currentElement == null) {
145: parseNextElement();
146: }
147:
148: if (this .currentElement == null) {
149: throw new NoSuchElementException(
150: "No more header elements available");
151: }
152:
153: HeaderElement element = this .currentElement;
154: this .currentElement = null;
155: return element;
156: }
157:
158: public final Object next() throws NoSuchElementException {
159: return nextElement();
160: }
161:
162: public void remove() throws UnsupportedOperationException {
163: throw new UnsupportedOperationException("Remove not supported");
164: }
165:
166: }
|