1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.message;
29
30 import java.util.NoSuchElementException;
31
32 import org.apache.http.FormattedHeader;
33 import org.apache.http.Header;
34 import org.apache.http.HeaderElement;
35 import org.apache.http.HeaderElementIterator;
36 import org.apache.http.HeaderIterator;
37 import org.apache.http.annotation.NotThreadSafe;
38 import org.apache.http.util.CharArrayBuffer;
39
40
41
42
43
44
45 @NotThreadSafe
46 public class BasicHeaderElementIterator implements HeaderElementIterator {
47
48 private final HeaderIterator headerIt;
49 private final HeaderValueParser parser;
50
51 private HeaderElement currentElement = null;
52 private CharArrayBuffer buffer = null;
53 private ParserCursor cursor = null;
54
55
56
57
58 public BasicHeaderElementIterator(
59 final HeaderIterator headerIterator,
60 final HeaderValueParser parser) {
61 if (headerIterator == null) {
62 throw new IllegalArgumentException("Header iterator may not be null");
63 }
64 if (parser == null) {
65 throw new IllegalArgumentException("Parser may not be null");
66 }
67 this.headerIt = headerIterator;
68 this.parser = parser;
69 }
70
71
72 public BasicHeaderElementIterator(final HeaderIterator headerIterator) {
73 this(headerIterator, BasicHeaderValueParser.DEFAULT);
74 }
75
76
77 private void bufferHeaderValue() {
78 this.cursor = null;
79 this.buffer = null;
80 while (this.headerIt.hasNext()) {
81 Header h = this.headerIt.nextHeader();
82 if (h instanceof FormattedHeader) {
83 this.buffer = ((FormattedHeader) h).getBuffer();
84 this.cursor = new ParserCursor(0, this.buffer.length());
85 this.cursor.updatePos(((FormattedHeader) h).getValuePos());
86 break;
87 } else {
88 String value = h.getValue();
89 if (value != null) {
90 this.buffer = new CharArrayBuffer(value.length());
91 this.buffer.append(value);
92 this.cursor = new ParserCursor(0, this.buffer.length());
93 break;
94 }
95 }
96 }
97 }
98
99 private void parseNextElement() {
100
101 while (this.headerIt.hasNext() || this.cursor != null) {
102 if (this.cursor == null || this.cursor.atEnd()) {
103
104 bufferHeaderValue();
105 }
106
107 if (this.cursor != null) {
108
109 while (!this.cursor.atEnd()) {
110 HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
111 if (!(e.getName().length() == 0 && e.getValue() == null)) {
112
113 this.currentElement = e;
114 return;
115 }
116 }
117
118 if (this.cursor.atEnd()) {
119
120 this.cursor = null;
121 this.buffer = null;
122 }
123 }
124 }
125 }
126
127 public boolean hasNext() {
128 if (this.currentElement == null) {
129 parseNextElement();
130 }
131 return this.currentElement != null;
132 }
133
134 public HeaderElement nextElement() throws NoSuchElementException {
135 if (this.currentElement == null) {
136 parseNextElement();
137 }
138
139 if (this.currentElement == null) {
140 throw new NoSuchElementException("No more header elements available");
141 }
142
143 HeaderElement element = this.currentElement;
144 this.currentElement = null;
145 return element;
146 }
147
148 public final Object next() throws NoSuchElementException {
149 return nextElement();
150 }
151
152 public void remove() throws UnsupportedOperationException {
153 throw new UnsupportedOperationException("Remove not supported");
154 }
155
156 }